From 3ab68bad104b176d62352b7366691ce25272fe08 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Tue, 25 Mar 2025 11:57:57 +0100 Subject: [PATCH] uucore: format: Fix hexadecimal uppercase print (again) When '%A' format is specified, we also need to capitalize the `0x`, i.e. `0XEP-3`, not `0xEP-3`. --- src/uucore/src/lib/features/format/num_format.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/uucore/src/lib/features/format/num_format.rs b/src/uucore/src/lib/features/format/num_format.rs index b636744df..f89c52102 100644 --- a/src/uucore/src/lib/features/format/num_format.rs +++ b/src/uucore/src/lib/features/format/num_format.rs @@ -509,9 +509,9 @@ fn format_float_hexadecimal( ) -> String { debug_assert!(!bd.is_negative()); - let exp_char = match case { - Case::Lowercase => 'p', - Case::Uppercase => 'P', + let (prefix, exp_char) = match case { + Case::Lowercase => ("0x", 'p'), + Case::Uppercase => ("0X", 'P'), }; if BigDecimal::zero().eq(bd) { @@ -607,7 +607,7 @@ fn format_float_hexadecimal( "" }; - format!("0x{first_digit}{dot}{remaining_digits}{exp_char}{exponent:+}") + format!("{prefix}{first_digit}{dot}{remaining_digits}{exp_char}{exponent:+}") } fn strip_fractional_zeroes_and_dot(s: &mut String) { @@ -964,8 +964,8 @@ mod test { ForceDecimal::No, ) }; - assert_eq!(f("0.00001"), "0xA.7C5AC4P-20"); - assert_eq!(f("0.125"), "0x8.000000P-6"); + assert_eq!(f("0.00001"), "0XA.7C5AC4P-20"); + assert_eq!(f("0.125"), "0X8.000000P-6"); // Test "0e10"/"0e-10". From cppreference.com: "If the value is ​0​, the exponent is also ​0​." let f = |digits, scale| { @@ -1178,7 +1178,7 @@ mod test { assert_eq!(f("%e", &(-123.0).into()), "-1.230000e+02"); assert_eq!(f("%#09.e", &(-100.0).into()), "-001.e+02"); assert_eq!(f("%# 9.E", &100.0.into()), " 1.E+02"); - assert_eq!(f("% 12.2A", &(-100.0).into()), " -0xC.80P+3"); + assert_eq!(f("% 12.2A", &(-100.0).into()), " -0XC.80P+3"); } #[test]