From 71624c3e49a8eb7b37eb591ca8c605696d8c0b33 Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Mon, 6 Jun 2022 11:33:43 -0500 Subject: [PATCH] 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. --- lexical-parse-integer/src/shared.rs | 2 ++ lexical-parse-integer/tests/api_tests.rs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/lexical-parse-integer/src/shared.rs b/lexical-parse-integer/src/shared.rs index 41e3ca41..fff53f58 100644 --- a/lexical-parse-integer/src/shared.rs +++ b/lexical-parse-integer/src/shared.rs @@ -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) } diff --git a/lexical-parse-integer/tests/api_tests.rs b/lexical-parse-integer/tests/api_tests.rs index 58380f92..329b244f 100644 --- a/lexical-parse-integer/tests/api_tests.rs +++ b/lexical-parse-integer/tests/api_tests.rs @@ -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]