From 367cc19d455a91547d14c2e06ea651fa6fb5220f Mon Sep 17 00:00:00 2001 From: aimerlief <152078880+aimerlief@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:42:34 +0900 Subject: [PATCH] fix(seq): handle 0e... scientific notation without padding (#6934) * fix(seq): handle 0e... scientific notation without padding - Updated the parse_exponent_no_decimal function to treat 0e... as zero. - Added test cases to verify correct behavior for 0e15 and -w 0e15. Fix: #6926 * fix(seq): improved parse for accurate BigDecimal handling * apply missing cargo fmt formatting adjustments --- src/uu/seq/src/numberparse.rs | 22 ++++++++++++++++++++-- tests/by-util/test_seq.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/uu/seq/src/numberparse.rs b/src/uu/seq/src/numberparse.rs index 891fa2ce6..80587f713 100644 --- a/src/uu/seq/src/numberparse.rs +++ b/src/uu/seq/src/numberparse.rs @@ -102,7 +102,16 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result() + .map_err(|_| ParseNumberError::Float)?; + if parsed_decimal == BigDecimal::zero() { + BigDecimal::zero() + } else { + parsed_decimal + } + }; let num_integral_digits = if is_minus_zero_float(s, &x) { if exponent > 0 { @@ -204,7 +213,16 @@ fn parse_decimal_and_exponent( // Because of the match guard, this subtraction will not underflow. let num_digits_between_decimal_point_and_e = (j - (i + 1)) as i64; let exponent: i64 = s[j + 1..].parse().map_err(|_| ParseNumberError::Float)?; - let val: BigDecimal = s.parse().map_err(|_| ParseNumberError::Float)?; + let val: BigDecimal = { + let parsed_decimal = s + .parse::() + .map_err(|_| ParseNumberError::Float)?; + if parsed_decimal == BigDecimal::zero() { + BigDecimal::zero() + } else { + parsed_decimal + } + }; let num_integral_digits = { let minimum: usize = { diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index ab0659f21..62a0212b1 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -842,3 +842,31 @@ fn test_invalid_format() { .no_stdout() .stderr_contains("format '%g%g' has too many % directives"); } + +#[test] +fn test_parse_scientific_zero() { + new_ucmd!() + .args(&["0e15", "1"]) + .succeeds() + .stdout_only("0\n1\n"); + new_ucmd!() + .args(&["0.0e15", "1"]) + .succeeds() + .stdout_only("0\n1\n"); + new_ucmd!() + .args(&["0", "1"]) + .succeeds() + .stdout_only("0\n1\n"); + new_ucmd!() + .args(&["-w", "0e15", "1"]) + .succeeds() + .stdout_only("0000000000000000\n0000000000000001\n"); + new_ucmd!() + .args(&["-w", "0.0e15", "1"]) + .succeeds() + .stdout_only("0000000000000000\n0000000000000001\n"); + new_ucmd!() + .args(&["-w", "0", "1"]) + .succeeds() + .stdout_only("0\n1\n"); +}