Skip to content
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

int::from_str("-1") == -253 and other bugs #1102

Closed
mbrubeck opened this issue Oct 30, 2011 · 0 comments
Closed

int::from_str("-1") == -253 and other bugs #1102

mbrubeck opened this issue Oct 30, 2011 · 0 comments

Comments

@mbrubeck
Copy link
Contributor

int::from_str returns bad results for strings that start with "-" signs, because it adjust its character position in the wrong direction. Examples:

assert(int::from_str("-1") == -253);
assert(int::from_str("-3") == -253);
assert(int::from_str("-10") == -2531);
assert(int::from_str("-x") == -253);
assert(int::from_str("-0x") == -2530);

It also returns bad results for any strings with non-numeric characters:

assert(int::from_str("x") == 72);

In addition, int::parse_buf is broken for any radix greater than 10:

assert(int::parse_buf(['a' as u8], 16u) == 49);

Most or all of the same bugs apply to the uint module too.

While we're here, would it be better for these methods to return option::t instead of failing on bad input?

mbrubeck added a commit to mbrubeck/rust that referenced this issue Oct 30, 2011
mbrubeck added a commit to mbrubeck/rust that referenced this issue Oct 31, 2011
Without this fix, int::parse_buf and uint::parse_buf return incorrect results
for any strings that contain non-numeric characters.  Fixes rust-lang#1102.
bjorn3 added a commit to bjorn3/rust that referenced this issue Nov 3, 2020
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants