Skip to content

Commit

Permalink
Merge pull request #5124 from shinhs0506/seq-parse
Browse files Browse the repository at this point in the history
seq: parse "infinity" and "-infinity"
  • Loading branch information
cakebaker authored Sep 30, 2023
2 parents 035032c + 9c0b9ca commit bc7877b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/uu/seq/src/numberparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,13 @@ fn parse_no_decimal_no_exponent(s: &str) -> Result<PreciseNumber, ParseNumberErr
}
Err(_) => {
// Possibly "NaN" or "inf".
//
// TODO In Rust v1.53.0, this change
// https:/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)
} 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))
}
}
}
Expand Down Expand Up @@ -483,11 +466,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]
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,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(
Expand Down

0 comments on commit bc7877b

Please sign in to comment.