Skip to content

Commit 64c4b51

Browse files
committed
Auto merge of #28826 - arthurprs:leading_plus, r=alexcrichton
Closes #27580
2 parents 95285c4 + 123a833 commit 64c4b51

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

src/libcore/num/mod.rs

+41-40
Original file line numberDiff line numberDiff line change
@@ -1387,50 +1387,51 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
13871387
// of multi-byte sequences
13881388
let src = src.as_bytes();
13891389

1390-
match (src[0], &src[1..]) {
1391-
(b'-', digits) if digits.is_empty() => Err(PIE { kind: Empty }),
1392-
(b'-', digits) if is_signed_ty => {
1393-
// The number is negative
1394-
let mut result = T::from_u32(0);
1395-
for &c in digits {
1396-
let x = match (c as char).to_digit(radix) {
1397-
Some(x) => x,
1398-
None => return Err(PIE { kind: InvalidDigit }),
1399-
};
1400-
result = match result.checked_mul(radix) {
1401-
Some(result) => result,
1402-
None => return Err(PIE { kind: Underflow }),
1403-
};
1404-
result = match result.checked_sub(x) {
1405-
Some(result) => result,
1406-
None => return Err(PIE { kind: Underflow }),
1407-
};
1408-
}
1409-
Ok(result)
1410-
},
1411-
(c, digits) => {
1412-
// The number is signed
1413-
let mut result = match (c as char).to_digit(radix) {
1414-
Some(x) => T::from_u32(x),
1390+
let (is_positive, digits) = match src[0] {
1391+
b'+' => (true, &src[1..]),
1392+
b'-' if is_signed_ty => (false, &src[1..]),
1393+
_ => (true, src)
1394+
};
1395+
1396+
if digits.is_empty() {
1397+
return Err(PIE { kind: Empty });
1398+
}
1399+
1400+
let mut result = T::from_u32(0);
1401+
if is_positive {
1402+
// The number is positive
1403+
for &c in digits {
1404+
let x = match (c as char).to_digit(radix) {
1405+
Some(x) => x,
14151406
None => return Err(PIE { kind: InvalidDigit }),
14161407
};
1417-
for &c in digits {
1418-
let x = match (c as char).to_digit(radix) {
1419-
Some(x) => x,
1420-
None => return Err(PIE { kind: InvalidDigit }),
1421-
};
1422-
result = match result.checked_mul(radix) {
1423-
Some(result) => result,
1424-
None => return Err(PIE { kind: Overflow }),
1425-
};
1426-
result = match result.checked_add(x) {
1427-
Some(result) => result,
1428-
None => return Err(PIE { kind: Overflow }),
1429-
};
1430-
}
1431-
Ok(result)
1408+
result = match result.checked_mul(radix) {
1409+
Some(result) => result,
1410+
None => return Err(PIE { kind: Overflow }),
1411+
};
1412+
result = match result.checked_add(x) {
1413+
Some(result) => result,
1414+
None => return Err(PIE { kind: Overflow }),
1415+
};
1416+
}
1417+
} else {
1418+
// The number is negative
1419+
for &c in digits {
1420+
let x = match (c as char).to_digit(radix) {
1421+
Some(x) => x,
1422+
None => return Err(PIE { kind: InvalidDigit }),
1423+
};
1424+
result = match result.checked_mul(radix) {
1425+
Some(result) => result,
1426+
None => return Err(PIE { kind: Underflow }),
1427+
};
1428+
result = match result.checked_sub(x) {
1429+
Some(result) => result,
1430+
None => return Err(PIE { kind: Underflow }),
1431+
};
14321432
}
14331433
}
1434+
Ok(result)
14341435
}
14351436

14361437
/// An error which can be returned when parsing an integer.

src/libcoretest/num/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,23 @@ mod tests {
118118
assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
119119
}
120120

121+
#[test]
122+
fn test_leading_plus() {
123+
assert_eq!("+127".parse::<u8>().ok(), Some(127u8));
124+
assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807i64));
125+
}
126+
121127
#[test]
122128
fn test_invalid() {
123129
assert_eq!("--129".parse::<i8>().ok(), None);
130+
assert_eq!("++129".parse::<i8>().ok(), None);
124131
assert_eq!("Съешь".parse::<u8>().ok(), None);
125132
}
126133

127134
#[test]
128135
fn test_empty() {
129136
assert_eq!("-".parse::<i8>().ok(), None);
137+
assert_eq!("+".parse::<i8>().ok(), None);
130138
assert_eq!("".parse::<u8>().ok(), None);
131139
}
132140
}

0 commit comments

Comments
 (0)