1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

printf: fix negative hex argument parsing

This commit is contained in:
Terts Diepraam 2023-11-17 14:57:09 +01:00
parent ce18e0ab97
commit 5f2374b339

View file

@ -49,9 +49,13 @@ impl FormatArgument {
match self {
Self::SignedInt(n) => Some(*n),
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") {
// For hex, we parse `u64` because we do not allow another
// minus sign. We might need to do more precise parsing here.
if let Some(s) = s.strip_prefix("-0x") {
Some(- (u64::from_str_radix(s, 16).ok()? as i64))
} else if let Some(s) = s.strip_prefix("0x") {
Some(u64::from_str_radix(s, 16).ok()? as i64)
} else if s.starts_with("-0") || s.starts_with('0') {
i64::from_str_radix(s, 8).ok()
} else if let Some(s) = s.strip_prefix('\'') {
Some(s.chars().next()? as i64)