1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

uucore: format: force NaN back to lowercase

Fixes formatting of `NaN` to `nan`.

Fixes part 1 of #7412.
This commit is contained in:
Nicolas Boichat 2025-03-07 12:19:36 +01:00
parent c0a1179e7c
commit e3872e8e8f

View file

@ -324,8 +324,9 @@ fn get_sign_indicator<T: PartialOrd + Default>(sign: PositiveSign, x: &T) -> Str
fn format_float_non_finite(f: f64, case: Case) -> String {
debug_assert!(!f.is_finite());
let mut s = format!("{f}");
if case == Case::Uppercase {
s.make_ascii_uppercase();
match case {
Case::Lowercase => s.make_ascii_lowercase(), // Forces NaN back to nan.
Case::Uppercase => s.make_ascii_uppercase(),
}
s
}
@ -550,6 +551,18 @@ mod test {
assert_eq!(f(8), "010");
}
#[test]
fn non_finite_float() {
use super::format_float_non_finite;
let f = |x| format_float_non_finite(x, Case::Lowercase);
assert_eq!(f(f64::NAN), "nan");
assert_eq!(f(f64::INFINITY), "inf");
let f = |x| format_float_non_finite(x, Case::Uppercase);
assert_eq!(f(f64::NAN), "NAN");
assert_eq!(f(f64::INFINITY), "INF");
}
#[test]
fn decimal_float() {
use super::format_float_decimal;