Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upLeading plus for string to integer parsing #27580
Comments
This comment has been minimized.
This comment has been minimized.
|
Need to decide if this is intentional or incidental. CC @arthurprs who wrote the current implementation. |
Gankro
added
A-libs
I-needs-decision
P-low
T-libs
labels
Aug 7, 2015
This comment has been minimized.
This comment has been minimized.
|
Interesting. I'm 99% sure this was the behavior before my change though. https://github.com/rust-lang/rust/pull/27110/files#diff-01076f91a26400b2db49663d787c2576L1457 |
This comment has been minimized.
This comment has been minimized.
|
@arthurprs I agree, I just figured you might have an opinion. |
This comment has been minimized.
This comment has been minimized.
|
@Gankro since all major languages accept the leading + we should probably do the same. |
This comment has been minimized.
This comment has been minimized.
|
Bump: We should absolutely support leading +. It is super annoying to work around this when you're parsing something that permits leading +. I already encountered this twice in the last months, and I don't write a lot of Rust code to begin with. |
arthurprs
referenced this issue
Oct 3, 2015
Merged
Integer parsing should accept leading plus #28826
This comment has been minimized.
This comment has been minimized.
|
If you add support for a bunch of additional features then people end up needing to write wrapper functions around the implementation to stop those features. For example, I often end up with code that is similar to the following because I don't want to accept nonconforming input:
This is not to mention the fact that I bet in the future that a LOT of people will be sloppy and not take care of corner cases and then their parsers will end up accepting nonconforming input for whatever program they write and so gratuitous incompatibilities and extra features between programs will proliferate. I feel that is a net harm. |
This comment has been minimized.
This comment has been minimized.
|
@sstewartgallus This is an interesting perspective, thank you! However it has been noted elsewhere (#28826) that the float parsing algorithm already does this, which suggests we should probably bite the bullet and do this just to be consistent. |
This comment has been minimized.
This comment has been minimized.
|
Just because current nightly and beta accept leading plus on floats doesn't mean a decision has been made. Like I said, this was probably unconscious, it slipped through the cracks so to speak. If desired, I can easily alter the code to reject leading plus. It would make me sad, because I think it ought to be accepted, but it would be be a one-minute fix and could be backported with little danger. @sstewartgallus In broad strokes, you are right and this is a real concern. On the other hand, there are also formats where leading plus is legal (for example, the exponents in floating point: Additionally, it's not like all occurrences of integers in fixed data formats are floating around in a vacuum. Often some code has to identify the substrings containing them, then pass those substrings to In total, I don't think in this instance there is a significant hazard one way or another, it's simply a matter of consistency with other languages and doing the thing that most users find convenient. |
Cigaes commentedAug 7, 2015
The
parse::<i32>()function (and all its cousins for other integer types) fail for strings starting with a+withParseIntError { kind: InvalidDigit }. Failing snippet:The same error occurs with the symmetric function call
i32::from_str("+42").Test snippet on Rust Playground
Rust Forum discussion