From d345a280bf588b325babb29c60dc7d84d2c22832 Mon Sep 17 00:00:00 2001 From: John Shin Date: Sun, 30 Jul 2023 14:37:17 -0700 Subject: [PATCH 1/3] seq: update inf and nan parsing --- src/uu/seq/src/numberparse.rs | 40 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/uu/seq/src/numberparse.rs b/src/uu/seq/src/numberparse.rs index 23d94ea2b79..1921e2e4c4e 100644 --- a/src/uu/seq/src/numberparse.rs +++ b/src/uu/seq/src/numberparse.rs @@ -69,27 +69,25 @@ fn parse_no_decimal_no_exponent(s: &str) -> Result { // Possibly "NaN" or "inf". - // - // TODO In Rust v1.53.0, this change - // https://github.com/rust-lang/rust/pull/78618 improves the - // parsing of floats to include being able to parse "NaN" - // and "inf". So when the minimum version of this crate is - // increased to 1.53.0, we should just use the built-in - // `f32` parsing instead. - if s.eq_ignore_ascii_case("inf") { - Ok(PreciseNumber::new( - Number::Float(ExtendedBigDecimal::Infinity), - 0, - 0, - )) - } else if s.eq_ignore_ascii_case("-inf") { - Ok(PreciseNumber::new( - Number::Float(ExtendedBigDecimal::MinusInfinity), - 0, - 0, - )) - } else if s.eq_ignore_ascii_case("nan") || s.eq_ignore_ascii_case("-nan") { - Err(ParseNumberError::Nan) + if let Ok(num) = f32::from_str(s) { + // pattern matching on floating point literal is not encouraged 'https://github.com/rust-lang/rust/issues/41620' + if num == f32::INFINITY { + Ok(PreciseNumber::new( + Number::Float(ExtendedBigDecimal::Infinity), + 0, + 0, + )) + } else if num == f32::NEG_INFINITY { + Ok(PreciseNumber::new( + Number::Float(ExtendedBigDecimal::MinusInfinity), + 0, + 0, + )) + } else if num.is_nan() { + Err(ParseNumberError::Nan) + } else { + Err(ParseNumberError::Float) + } } else { Err(ParseNumberError::Float) } From 2bb56d44a4161b1b40225f27f8e30090910aae38 Mon Sep 17 00:00:00 2001 From: John Shin Date: Sat, 5 Aug 2023 18:23:29 -0700 Subject: [PATCH 2/3] seq: add tests for infinity and -infinity args --- src/uu/seq/src/numberparse.rs | 12 ++++++++++++ tests/by-util/test_seq.rs | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/uu/seq/src/numberparse.rs b/src/uu/seq/src/numberparse.rs index 1921e2e4c4e..156f80fb91f 100644 --- a/src/uu/seq/src/numberparse.rs +++ b/src/uu/seq/src/numberparse.rs @@ -477,11 +477,23 @@ mod tests { #[test] fn test_parse_inf() { assert_eq!(parse("inf"), Number::Float(ExtendedBigDecimal::Infinity)); + assert_eq!( + parse("infinity"), + Number::Float(ExtendedBigDecimal::Infinity) + ); assert_eq!(parse("+inf"), Number::Float(ExtendedBigDecimal::Infinity)); + assert_eq!( + parse("+infinity"), + Number::Float(ExtendedBigDecimal::Infinity) + ); assert_eq!( parse("-inf"), Number::Float(ExtendedBigDecimal::MinusInfinity) ); + assert_eq!( + parse("-infinity"), + Number::Float(ExtendedBigDecimal::MinusInfinity) + ); } #[test] diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index de078191251..95de2f3cd0a 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -619,11 +619,21 @@ fn test_neg_inf() { run(&["--", "-inf", "0"], b"-inf\n-inf\n-inf\n"); } +#[test] +fn test_neg_infinity() { + run(&["--", "-infinity", "0"], b"-inf\n-inf\n-inf\n"); +} + #[test] fn test_inf() { run(&["inf"], b"1\n2\n3\n"); } +#[test] +fn test_infinity() { + run(&["infinity"], b"1\n2\n3\n"); +} + #[test] fn test_inf_width() { run( From 9c0b9cae5610a81c2c9d152edb7fcca1c77e83dd Mon Sep 17 00:00:00 2001 From: John Shin Date: Wed, 27 Sep 2023 14:55:08 -0700 Subject: [PATCH 3/3] seq: simplyfy nan and inf parsing --- src/uu/seq/src/numberparse.rs | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/uu/seq/src/numberparse.rs b/src/uu/seq/src/numberparse.rs index 156f80fb91f..07d853d586c 100644 --- a/src/uu/seq/src/numberparse.rs +++ b/src/uu/seq/src/numberparse.rs @@ -69,28 +69,13 @@ fn parse_no_decimal_no_exponent(s: &str) -> Result { // Possibly "NaN" or "inf". - if let Ok(num) = f32::from_str(s) { - // pattern matching on floating point literal is not encouraged 'https://github.com/rust-lang/rust/issues/41620' - if num == f32::INFINITY { - Ok(PreciseNumber::new( - Number::Float(ExtendedBigDecimal::Infinity), - 0, - 0, - )) - } else if num == f32::NEG_INFINITY { - Ok(PreciseNumber::new( - Number::Float(ExtendedBigDecimal::MinusInfinity), - 0, - 0, - )) - } else if num.is_nan() { - Err(ParseNumberError::Nan) - } else { - Err(ParseNumberError::Float) - } - } else { - Err(ParseNumberError::Float) - } + let float_val = match s.to_ascii_lowercase().as_str() { + "inf" | "infinity" => ExtendedBigDecimal::Infinity, + "-inf" | "-infinity" => ExtendedBigDecimal::MinusInfinity, + "nan" | "-nan" => return Err(ParseNumberError::Nan), + _ => return Err(ParseNumberError::Float), + }; + Ok(PreciseNumber::new(Number::Float(float_val), 0, 0)) } } }