parse scientific notation

This commit is contained in:
Folkert 2021-02-26 16:09:17 +01:00
parent fb460ce984
commit 94fc5a1935
2 changed files with 26 additions and 0 deletions

View file

@ -174,6 +174,16 @@ mod test_can {
assert_can_float("-0.0", -0.0);
}
#[test]
fn scientific_positive() {
assert_can_float("5e4", 5000.0);
}
#[test]
fn scientific_negative() {
assert_can_float("5e-4", 0.0005);
}
#[test]
fn num_max() {
assert_can_num(&(i64::MAX.to_string()), i64::MAX);

View file

@ -118,6 +118,22 @@ fn chomp_number<'a>(mut bytes: &'a [u8]) -> (bool, usize) {
is_float = true;
bytes = &bytes[1..];
}
b'e' => {
// maybe scientific notation?
match bytes.get(1) {
Some(b'-') => {
is_float = true;
bytes = &bytes[2..];
}
Some(c) if (*c as char).is_ascii_digit() => {
is_float = true;
bytes = &bytes[2..];
}
_ => {
bytes = &bytes[1..];
}
}
}
b'_' => {
// skip
bytes = &bytes[1..];