Skip to content

Commit

Permalink
Fix return of negative integers with partial parser.
Browse files Browse the repository at this point in the history
Previously, numbers such as `-1a` would return `Ok((1, 2))` from the
partial parser, because the wrapping, negative value wasn't handled.
This changes it to correctly return `Ok((-1, 2))`.

Closes #86.
  • Loading branch information
Alexhuszagh committed Jun 6, 2022
1 parent 17dca4a commit 71624c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lexical-parse-integer/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ macro_rules! invalid_digit_partial {
} else {
into_error!(Overflow, (count - 1).min(min + 1))
}
} else if <$t>::IS_SIGNED && $is_negative {
into_ok_partial!($value.wrapping_neg(), $iter.cursor() - 1)
} else {
into_ok_partial!($value, $iter.cursor() - 1)
}
Expand Down
5 changes: 5 additions & 0 deletions lexical-parse-integer/tests/api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ fn i8_decimal_test() {
assert_eq!(Err(Error::Overflow(2)), i8::from_lexical(b"255"));
assert_eq!(Ok(-1), i8::from_lexical(b"-1"));
assert_eq!(Err(Error::InvalidDigit(1)), i8::from_lexical(b"1a"));

assert_eq!(Ok((1, 1)), i8::from_lexical_partial(b"1"));
assert_eq!(Ok((1, 1)), i8::from_lexical_partial(b"1a"));
assert_eq!(Ok((-1, 2)), i8::from_lexical_partial(b"-1"));
assert_eq!(Ok((-1, 2)), i8::from_lexical_partial(b"-1a"));
}

#[test]
Expand Down

0 comments on commit 71624c3

Please sign in to comment.