Skip to content

Commit 71624c3

Browse files
committed
Fix return of negative integers with partial parser.
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.
1 parent 17dca4a commit 71624c3

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lexical-parse-integer/src/shared.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ macro_rules! invalid_digit_partial {
8686
} else {
8787
into_error!(Overflow, (count - 1).min(min + 1))
8888
}
89+
} else if <$t>::IS_SIGNED && $is_negative {
90+
into_ok_partial!($value.wrapping_neg(), $iter.cursor() - 1)
8991
} else {
9092
into_ok_partial!($value, $iter.cursor() - 1)
9193
}

lexical-parse-integer/tests/api_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ fn i8_decimal_test() {
2828
assert_eq!(Err(Error::Overflow(2)), i8::from_lexical(b"255"));
2929
assert_eq!(Ok(-1), i8::from_lexical(b"-1"));
3030
assert_eq!(Err(Error::InvalidDigit(1)), i8::from_lexical(b"1a"));
31+
32+
assert_eq!(Ok((1, 1)), i8::from_lexical_partial(b"1"));
33+
assert_eq!(Ok((1, 1)), i8::from_lexical_partial(b"1a"));
34+
assert_eq!(Ok((-1, 2)), i8::from_lexical_partial(b"-1"));
35+
assert_eq!(Ok((-1, 2)), i8::from_lexical_partial(b"-1a"));
3136
}
3237

3338
#[test]

0 commit comments

Comments
 (0)