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
This commit is contained in:
aimerlief 2024-12-08 00:42:34 +09:00 committed by GitHub
parent f7c38a3079
commit 367cc19d45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 2 deletions

View file

@ -102,7 +102,16 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result<PreciseNumber, ParseNu
// displayed in decimal notation. For example, "1e-2" will be
// displayed as "0.01", but "1e2" will be displayed as "100",
// without a decimal point.
let x: BigDecimal = s.parse().map_err(|_| ParseNumberError::Float)?;
let x: BigDecimal = {
let parsed_decimal = s
.parse::<BigDecimal>()
.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::<BigDecimal>()
.map_err(|_| ParseNumberError::Float)?;
if parsed_decimal == BigDecimal::zero() {
BigDecimal::zero()
} else {
parsed_decimal
}
};
let num_integral_digits = {
let minimum: usize = {

View file

@ -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");
}