Skip to content

Commit 559439e

Browse files
committed
---
yaml --- r: 220883 b: refs/heads/auto c: c073f81 h: refs/heads/master i: 220881: 6a5a2f3 220879: d82e941 v: v3
1 parent 40842c3 commit 559439e

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
88
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
99
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1010
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11-
refs/heads/auto: ebf9e1aaf63e741cec122f3cea1afa6d62b01350
11+
refs/heads/auto: c073f81920bc2324b026cf9610a096df0d97c6a5
1212
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1313
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
1414
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1

branches/auto/src/libcore/num/mod.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use mem::size_of;
2424
use option::Option::{self, Some, None};
2525
use result::Result::{self, Ok, Err};
2626
use str::{FromStr, StrExt};
27+
use slice::SliceExt;
2728

2829
/// Provides intentionally-wrapped arithmetic on `T`.
2930
///
@@ -1448,19 +1449,30 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
14481449
-> Result<T, ParseIntError> {
14491450
use self::IntErrorKind::*;
14501451
use self::ParseIntError as PIE;
1452+
14511453
assert!(radix >= 2 && radix <= 36,
14521454
"from_str_radix_int: must lie in the range `[2, 36]` - found {}",
14531455
radix);
14541456

1457+
if src.is_empty() {
1458+
return Err(PIE { kind: Empty });
1459+
}
1460+
14551461
let is_signed_ty = T::from_u32(0) > T::min_value();
14561462

1457-
match src.slice_shift_char() {
1458-
Some(('-', "")) => Err(PIE { kind: Empty }),
1459-
Some(('-', src)) if is_signed_ty => {
1463+
// all valid digits are ascii, so we will just iterate over the utf8 bytes
1464+
// and cast them to chars. .to_digit() will safely return None for anything
1465+
// other than a valid ascii digit for a the given radix, including the first-byte
1466+
// of multi-byte sequences
1467+
let src = src.as_bytes();
1468+
1469+
match (src[0], &src[1..]) {
1470+
(b'-', digits) if digits.is_empty() => Err(PIE { kind: Empty }),
1471+
(b'-', digits) if is_signed_ty => {
14601472
// The number is negative
14611473
let mut result = T::from_u32(0);
1462-
for c in src.chars() {
1463-
let x = match c.to_digit(radix) {
1474+
for &c in digits {
1475+
let x = match (c as char).to_digit(radix) {
14641476
Some(x) => x,
14651477
None => return Err(PIE { kind: InvalidDigit }),
14661478
};
@@ -1475,11 +1487,14 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
14751487
}
14761488
Ok(result)
14771489
},
1478-
Some((_, _)) => {
1490+
(c, digits) => {
14791491
// The number is signed
1480-
let mut result = T::from_u32(0);
1481-
for c in src.chars() {
1482-
let x = match c.to_digit(radix) {
1492+
let mut result = match (c as char).to_digit(radix) {
1493+
Some(x) => T::from_u32(x),
1494+
None => return Err(PIE { kind: InvalidDigit }),
1495+
};
1496+
for &c in digits {
1497+
let x = match (c as char).to_digit(radix) {
14831498
Some(x) => x,
14841499
None => return Err(PIE { kind: InvalidDigit }),
14851500
};
@@ -1493,8 +1508,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
14931508
};
14941509
}
14951510
Ok(result)
1496-
},
1497-
None => Err(ParseIntError { kind: Empty }),
1511+
}
14981512
}
14991513
}
15001514

branches/auto/src/libcoretest/num/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ mod tests {
117117
}
118118

119119
#[test]
120-
fn test_int_from_minus_sign() {
121-
assert_eq!("-".parse::<i32>().ok(), None);
120+
fn test_invalid() {
121+
assert_eq!("--129".parse::<i8>().ok(), None);
122+
assert_eq!("Съешь".parse::<u8>().ok(), None);
123+
}
124+
125+
#[test]
126+
fn test_empty() {
127+
assert_eq!("-".parse::<i8>().ok(), None);
128+
assert_eq!("".parse::<u8>().ok(), None);
122129
}
123130
}

0 commit comments

Comments
 (0)