feat: add functions to suport scientific notations
@@ -675,7 +675,12 @@ fn field_uinteger_value(i: &str) -> IResult<&str, u64> {
}
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| {
value.parse().context(FloatValueInvalid { value })
})(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)
}
+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> {
integral_value_signed(i)
}
@@ -1473,9 +1494,23 @@ mod test {
#[test]
fn parse_scientific_float() -> Result {
let input = "m0 field=-1.234456e+06 1615869152385000000";
- //let input = "m0 field=10";
+ let vals = 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!(
+ matches!(parsed, Err(super::Error::CannotParseEntireLine { .. })),
+ "Wrong error: {:?}",
+ parsed,
+ );
+ let input = "m0 field=1.234456e06 1615869152385000000";
+ let parsed = parse(input);
assert!(
matches!(parsed, Err(super::Error::CannotParseEntireLine { .. })),
"Wrong error: {:?}",
