Skip to content

Commit

Permalink
Add minor patches for the correct indexes on format errors in parsing…
Browse files Browse the repository at this point in the history
… integers.
  • Loading branch information
Alexhuszagh committed Jan 24, 2025
1 parent 1b955e2 commit a023bef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lexical-parse-integer/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ macro_rules! on_invalid_digit {
if $iter.is_buffer_empty() && $is_end {
$into_ok!($value, $iter.buffer_length(), have_any)
} else {
$invalid_digit!($value, $iter.cursor(), have_any)
$invalid_digit!($value, $iter.cursor() + 1, have_any)
}
} else {
$invalid_digit!($value, $iter.cursor() + 1, have_any)
Expand Down Expand Up @@ -332,7 +332,9 @@ macro_rules! parse_1digit_unchecked {
Some(v) => v,
None => {
// This optimizes better for success cases, which is what we want.
// It's an odd hack, but it's tested to work.
// It's an odd hack, but it's tested to work. This **HAS**
// to be used like this to get the correct digit count, even
// if we always increment it later.
// SAFETY: Safe since we must have gotten one digit from next.
unsafe { $iter.set_cursor($iter.cursor() - 1) };
on_invalid_digit!(
Expand Down Expand Up @@ -386,7 +388,9 @@ macro_rules! parse_1digit_checked {
Some(v) => v,
None => {
// This optimizes better for success cases, which is what we want.
// It's an odd hack, but it's tested to work.
// It's an odd hack, but it's tested to work. This **HAS**
// to be used like this to get the correct digit count, even
// if we always increment it later.
// SAFETY: Safe since we must have gotten one digit from next.
unsafe { $iter.set_cursor($iter.cursor() - 1) };
on_invalid_digit!(
Expand Down
12 changes: 4 additions & 8 deletions lexical-util/src/format_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ const fn unwrap_or_zero(option: OptionU8) -> u8 {
[`required_fraction_digits_with_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/0cad692/lexical-util/src/format_builder.rs#L1149\n
[`required_mantissa_digits_with_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/47a090d/lexical-util/src/format_builder.rs#L1233\n
[`case_sensitive_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L765\n
[`no_unsigned_negative_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/TODO/lexical-util/src/format_builder.rs#LTODO\n
[`no_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/TODO/lexical-util/src/format_builder.rs#LTODO\n
[`no_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/TODO/lexical-util/src/format_builder.rs#LTODO\n
[`no_unsigned_negative_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/36599c3/lexical-util/src/format_builder.rs#L1640\n
[`no_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/36599c3/lexical-util/src/format_builder.rs#L1671\n
[`no_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/36599c3/lexical-util/src/format_builder.rs#L1696\n
[`start_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/27ca418/lexical-util/src/format_builder.rs#L1650\n
[`integer_sign_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/27ca418/lexical-util/src/format_builder.rs#L1678\n
[`integer_consecutive_sign_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/27ca418/lexical-util/src/format_builder.rs#L1706\n
Expand Down Expand Up @@ -2697,9 +2697,8 @@ impl NumberFormatBuilder {
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Ok(1.0));
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x1", &PF_OPTS), Err(Error::InvalidDigit(2)));
///
/// // TODO: FIXME! This is incorrectly getting the location wrong
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Err(Error::InvalidDigit(2)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"x1", &PI_OPTS), Err(Error::InvalidDigit(0)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"x1", &PI_OPTS), Err(Error::InvalidDigit(1)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Ok(1));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x1", &PI_OPTS), Err(Error::InvalidDigit(2)));
Expand Down Expand Up @@ -3496,7 +3495,6 @@ impl NumberFormatBuilder {
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Ok(1.0));
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1X", &PF_OPTS), Err(Error::InvalidDigit(1)));
///
/// // TODO: This has the wrong placement
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Err(Error::InvalidDigit(2)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Ok(1));
Expand Down Expand Up @@ -4332,7 +4330,6 @@ impl NumberFormatBuilder {
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_2d", &PI_OPTS), Err(Error::InvalidDigit(1)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12_d", &PI_OPTS), Ok(12));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12__d", &PI_OPTS), Err(Error::InvalidDigit(2)));
/// // TODO: This is incorrectly using the current placement
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12d_", &PI_OPTS), Err(Error::InvalidDigit(3)));
/// ```
/// -->
Expand Down Expand Up @@ -4379,7 +4376,6 @@ impl NumberFormatBuilder {
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_2d", &PI_OPTS), Err(Error::InvalidDigit(1)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12_d", &PI_OPTS), Err(Error::InvalidDigit(2)));
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12d_", &PI_OPTS), Ok(12));
/// // TODO: This is getting the location wrong
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12d__", &PI_OPTS), Err(Error::InvalidDigit(4)));
/// ```
/// -->
Expand Down

0 comments on commit a023bef

Please sign in to comment.