Skip to content

Commit 7998ff9

Browse files
committed
---
yaml --- r: 235572 b: refs/heads/stable c: c073f81 h: refs/heads/master v: v3
1 parent aa2541d commit 7998ff9

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
@@ -29,7 +29,7 @@ refs/heads/tmp: afae2ff723393b3ab4ccffef6ac7c6d1809e2da0
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: f859507de8c410b648d934d8f5ec1c52daac971d
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: ebf9e1aaf63e741cec122f3cea1afa6d62b01350
32+
refs/heads/stable: c073f81920bc2324b026cf9610a096df0d97c6a5
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b
3535
refs/tags/1.2.0: f557861f822c34f07270347b94b5280de20a597e

branches/stable/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/stable/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)