mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #5124 from shinhs0506/seq-parse
seq: parse "infinity" and "-infinity"
This commit is contained in:
commit
bc7877b58c
2 changed files with 29 additions and 24 deletions
|
@ -73,30 +73,13 @@ fn parse_no_decimal_no_exponent(s: &str) -> Result<PreciseNumber, ParseNumberErr
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
// Possibly "NaN" or "inf".
|
// Possibly "NaN" or "inf".
|
||||||
//
|
let float_val = match s.to_ascii_lowercase().as_str() {
|
||||||
// TODO In Rust v1.53.0, this change
|
"inf" | "infinity" => ExtendedBigDecimal::Infinity,
|
||||||
// https://github.com/rust-lang/rust/pull/78618 improves the
|
"-inf" | "-infinity" => ExtendedBigDecimal::MinusInfinity,
|
||||||
// parsing of floats to include being able to parse "NaN"
|
"nan" | "-nan" => return Err(ParseNumberError::Nan),
|
||||||
// and "inf". So when the minimum version of this crate is
|
_ => return Err(ParseNumberError::Float),
|
||||||
// increased to 1.53.0, we should just use the built-in
|
};
|
||||||
// `f32` parsing instead.
|
Ok(PreciseNumber::new(Number::Float(float_val), 0, 0))
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -483,11 +466,23 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_inf() {
|
fn test_parse_inf() {
|
||||||
assert_eq!(parse("inf"), 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::Infinity));
|
assert_eq!(parse("+inf"), Number::Float(ExtendedBigDecimal::Infinity));
|
||||||
|
assert_eq!(
|
||||||
|
parse("+infinity"),
|
||||||
|
Number::Float(ExtendedBigDecimal::Infinity)
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("-inf"),
|
parse("-inf"),
|
||||||
Number::Float(ExtendedBigDecimal::MinusInfinity)
|
Number::Float(ExtendedBigDecimal::MinusInfinity)
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse("-infinity"),
|
||||||
|
Number::Float(ExtendedBigDecimal::MinusInfinity)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -623,11 +623,21 @@ fn test_neg_inf() {
|
||||||
run(&["--", "-inf", "0"], b"-inf\n-inf\n-inf\n");
|
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]
|
#[test]
|
||||||
fn test_inf() {
|
fn test_inf() {
|
||||||
run(&["inf"], b"1\n2\n3\n");
|
run(&["inf"], b"1\n2\n3\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_infinity() {
|
||||||
|
run(&["infinity"], b"1\n2\n3\n");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inf_width() {
|
fn test_inf_width() {
|
||||||
run(
|
run(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue