1
Fork 0

optimize from_str_radix

This commit is contained in:
arthurprs 2015-07-18 18:59:38 -03:00
parent ebf9e1aaf6
commit c073f81920
2 changed files with 34 additions and 13 deletions

View file

@ -24,6 +24,7 @@ use mem::size_of;
use option::Option::{self, Some, None}; use option::Option::{self, Some, None};
use result::Result::{self, Ok, Err}; use result::Result::{self, Ok, Err};
use str::{FromStr, StrExt}; use str::{FromStr, StrExt};
use slice::SliceExt;
/// Provides intentionally-wrapped arithmetic on `T`. /// Provides intentionally-wrapped arithmetic on `T`.
/// ///
@ -1448,19 +1449,30 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
-> Result<T, ParseIntError> { -> Result<T, ParseIntError> {
use self::IntErrorKind::*; use self::IntErrorKind::*;
use self::ParseIntError as PIE; use self::ParseIntError as PIE;
assert!(radix >= 2 && radix <= 36, assert!(radix >= 2 && radix <= 36,
"from_str_radix_int: must lie in the range `[2, 36]` - found {}", "from_str_radix_int: must lie in the range `[2, 36]` - found {}",
radix); radix);
if src.is_empty() {
return Err(PIE { kind: Empty });
}
let is_signed_ty = T::from_u32(0) > T::min_value(); let is_signed_ty = T::from_u32(0) > T::min_value();
match src.slice_shift_char() { // all valid digits are ascii, so we will just iterate over the utf8 bytes
Some(('-', "")) => Err(PIE { kind: Empty }), // and cast them to chars. .to_digit() will safely return None for anything
Some(('-', src)) if is_signed_ty => { // other than a valid ascii digit for a the given radix, including the first-byte
// of multi-byte sequences
let src = src.as_bytes();
match (src[0], &src[1..]) {
(b'-', digits) if digits.is_empty() => Err(PIE { kind: Empty }),
(b'-', digits) if is_signed_ty => {
// The number is negative // The number is negative
let mut result = T::from_u32(0); let mut result = T::from_u32(0);
for c in src.chars() { for &c in digits {
let x = match c.to_digit(radix) { let x = match (c as char).to_digit(radix) {
Some(x) => x, Some(x) => x,
None => return Err(PIE { kind: InvalidDigit }), None => return Err(PIE { kind: InvalidDigit }),
}; };
@ -1475,11 +1487,14 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
} }
Ok(result) Ok(result)
}, },
Some((_, _)) => { (c, digits) => {
// The number is signed // The number is signed
let mut result = T::from_u32(0); let mut result = match (c as char).to_digit(radix) {
for c in src.chars() { Some(x) => T::from_u32(x),
let x = match c.to_digit(radix) { None => return Err(PIE { kind: InvalidDigit }),
};
for &c in digits {
let x = match (c as char).to_digit(radix) {
Some(x) => x, Some(x) => x,
None => return Err(PIE { kind: InvalidDigit }), None => return Err(PIE { kind: InvalidDigit }),
}; };
@ -1493,8 +1508,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
}; };
} }
Ok(result) Ok(result)
}, }
None => Err(ParseIntError { kind: Empty }),
} }
} }

View file

@ -117,7 +117,14 @@ mod tests {
} }
#[test] #[test]
fn test_int_from_minus_sign() { fn test_invalid() {
assert_eq!("-".parse::<i32>().ok(), None); assert_eq!("--129".parse::<i8>().ok(), None);
assert_eq!("Съешь".parse::<u8>().ok(), None);
}
#[test]
fn test_empty() {
assert_eq!("-".parse::<i8>().ok(), None);
assert_eq!("".parse::<u8>().ok(), None);
} }
} }