mirror of
https://github.com/nushell/nushell
synced 2024-11-15 01:17:07 +00:00
Handle inf/nan in delimited data (#2652)
This commit is contained in:
parent
2b076369e0
commit
6817b472d0
1 changed files with 41 additions and 7 deletions
|
@ -202,6 +202,14 @@ impl UntaggedValue {
|
|||
pub fn decimal_from_float(f: f64, span: Span) -> UntaggedValue {
|
||||
let dec = BigDecimal::from_f64(f);
|
||||
|
||||
// BigDecimal doesn't have the concept of inf/NaN so handle manually
|
||||
if f.is_sign_negative() && f.is_infinite() {
|
||||
UntaggedValue::from("-inf")
|
||||
} else if f.is_infinite() {
|
||||
UntaggedValue::from("inf")
|
||||
} else if f.is_nan() {
|
||||
UntaggedValue::from("NaN")
|
||||
} else {
|
||||
match dec {
|
||||
Some(dec) => UntaggedValue::Primitive(Primitive::Decimal(dec)),
|
||||
None => UntaggedValue::Error(ShellError::labeled_error(
|
||||
|
@ -211,6 +219,7 @@ impl UntaggedValue {
|
|||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper for creating binary (non-text) buffer values
|
||||
pub fn binary(binary: Vec<u8>) -> UntaggedValue {
|
||||
|
@ -550,3 +559,28 @@ pub fn merge_descriptors(values: &[Value]) -> Vec<String> {
|
|||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_decimal_from_float() {
|
||||
assert_eq!(
|
||||
UntaggedValue::from("inf"),
|
||||
UntaggedValue::decimal_from_float(f64::INFINITY, Span::default())
|
||||
);
|
||||
assert_eq!(
|
||||
UntaggedValue::from("-inf"),
|
||||
UntaggedValue::decimal_from_float(f64::NEG_INFINITY, Span::default())
|
||||
);
|
||||
assert_eq!(
|
||||
UntaggedValue::from("NaN"),
|
||||
UntaggedValue::decimal_from_float(f64::NAN, Span::default())
|
||||
);
|
||||
assert_eq!(
|
||||
UntaggedValue::from(5.5),
|
||||
UntaggedValue::decimal_from_float(5.5, Span::default())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue