@@ -824,34 +824,39 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
824824 radix
825825 ) ;
826826
827- if src. is_empty ( ) {
828- return Err ( PIE { kind : Empty } ) ;
829- }
830-
831827 let is_signed_ty = T :: from_u32 ( 0 ) > T :: min_value ( ) ;
832828
833829 // all valid digits are ascii, so we will just iterate over the utf8 bytes
834830 // and cast them to chars. .to_digit() will safely return None for anything
835831 // other than a valid ascii digit for the given radix, including the first-byte
836832 // of multi-byte sequences
837- let src = src. as_bytes ( ) ;
838-
839- let ( is_positive, digits) = match src[ 0 ] {
840- b'+' | b'-' if src[ 1 ..] . is_empty ( ) => {
841- return Err ( PIE { kind : InvalidDigit } ) ;
833+ let mut src = src. as_bytes ( ) . iter ( ) . enumerate ( ) . peekable ( ) ;
834+
835+ let first_digit = src. peek ( ) . ok_or ( PIE { kind : Empty } ) ?. 1 ;
836+
837+ let ( is_positive, digits) = match first_digit {
838+ b'+' | b'-' => {
839+ src. next ( ) ;
840+ if src. peek ( ) . is_none ( ) {
841+ return Err ( PIE { kind : InvalidDigit ( 0 ) } ) ;
842+ } else {
843+ match first_digit {
844+ b'+' => ( true , src) ,
845+ b'-' if is_signed_ty => ( false , src) ,
846+ _ => return Err ( PIE { kind : InvalidDigit ( 0 ) } ) ,
847+ }
848+ }
842849 }
843- b'+' => ( true , & src[ 1 ..] ) ,
844- b'-' if is_signed_ty => ( false , & src[ 1 ..] ) ,
845850 _ => ( true , src) ,
846851 } ;
847852
848853 let mut result = T :: from_u32 ( 0 ) ;
849854 if is_positive {
850855 // The number is positive
851- for & c in digits {
856+ for ( index , & c ) in digits {
852857 let x = match ( c as char ) . to_digit ( radix) {
853858 Some ( x) => x,
854- None => return Err ( PIE { kind : InvalidDigit } ) ,
859+ None => return Err ( PIE { kind : InvalidDigit ( index ) } ) ,
855860 } ;
856861 result = match result. checked_mul ( radix) {
857862 Some ( result) => result,
@@ -864,10 +869,10 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
864869 }
865870 } else {
866871 // The number is negative
867- for & c in digits {
872+ for ( index , & c ) in digits {
868873 let x = match ( c as char ) . to_digit ( radix) {
869874 Some ( x) => x,
870- None => return Err ( PIE { kind : InvalidDigit } ) ,
875+ None => return Err ( PIE { kind : InvalidDigit ( index ) } ) ,
871876 } ;
872877 result = match result. checked_mul ( radix) {
873878 Some ( result) => result,
0 commit comments