Skip to content

Commit

Permalink
bigint: Create the parsing error better for nested +
Browse files Browse the repository at this point in the history
If a `+` is encountered in the middle of parsing a BigUint, this should
generate an `ParseIntError::InvalidDigit`.  Since we can't create that
directly, we get it by trying to parse a `u64` from this point, but of
course `+` is a perfectly valid prefix to a `u64`.

Now we include the previous character in the string passed to `u64`, so
it has proper parsing context to understand what's in error.

Fixes #268.
  • Loading branch information
cuviper committed Mar 6, 2017
1 parent 4b8c71f commit 0b6cae0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bigint/src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ impl Num for BigUint {
v.push(d);
} else {
// create ParseIntError::InvalidDigit
let e = u64::from_str_radix(&s[v.len()..], radix).unwrap_err();
// Include the previous character for context.
let i = cmp::max(v.len(), 1) - 1;
let e = u64::from_str_radix(&s[i..], radix).unwrap_err();
return Err(e.into());
}
}
Expand Down
2 changes: 2 additions & 0 deletions bigint/src/tests/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,8 @@ fn test_from_str_radix() {
assert_eq!(plus_plus_one, None);
let minus_one = BigUint::from_str_radix("-1", 10).ok();
assert_eq!(minus_one, None);
let zero_plus_two = BigUint::from_str_radix("0+2", 10).ok();
assert_eq!(zero_plus_two, None);
}

#[test]
Expand Down

0 comments on commit 0b6cae0

Please sign in to comment.