From 4b8c71fbec13c21dfd2d8cadb63f45c7c1646a2c Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 6 Mar 2017 13:20:30 -0800 Subject: [PATCH 1/2] bigint: Cleanup unused imports --- bigint/src/bigint.rs | 3 +-- bigint/src/biguint.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/bigint/src/bigint.rs b/bigint/src/bigint.rs index 7a9b02d..32a4441 100644 --- a/bigint/src/bigint.rs +++ b/bigint/src/bigint.rs @@ -3,8 +3,7 @@ use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub}; use std::str::{self, FromStr}; use std::fmt; use std::cmp::Ordering::{self, Less, Greater, Equal}; -use std::{f32, f64}; -use std::{u8, i64, u64}; +use std::{i64, u64}; use std::ascii::AsciiExt; #[cfg(feature = "serde")] diff --git a/bigint/src/biguint.rs b/bigint/src/biguint.rs index 5248115..fe6303e 100644 --- a/bigint/src/biguint.rs +++ b/bigint/src/biguint.rs @@ -7,7 +7,7 @@ use std::fmt; use std::cmp; use std::cmp::Ordering::{self, Less, Greater, Equal}; use std::{f32, f64}; -use std::{u8, i64, u64}; +use std::{u8, u64}; use std::ascii::AsciiExt; #[cfg(feature = "serde")] From 0b6cae0dc77635cc52935ecf112fc45db0beae24 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 6 Mar 2017 13:23:40 -0800 Subject: [PATCH 2/2] bigint: Create the parsing error better for nested `+` 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. --- bigint/src/biguint.rs | 4 +++- bigint/src/tests/biguint.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/bigint/src/biguint.rs b/bigint/src/biguint.rs index fe6303e..00877c8 100644 --- a/bigint/src/biguint.rs +++ b/bigint/src/biguint.rs @@ -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()); } } diff --git a/bigint/src/tests/biguint.rs b/bigint/src/tests/biguint.rs index 87a223f..01dec42 100644 --- a/bigint/src/tests/biguint.rs +++ b/bigint/src/tests/biguint.rs @@ -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]