1
Fork 0
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:
Terts Diepraam 2023-11-17 14:39:39 +01:00
parent eaf5006379
commit a45ff8ca73

View file

@ -30,23 +30,51 @@ impl FormatArgument {
pub fn get_u64(&self) -> Option<u64> { pub fn get_u64(&self) -> Option<u64> {
match self { match self {
Self::UnsignedInt(n) => Some(*n), 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, _ => None,
} }
} }
pub fn get_i64(&self) -> Option<i64> { pub fn get_i64(&self) -> Option<i64> {
match self { match self {
Self::SignedInt(n) => Some(*n), 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, _ => None,
} }
} }
pub fn get_f64(&self) -> Option<f64> { pub fn get_f64(&self) -> Option<f64> {
match self { match self {
Self::Float(n) => Some(*n), 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, _ => None,
} }
} }
@ -57,4 +85,4 @@ impl FormatArgument {
_ => None, _ => None,
} }
} }
} }