Skip to content

Commit a023bef

Browse files
committed
Add minor patches for the correct indexes on format errors in parsing integers.
1 parent 1b955e2 commit a023bef

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lexical-parse-integer/src/algorithm.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ macro_rules! on_invalid_digit {
138138
if $iter.is_buffer_empty() && $is_end {
139139
$into_ok!($value, $iter.buffer_length(), have_any)
140140
} else {
141-
$invalid_digit!($value, $iter.cursor(), have_any)
141+
$invalid_digit!($value, $iter.cursor() + 1, have_any)
142142
}
143143
} else {
144144
$invalid_digit!($value, $iter.cursor() + 1, have_any)
@@ -332,7 +332,9 @@ macro_rules! parse_1digit_unchecked {
332332
Some(v) => v,
333333
None => {
334334
// This optimizes better for success cases, which is what we want.
335-
// It's an odd hack, but it's tested to work.
335+
// It's an odd hack, but it's tested to work. This **HAS**
336+
// to be used like this to get the correct digit count, even
337+
// if we always increment it later.
336338
// SAFETY: Safe since we must have gotten one digit from next.
337339
unsafe { $iter.set_cursor($iter.cursor() - 1) };
338340
on_invalid_digit!(
@@ -386,7 +388,9 @@ macro_rules! parse_1digit_checked {
386388
Some(v) => v,
387389
None => {
388390
// This optimizes better for success cases, which is what we want.
389-
// It's an odd hack, but it's tested to work.
391+
// It's an odd hack, but it's tested to work. This **HAS**
392+
// to be used like this to get the correct digit count, even
393+
// if we always increment it later.
390394
// SAFETY: Safe since we must have gotten one digit from next.
391395
unsafe { $iter.set_cursor($iter.cursor() - 1) };
392396
on_invalid_digit!(

lexical-util/src/format_builder.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ const fn unwrap_or_zero(option: OptionU8) -> u8 {
480480
[`required_fraction_digits_with_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/0cad692/lexical-util/src/format_builder.rs#L1149\n
481481
[`required_mantissa_digits_with_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/47a090d/lexical-util/src/format_builder.rs#L1233\n
482482
[`case_sensitive_exponent`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/format_builder.rs#L765\n
483-
[`no_unsigned_negative_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/TODO/lexical-util/src/format_builder.rs#LTODO\n
484-
[`no_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/TODO/lexical-util/src/format_builder.rs#LTODO\n
485-
[`no_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/TODO/lexical-util/src/format_builder.rs#LTODO\n
483+
[`no_unsigned_negative_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/36599c3/lexical-util/src/format_builder.rs#L1640\n
484+
[`no_mantissa_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/36599c3/lexical-util/src/format_builder.rs#L1671\n
485+
[`no_exponent_sign`]: https://github.com/Alexhuszagh/rust-lexical/blob/36599c3/lexical-util/src/format_builder.rs#L1696\n
486486
[`start_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/27ca418/lexical-util/src/format_builder.rs#L1650\n
487487
[`integer_sign_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/27ca418/lexical-util/src/format_builder.rs#L1678\n
488488
[`integer_consecutive_sign_digit_separator`]: https://github.com/Alexhuszagh/rust-lexical/blob/27ca418/lexical-util/src/format_builder.rs#L1706\n
@@ -2697,9 +2697,8 @@ impl NumberFormatBuilder {
26972697
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Ok(1.0));
26982698
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x1", &PF_OPTS), Err(Error::InvalidDigit(2)));
26992699
///
2700-
/// // TODO: FIXME! This is incorrectly getting the location wrong
27012700
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Err(Error::InvalidDigit(2)));
2702-
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"x1", &PI_OPTS), Err(Error::InvalidDigit(0)));
2701+
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"x1", &PI_OPTS), Err(Error::InvalidDigit(1)));
27032702
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1));
27042703
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Ok(1));
27052704
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x1", &PI_OPTS), Err(Error::InvalidDigit(2)));
@@ -3496,7 +3495,6 @@ impl NumberFormatBuilder {
34963495
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1x", &PF_OPTS), Ok(1.0));
34973496
/// assert_eq!(parse_with_options::<f64, FORMAT>(b"1X", &PF_OPTS), Err(Error::InvalidDigit(1)));
34983497
///
3499-
/// // TODO: This has the wrong placement
35003498
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"0x1", &PI_OPTS), Err(Error::InvalidDigit(2)));
35013499
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1", &PI_OPTS), Ok(1));
35023500
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1x", &PI_OPTS), Ok(1));
@@ -4332,7 +4330,6 @@ impl NumberFormatBuilder {
43324330
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_2d", &PI_OPTS), Err(Error::InvalidDigit(1)));
43334331
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12_d", &PI_OPTS), Ok(12));
43344332
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12__d", &PI_OPTS), Err(Error::InvalidDigit(2)));
4335-
/// // TODO: This is incorrectly using the current placement
43364333
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12d_", &PI_OPTS), Err(Error::InvalidDigit(3)));
43374334
/// ```
43384335
/// -->
@@ -4379,7 +4376,6 @@ impl NumberFormatBuilder {
43794376
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"1_2d", &PI_OPTS), Err(Error::InvalidDigit(1)));
43804377
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12_d", &PI_OPTS), Err(Error::InvalidDigit(2)));
43814378
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12d_", &PI_OPTS), Ok(12));
4382-
/// // TODO: This is getting the location wrong
43834379
/// assert_eq!(parse_with_options::<i64, FORMAT>(b"12d__", &PI_OPTS), Err(Error::InvalidDigit(4)));
43844380
/// ```
43854381
/// -->

0 commit comments

Comments
 (0)