feat: add functions to suport scientific notations
parent
13d361e645
commit
b758804df7
|
@ -675,7 +675,12 @@ fn field_uinteger_value(i: &str) -> IResult<&str, u64> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn field_float_value(i: &str) -> IResult<&str, f64> {
|
fn field_float_value(i: &str) -> IResult<&str, f64> {
|
||||||
let value = alt((field_float_value_with_decimal, field_float_value_no_decimal));
|
let value = alt((
|
||||||
|
field_float_value_with_exponential_and_decimal,
|
||||||
|
field_float_value_with_exponential_no_decimal,
|
||||||
|
field_float_value_with_decimal,
|
||||||
|
field_float_value_no_decimal,
|
||||||
|
));
|
||||||
map_fail(value, |value| {
|
map_fail(value, |value| {
|
||||||
value.parse().context(FloatValueInvalid { value })
|
value.parse().context(FloatValueInvalid { value })
|
||||||
})(i)
|
})(i)
|
||||||
|
@ -685,6 +690,22 @@ fn field_float_value_with_decimal(i: &str) -> IResult<&str, &str> {
|
||||||
recognize(separated_pair(integral_value_signed, tag("."), digit1))(i)
|
recognize(separated_pair(integral_value_signed, tag("."), digit1))(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn field_float_value_with_exponential_and_decimal(i: &str) -> IResult<&str, &str> {
|
||||||
|
recognize(separated_pair(
|
||||||
|
integral_value_signed,
|
||||||
|
tag("."),
|
||||||
|
exponential_value,
|
||||||
|
))(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn field_float_value_with_exponential_no_decimal(i: &str) -> IResult<&str, &str> {
|
||||||
|
exponential_value(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exponential_value(i: &str) -> IResult<&str, &str> {
|
||||||
|
recognize(separated_pair(digit1, tag("e+"), digit1))(i)
|
||||||
|
}
|
||||||
|
|
||||||
fn field_float_value_no_decimal(i: &str) -> IResult<&str, &str> {
|
fn field_float_value_no_decimal(i: &str) -> IResult<&str, &str> {
|
||||||
integral_value_signed(i)
|
integral_value_signed(i)
|
||||||
}
|
}
|
||||||
|
@ -1473,16 +1494,30 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_scientific_float() -> Result {
|
fn parse_scientific_float() -> Result {
|
||||||
let input = "m0 field=-1.234456e+06 1615869152385000000";
|
let input = "m0 field=-1.234456e+06 1615869152385000000";
|
||||||
//let input = "m0 field=10";
|
let vals = parse(input)?;
|
||||||
let parsed = parse(input);
|
assert_eq!(vals.len(), 1);
|
||||||
|
|
||||||
|
let input = "m0 field=1.234456e+06 1615869152385000000";
|
||||||
|
let vals = parse(input)?;
|
||||||
|
assert_eq!(vals.len(), 1);
|
||||||
|
|
||||||
|
let input = "m0 field=-1.234456e06 1615869152385000000";
|
||||||
|
let parsed = parse(input);
|
||||||
assert!(
|
assert!(
|
||||||
matches!(parsed, Err(super::Error::CannotParseEntireLine { .. })),
|
matches!(parsed, Err(super::Error::CannotParseEntireLine { .. })),
|
||||||
"Wrong error: {:?}",
|
"Wrong error: {:?}",
|
||||||
parsed,
|
parsed,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
let input = "m0 field=1.234456e06 1615869152385000000";
|
||||||
|
let parsed = parse(input);
|
||||||
|
assert!(
|
||||||
|
matches!(parsed, Err(super::Error::CannotParseEntireLine { .. })),
|
||||||
|
"Wrong error: {:?}",
|
||||||
|
parsed,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue