@@ -237,8 +237,7 @@ macro_rules! expect_number {
237
237
match ch {
238
238
b'0' ... b'9' => {
239
239
$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 ;
242
241
} ,
243
242
_ => {
244
243
let mut e = 0 ;
@@ -288,9 +287,31 @@ macro_rules! expect_fraction {
288
287
( $parser: ident, $num: ident, $e: ident) => ( {
289
288
let result: Number ;
290
289
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
+
291
312
loop {
292
313
if $parser. is_eof( ) {
293
- result = Number :: from_parts( true , $num, $e as i16 ) ;
314
+ result = Number :: from_parts( true , $num, $e) ;
294
315
break ;
295
316
}
296
317
let ch = $parser. read_byte( ) ;
@@ -299,7 +320,7 @@ macro_rules! expect_fraction {
299
320
b'0' ... b'9' => {
300
321
$parser. bump( ) ;
301
322
if $num < MAX_PRECISION {
302
- $num = ( $num << 3 ) + ( $num << 1 ) + ( ch - b'0' ) as u64 ;
323
+ $num = $num * 10 + ( ch - b'0' ) as u64 ;
303
324
$e -= 1 ;
304
325
} else {
305
326
match $num. checked_mul( 10 ) . and_then( |num| {
@@ -319,7 +340,7 @@ macro_rules! expect_fraction {
319
340
break ;
320
341
}
321
342
_ => {
322
- result = Number :: from_parts( true , $num, $e as i16 ) ;
343
+ result = Number :: from_parts( true , $num, $e) ;
323
344
break ;
324
345
}
325
346
}
@@ -614,10 +635,10 @@ impl<'a> Parser<'a> {
614
635
// the exponent. Note that no digits are actually read here, as we already
615
636
// exceeded the precision range of f64 anyway.
616
637
fn read_big_number ( & mut self , mut num : u64 ) -> Result < Number > {
617
- let mut e = 0i32 ;
638
+ let mut e = 0i16 ;
618
639
loop {
619
640
if self . is_eof ( ) {
620
- return Ok ( Number :: from_parts ( true , num, e as i16 ) ) ;
641
+ return Ok ( Number :: from_parts ( true , num, e) ) ;
621
642
}
622
643
let ch = self . read_byte ( ) ;
623
644
match ch {
@@ -642,12 +663,12 @@ impl<'a> Parser<'a> {
642
663
}
643
664
}
644
665
645
- Ok ( Number :: from_parts ( true , num, e as i16 ) )
666
+ Ok ( Number :: from_parts ( true , num, e) )
646
667
}
647
668
648
669
// Called in the rare case that a number with `e` notation has been
649
670
// 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 > {
651
672
let mut ch = expect_byte ! ( self ) ;
652
673
let sign = match ch {
653
674
b'-' => {
@@ -662,7 +683,7 @@ impl<'a> Parser<'a> {
662
683
} ;
663
684
664
685
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 ,
666
687
_ => return self . unexpected_character ( ch) ,
667
688
} ;
668
689
@@ -674,13 +695,13 @@ impl<'a> Parser<'a> {
674
695
match ch {
675
696
b'0' ... b'9' => {
676
697
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 ) ;
678
699
} ,
679
700
_ => break
680
701
}
681
702
}
682
703
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) ) ) )
684
705
}
685
706
686
707
// Given how compilcated reading numbers and strings is, reading objects
0 commit comments