-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Carry index of InvalidDigit on IntErrorKind #79728
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
r? @KodrAus |
This seems reasonable to me. Maybe we could also update the EDIT: Since we don’t have access to the original buffer just the index would be fine. |
Thinking about this more, it might be better to try keep |
I agree that it would be nice to include the index in the I do not think placing the index as an I don't know what benefit keeping |
@ethanboxx The way I see it, the benefits of carrying the index on
|
Ping from triage: @ethanboxx could you address KodrAus's review? Thanks! |
048b00f
to
6ce70b1
Compare
r? @dtolnay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not immediately sold on this. Can you provide more detail on the use case where you want to use this? Is there code you could show that you would want to be able to write?
Use casesAn example use case would be to print an arrow pointing to the exact location of parsing failure as mentioned in #22639 (comment) where this feature was requested. Another example could be writing a parser that ignores invalid characters. Currently, the way of doing these things would be to rewrite the parsing logic in the local crate to plug in the index logic. Why put this on
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll go ahead and close this, since this API addition doesn't strike me as the most appropriate solution to either of those use cases.
An example use case would be to print an arrow pointing to the exact location of parsing failure as mentioned in #22639 (comment) where this feature was requested.
For parsing, ordinarily you need some variation of a lexer to decide what part of the input you are suppose to parse as a number in the first place. Consider input like {"id":987654321,"username":"ethanboxx"}
. Some piece of logic needs to determine that the substring 987654321
is the correct thing to pass to u32::from_str, which it does by scanning ahead valid digits. Having from_str report an invalid digit position is only useful if you are planning to pass an entire suffix of the input to from_str i.e. 987654321,"username":"ethanboxx"}
, then collect the invalid digit position, and run from_str a second time up to that position. This seems more confusing than just lexing the valid digits yourself up front.
The standard library already exposes the concept of "valid digit" by way of:
u8::is_ascii_digit
/char::is_ascii_digit
(for base 10)u8::is_ascii_hexdigit
/char::is_ascii_hexdigit
(for base 16)char::is_digit
(for arbitrary radix)
Another example could be writing a parser that ignores invalid characters.
Again I don't think an invalid digit position reported by from_str is really appropriate here. It sounds like you are planning to take an input containing a mix of valid and invalid digits, and run from_str on it in a loop, removing one invalid digit at a time. That seems needlessly complicated and confusing compared to filtering for valid digits up front (using the above set of methods) and running from_str a single time on only valid digits.
For |
…yaahc stabilize `int_error_matching` closes rust-lang#22639 > It has been over half a year since rust-lang#77640 (review), and the indexing question is rejected in rust-lang#79728 (review), so I guess we can submit another stabilization attempt? 😉 _Originally posted by `@kennytm` in rust-lang#22639 (comment)
@Lucretiel #22639 (comment)
@ethanboxx #22639 (comment)