@@ -237,8 +237,7 @@ macro_rules! expect_number {
237237 match ch {
238238 b'0' ... b'9' => {
239239 $parser. bump( ) ;
240- // Avoid multiplication with bitshifts and addition
241- num = ( num << 1 ) + ( num << 3 ) + ( ch - b'0' ) as u64 ;
240+ num = num * 10 + ( ch - b'0' ) as u64 ;
242241 } ,
243242 _ => {
244243 let mut e = 0 ;
@@ -288,9 +287,31 @@ macro_rules! expect_fraction {
288287 ( $parser: ident, $num: ident, $e: ident) => ( {
289288 let result: Number ;
290289
290+ let ch = expect_byte!( $parser) ;
291+
292+ match ch {
293+ b'0' ... b'9' => {
294+ if $num < MAX_PRECISION {
295+ $num = $num * 10 + ( ch - b'0' ) as u64 ;
296+ $e -= 1 ;
297+ } else {
298+ match $num. checked_mul( 10 ) . and_then( |num| {
299+ num. checked_add( ( ch - b'0' ) as u64 )
300+ } ) {
301+ Some ( result) => {
302+ $num = result;
303+ $e -= 1 ;
304+ } ,
305+ None => { }
306+ }
307+ }
308+ } ,
309+ _ => return $parser. unexpected_character( ch)
310+ }
311+
291312 loop {
292313 if $parser. is_eof( ) {
293- result = Number :: from_parts( true , $num, $e as i16 ) ;
314+ result = Number :: from_parts( true , $num, $e) ;
294315 break ;
295316 }
296317 let ch = $parser. read_byte( ) ;
@@ -299,7 +320,7 @@ macro_rules! expect_fraction {
299320 b'0' ... b'9' => {
300321 $parser. bump( ) ;
301322 if $num < MAX_PRECISION {
302- $num = ( $num << 3 ) + ( $num << 1 ) + ( ch - b'0' ) as u64 ;
323+ $num = $num * 10 + ( ch - b'0' ) as u64 ;
303324 $e -= 1 ;
304325 } else {
305326 match $num. checked_mul( 10 ) . and_then( |num| {
@@ -319,7 +340,7 @@ macro_rules! expect_fraction {
319340 break ;
320341 }
321342 _ => {
322- result = Number :: from_parts( true , $num, $e as i16 ) ;
343+ result = Number :: from_parts( true , $num, $e) ;
323344 break ;
324345 }
325346 }
@@ -614,10 +635,10 @@ impl<'a> Parser<'a> {
614635 // the exponent. Note that no digits are actually read here, as we already
615636 // exceeded the precision range of f64 anyway.
616637 fn read_big_number ( & mut self , mut num : u64 ) -> Result < Number > {
617- let mut e = 0i32 ;
638+ let mut e = 0i16 ;
618639 loop {
619640 if self . is_eof ( ) {
620- return Ok ( Number :: from_parts ( true , num, e as i16 ) ) ;
641+ return Ok ( Number :: from_parts ( true , num, e) ) ;
621642 }
622643 let ch = self . read_byte ( ) ;
623644 match ch {
@@ -642,12 +663,12 @@ impl<'a> Parser<'a> {
642663 }
643664 }
644665
645- Ok ( Number :: from_parts ( true , num, e as i16 ) )
666+ Ok ( Number :: from_parts ( true , num, e) )
646667 }
647668
648669 // Called in the rare case that a number with `e` notation has been
649670 // encountered. This is pretty straight forward, I guess.
650- fn expect_exponent ( & mut self , num : u64 , big_e : i32 ) -> Result < Number > {
671+ fn expect_exponent ( & mut self , num : u64 , big_e : i16 ) -> Result < Number > {
651672 let mut ch = expect_byte ! ( self ) ;
652673 let sign = match ch {
653674 b'-' => {
@@ -662,7 +683,7 @@ impl<'a> Parser<'a> {
662683 } ;
663684
664685 let mut e = match ch {
665- b'0' ... b'9' => ( ch - b'0' ) as i32 ,
686+ b'0' ... b'9' => ( ch - b'0' ) as i16 ,
666687 _ => return self . unexpected_character ( ch) ,
667688 } ;
668689
@@ -674,13 +695,13 @@ impl<'a> Parser<'a> {
674695 match ch {
675696 b'0' ... b'9' => {
676697 self . bump ( ) ;
677- e = ( e << 3 ) + ( e << 1 ) + ( ch - b'0' ) as i32 ;
698+ e = e . saturating_mul ( 10 ) . saturating_add ( ( ch - b'0' ) as i16 ) ;
678699 } ,
679700 _ => break
680701 }
681702 }
682703
683- Ok ( Number :: from_parts ( true , num, ( big_e + ( e * sign) ) as i16 ) )
704+ Ok ( Number :: from_parts ( true , num, ( big_e. saturating_add ( e * sign) ) ) )
684705 }
685706
686707 // Given how compilcated reading numbers and strings is, reading objects
0 commit comments