8000 seq: Removed zero-padding of string when parsing with parse_exponent_no_decimal by maxer137 · Pull Request #6185 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

seq: Removed zero-padding of string when parsing with parse_exponent_no_decimal #6185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 23 additions & 43 deletions src/uu/seq/src/numberparse.rs
39CE
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result<PreciseNumber, ParseNu
// 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 num_integral_digits = if is_minus_zero_float(s, &x) {
2
if exponent > 0 {
2usize + exponent as usize
} else {
2usize
}
} else {
let total = j as i64 + exponent;
let result = if total < 1 {
Expand All @@ -120,24 +125,18 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result<PreciseNumber, ParseNu
};
let num_fractional_digits = if exponent < 0 { -exponent as usize } else { 0 };

if exponent < 0 {
if is_minus_zero_float(s, &x) {
Ok(PreciseNumber::new(
ExtendedBigDecimal::MinusZero,
num_integral_digits,
num_fractional_digits,
))
} else {
Ok(PreciseNumber::new(
ExtendedBigDecimal::BigDecimal(x),
num_integral_digits,
num_fractional_digits,
))
}
if is_minus_zero_float(s, &x) {
Ok(PreciseNumber::new(
ExtendedBigDecimal::MinusZero,
num_integral_digits,
num_fractional_digits,
))
} else {
let zeros = "0".repeat(exponent.try_into().unwrap());
let expanded = [&s[0..j], &zeros].concat();
parse_no_decimal_no_exponent(&expanded)
Ok(PreciseNumber::new(
ExtendedBigDecimal::BigDecimal(x),
num_integral_digits,
num_fractional_digits,
))
}
}

Expand Down Expand Up @@ -207,7 +206,11 @@ fn parse_decimal_and_exponent(
let minimum: usize = {
let integral_part: f64 = s[..j].parse().map_err(|_| ParseNumberError::Float)?;
if integral_part.is_sign_negative() {
2
if exponent > 0 {
2usize + exponent as usize
} else {
2usize
}
} else {
1
}
Expand All @@ -234,30 +237,7 @@ fn parse_decimal_and_exponent(
.unwrap()
};

if num_digits_between_decimal_point_and_e <= exponent {
if is_minus_zero_float(s, &val) {
Ok(PreciseNumber::new(
ExtendedBigDecimal::MinusZero,
num_integral_digits,
num_fractional_digits,
))
} else {
let zeros: String = "0".repeat(
(exponent - num_digits_between_decimal_point_and_e)
.try_into()
.unwrap(),
);
let expanded = [&s[0..i], &s[i + 1..j], &zeros].concat();
let n = expanded
.parse::<BigDecimal>()
.map_err(|_| ParseNumberError::Float)?;
Ok(PreciseNumber::new(
ExtendedBigDecimal::BigDecimal(n),
num_integral_digits,
num_fractional_digits,
))
}
} else if is_minus_zero_float(s, &val) {
if is_minus_zero_float(s, &val) {
Ok(PreciseNumber::new(
ExtendedBigDecimal::MinusZero,
num_integral_digits,
Expand Down
0