mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 05:27:45 +00:00
printf: more flexible parsing of unparsed arguments
This commit is contained in:
parent
eaf5006379
commit
a45ff8ca73
1 changed files with 34 additions and 6 deletions
|
@ -30,7 +30,17 @@ impl FormatArgument {
|
|||
pub fn get_u64(&self) -> Option<u64> {
|
||||
match self {
|
||||
Self::UnsignedInt(n) => Some(*n),
|
||||
Self::Unparsed(s) => s.parse().ok(),
|
||||
Self::Unparsed(s) => {
|
||||
if let Some(s) = s.strip_prefix("0x") {
|
||||
u64::from_str_radix(s, 16).ok()
|
||||
} else if let Some(s) = s.strip_prefix("0") {
|
||||
u64::from_str_radix(s, 8).ok()
|
||||
} else if let Some(s) = s.strip_prefix('\'') {
|
||||
Some(s.chars().next()? as u64)
|
||||
} else {
|
||||
s.parse().ok()
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +48,17 @@ impl FormatArgument {
|
|||
pub fn get_i64(&self) -> Option<i64> {
|
||||
match self {
|
||||
Self::SignedInt(n) => Some(*n),
|
||||
Self::Unparsed(s) => s.parse().ok(),
|
||||
Self::Unparsed(s) => {
|
||||
if let Some(s) = s.strip_prefix("0x") {
|
||||
i64::from_str_radix(s, 16).ok()
|
||||
} else if let Some(s) = s.strip_prefix("0") {
|
||||
i64::from_str_radix(s, 8).ok()
|
||||
} else if let Some(s) = s.strip_prefix('\'') {
|
||||
Some(s.chars().next()? as i64)
|
||||
} else {
|
||||
s.parse().ok()
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +66,15 @@ impl FormatArgument {
|
|||
pub fn get_f64(&self) -> Option<f64> {
|
||||
match self {
|
||||
Self::Float(n) => Some(*n),
|
||||
Self::Unparsed(s) => s.parse().ok(),
|
||||
Self::Unparsed(s) => {
|
||||
if s.starts_with("0x") || s.starts_with("-0x") {
|
||||
unimplemented!("Hexadecimal floats are unimplemented!")
|
||||
} else if let Some(s) = s.strip_prefix('\'') {
|
||||
Some(s.chars().next()? as u64 as f64)
|
||||
} else {
|
||||
s.parse().ok()
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue