From 3b884966ac207107feb12a831e63083d746b06cb Mon Sep 17 00:00:00 2001 From: Marras Antoine Date: Mon, 8 Jan 2024 14:30:56 +0100 Subject: [PATCH 1/2] printf: added failing tests on alternative hex form --- tests/by-util/test_printf.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index 48fc1e6ac..f162df490 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -639,3 +639,29 @@ fn partial_char() { "printf: warning: bc: character(s) following character constant have been ignored\n", ); } + +#[test] +fn sub_alternative_lower_hex_0() { + new_ucmd!().args(&["%#x", "0"]).succeeds().stdout_only("0"); +} + +#[test] +fn sub_alternative_lower_hex() { + new_ucmd!() + .args(&["%#x", "42"]) + .succeeds() + .stdout_only("0x2a"); +} + +#[test] +fn sub_alternative_upper_hex_0() { + new_ucmd!().args(&["%#X", "0"]).succeeds().stdout_only("0"); +} + +#[test] +fn sub_alternative_upper_hex() { + new_ucmd!() + .args(&["%#X", "42"]) + .succeeds() + .stdout_only("0x2A"); +} From 0648321d9796fd709adb7a78b169be3859d93dfb Mon Sep 17 00:00:00 2001 From: Marras Antoine Date: Mon, 8 Jan 2024 15:08:18 +0100 Subject: [PATCH 2/2] printf: 0x not shown anymore in front of 0 while in alternative mode --- src/uucore/src/lib/features/format/num_format.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/uucore/src/lib/features/format/num_format.rs b/src/uucore/src/lib/features/format/num_format.rs index 325da3ce6..9b7e805f0 100644 --- a/src/uucore/src/lib/features/format/num_format.rs +++ b/src/uucore/src/lib/features/format/num_format.rs @@ -156,13 +156,21 @@ impl Formatter for UnsignedInt { format!("{x:x}") } UnsignedIntVariant::Hexadecimal(Case::Lowercase, Prefix::Yes) => { - format!("{x:#x}") + if x == 0 { + "0".to_string() + } else { + format!("{x:#x}") + } } UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::No) => { format!("{x:X}") } UnsignedIntVariant::Hexadecimal(Case::Uppercase, Prefix::Yes) => { - format!("{x:#X}") + if x == 0 { + "0".to_string() + } else { + format!("{x:#X}") + } } };