@@ -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.
0 commit comments