@@ -2472,10 +2472,10 @@ final class JsonReader private[jsoniter_scala](
2472
2472
else nextByte(head)
2473
2473
if (isToken && b == 'n' ) readNullOrNumberError(default, head)
2474
2474
else {
2475
- var isNeg = false
2475
+ var s = 0
2476
2476
if (b == '-' ) {
2477
2477
b = nextByte(head)
2478
- isNeg = true
2478
+ s = - 1
2479
2479
}
2480
2480
if (b < '0' || b > '9' ) numberError()
2481
2481
if (isToken && b == '0' ) {
@@ -2519,7 +2519,7 @@ final class JsonReader private[jsoniter_scala](
2519
2519
if (mark == 0 ) from -= newMark
2520
2520
if (mark > oldMark) mark = oldMark
2521
2521
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1 )
2522
- toBigInt(buf, from, pos, isNeg )
2522
+ toBigInt(buf, from, pos, s )
2523
2523
}
2524
2524
}
2525
2525
}
@@ -2531,10 +2531,10 @@ final class JsonReader private[jsoniter_scala](
2531
2531
else nextByte(head)
2532
2532
if (isToken && b == 'n' ) readNullOrNumberError(default, head)
2533
2533
else {
2534
- var isNeg = false
2534
+ var s = 0
2535
2535
if (b == '-' ) {
2536
2536
b = nextByte(head)
2537
- isNeg = true
2537
+ s = - 1
2538
2538
}
2539
2539
if (b < '0' || b > '9' ) numberError()
2540
2540
var pos = head
@@ -2670,18 +2670,17 @@ final class JsonReader private[jsoniter_scala](
2670
2670
x = x * 10 + (buf(from) - '0' )
2671
2671
from += 1
2672
2672
}
2673
- if (isNeg) x = - x
2674
- java.math.BigDecimal .valueOf(x, scale + fracLen)
2675
- } else toBigDecimal(buf, from, fracLimit, isNeg, scale)
2676
- .add(toBigDecimal(buf, fracPos, limit, isNeg, scale + fracLen))
2677
- } else toBigDecimal(buf, from, from + digits, isNeg, scale)
2673
+ java.math.BigDecimal .valueOf((x ^ s) - s, scale + fracLen)
2674
+ } else toBigDecimal(buf, from, fracLimit, s, scale)
2675
+ .add(toBigDecimal(buf, fracPos, limit, s, scale + fracLen))
2676
+ } else toBigDecimal(buf, from, from + digits, s, scale)
2678
2677
if (mc.getPrecision < digits) x = x.plus(mc)
2679
2678
if (Math .abs(x.scale) >= scaleLimit) scaleLimitError()
2680
2679
new BigDecimal (x, mc)
2681
2680
}
2682
2681
}
2683
2682
2684
- private [this ] def toBigInt (buf : Array [Byte ], p : Int , limit : Int , isNeg : Boolean ): BigInt = {
2683
+ private [this ] def toBigInt (buf : Array [Byte ], p : Int , limit : Int , s : Int ): BigInt = {
2685
2684
val len = limit - p
2686
2685
if (len < 19 ) {
2687
2686
var pos = p
@@ -2691,24 +2690,22 @@ final class JsonReader private[jsoniter_scala](
2691
2690
x = x * 10 + (buf(pos) - '0' )
2692
2691
pos += 1
2693
2692
}
2694
- if (isNeg) x = - x
2695
- BigInt (x)
2696
- } else if (len <= 36 ) toBigInt36(buf, p, limit, isNeg)
2693
+ BigInt ((x ^ s) - s)
2694
+ } else if (len <= 36 ) toBigInt36(buf, p, limit, s)
2697
2695
else new BigInt ({
2698
- if (len <= 308 ) toBigInteger308(buf, p, limit, isNeg )
2696
+ if (len <= 308 ) toBigInteger308(buf, p, limit, s )
2699
2697
else {
2700
2698
// Based on the great idea of Eric Obermühlner to use a tree of smaller BigDecimals for parsing really big numbers
2701
2699
// with O(n^1.5) complexity instead of O(n^2) when using the constructor for the decimal representation from JDK:
2702
2700
// https://github.com/eobermuhlner/big-math/commit/7a5419aac8b2adba2aa700ccf00197f97b2ad89f
2703
2701
val mid = len >> 1
2704
2702
val midPos = limit - mid
2705
- toBigDecimal(buf, p, midPos, isNeg , - mid).add(toBigDecimal(buf, midPos, limit, isNeg , 0 )).unscaledValue()
2703
+ toBigDecimal(buf, p, midPos, s , - mid).add(toBigDecimal(buf, midPos, limit, s , 0 )).unscaledValue()
2706
2704
}
2707
2705
})
2708
2706
}
2709
2707
2710
- private [this ] def toBigDecimal (buf : Array [Byte ], p : Int , limit : Int , isNeg : Boolean ,
2711
- scale : Int ): java.math.BigDecimal = {
2708
+ private [this ] def toBigDecimal (buf : Array [Byte ], p : Int , limit : Int , s : Int , scale : Int ): java.math.BigDecimal = {
2712
2709
val len = limit - p
2713
2710
if (len < 19 ) {
2714
2711
var pos = p
@@ -2718,21 +2715,20 @@ final class JsonReader private[jsoniter_scala](
2718
2715
x = x * 10 + (buf(pos) - '0' )
2719
2716
pos += 1
2720
2717
}
2721
- if (isNeg) x = - x
2722
- java.math.BigDecimal .valueOf(x, scale)
2723
- } else if (len <= 36 ) toBigDecimal36(buf, p, limit, isNeg, scale)
2724
- else if (len <= 308 ) new java.math.BigDecimal (toBigInteger308(buf, p, limit, isNeg), scale)
2718
+ java.math.BigDecimal .valueOf((x ^ s) - s, scale)
2719
+ } else if (len <= 36 ) toBigDecimal36(buf, p, limit, s, scale)
2720
+ else if (len <= 308 ) new java.math.BigDecimal (toBigInteger308(buf, p, limit, s), scale)
2725
2721
else {
2726
2722
// Based on the great idea of Eric Obermühlner to use a tree of smaller BigDecimals for parsing really big numbers
2727
2723
// with O(n^1.5) complexity instead of O(n^2) when using the constructor for the decimal representation from JDK:
2728
2724
// https://github.com/eobermuhlner/big-math/commit/7a5419aac8b2adba2aa700ccf00197f97b2ad89f
2729
2725
val mid = len >> 1
2730
2726
val midPos = limit - mid
2731
- toBigDecimal(buf, p, midPos, isNeg , scale - mid).add(toBigDecimal(buf, midPos, limit, isNeg , scale))
2727
+ toBigDecimal(buf, p, midPos, s , scale - mid).add(toBigDecimal(buf, midPos, limit, s , scale))
2732
2728
}
2733
2729
}
2734
2730
2735
- private [this ] def toBigInt36 (buf : Array [Byte ], p : Int , limit : Int , isNeg : Boolean ): BigInt = {
2731
+ private [this ] def toBigInt36 (buf : Array [Byte ], p : Int , limit : Int , s : Int ): BigInt = {
2736
2732
val firstBlockLimit = limit - 18
2737
2733
var pos = p
2738
2734
var x1 = (buf(pos) - '0' ).toLong
@@ -2749,28 +2745,22 @@ final class JsonReader private[jsoniter_scala](
2749
2745
(dec >> 8 & 0xFF000000FFL) * 42949672960001000L + (dec >> 24 & 0xFF000000FFL) * 429496729600010L >> 32
2750
2746
} + buf(pos + 17 ) - 48000000048L
2751
2747
val q = x1 * 1000000000000000000L
2752
- var l = q + x2
2748
+ val l = q + x2
2753
2749
val h = Math .multiplyHigh(x1, 1000000000000000000L ) + ((~ l & q) >>> 63 )
2754
- if (l >= 0 && h == 0 ) {
2755
- if (isNeg) l = - l
2756
- BigInt (l)
2757
- } else {
2758
- val signum =
2759
- if (isNeg) - 1
2760
- else 1
2750
+ if (l >= 0 && h == 0 ) BigInt ((l ^ s) - s)
2751
+ else {
2761
2752
var magnitude = this .magnitude
2762
2753
if (magnitude eq null ) {
2763
2754
magnitude = new Array [Byte ](128 )
2764
2755
this .magnitude = magnitude
2765
2756
}
2766
2757
ByteArrayAccess .setLongReversed(magnitude, 0 , h)
2767
2758
ByteArrayAccess .setLongReversed(magnitude, 8 , l)
2768
- new BigInt (new java.math.BigInteger (signum , magnitude, 0 , 16 ))
2759
+ new BigInt (new java.math.BigInteger (s | 1 , magnitude, 0 , 16 ))
2769
2760
}
2770
2761
}
2771
2762
2772
- private [this ] def toBigDecimal36 (buf : Array [Byte ], p : Int , limit : Int , isNeg : Boolean ,
2773
- scale : Int ): java.math.BigDecimal = {
2763
+ private [this ] def toBigDecimal36 (buf : Array [Byte ], p : Int , limit : Int , s : Int , scale : Int ): java.math.BigDecimal = {
2774
2764
val firstBlockLimit = limit - 18
2775
2765
var pos = p
2776
2766
var x1 = (buf(pos) - '0' ).toLong
@@ -2787,27 +2777,22 @@ final class JsonReader private[jsoniter_scala](
2787
2777
(dec >> 8 & 0xFF000000FFL) * 42949672960001000L + (dec >> 24 & 0xFF000000FFL) * 429496729600010L >> 32
2788
2778
} + buf(pos + 17 ) - 48000000048L
2789
2779
val q = x1 * 1000000000000000000L
2790
- var l = q + x2
2780
+ val l = q + x2
2791
2781
val h = Math .multiplyHigh(x1, 1000000000000000000L ) + ((~ l & q) >>> 63 )
2792
- if (l >= 0 && h == 0 ) {
2793
- if (isNeg) l = - l
2794
- java.math.BigDecimal .valueOf(l, scale)
2795
- } else {
2796
- val signum =
2797
- if (isNeg) - 1
2798
- else 1
2782
+ if (l >= 0 && h == 0 ) java.math.BigDecimal .valueOf((l ^ s) - s, scale)
2783
+ else {
2799
2784
var magnitude = this .magnitude
2800
2785
if (magnitude eq null ) {
2801
2786
magnitude = new Array [Byte ](128 )
2802
2787
this .magnitude = magnitude
2803
2788
}
2804
2789
ByteArrayAccess .setLongReversed(magnitude, 0 , h)
2805
2790
ByteArrayAccess .setLongReversed(magnitude, 8 , l)
2806
- new java.math.BigDecimal (new java.math.BigInteger (signum , magnitude, 0 , 16 ), scale)
2791
+ new java.math.BigDecimal (new java.math.BigInteger (s | 1 , magnitude, 0 , 16 ), scale)
2807
2792
}
2808
2793
}
2809
2794
2810
- private [this ] def toBigInteger308 (buf : Array [Byte ], p : Int , limit : Int , isNeg : Boolean ): java.math.BigInteger = {
2795
+ private [this ] def toBigInteger308 (buf : Array [Byte ], p : Int , limit : Int , s : Int ): java.math.BigInteger = {
2811
2796
val len = limit - p
2812
2797
val last = (len * 222930821L >> 32 ).toInt << 3 // (len * Math.log(10) / Math.log(1L << 64)).toInt * 8
2813
2798
var magnitude = this .magnitude
@@ -2861,10 +2846,7 @@ final class JsonReader private[jsoniter_scala](
2861
2846
ByteArrayAccess .setLongReversed(magnitude, i, ByteArrayAccess .getLong(magnitude, i))
2862
2847
i += 8
2863
2848
}
2864
- val signum =
2865
- if (isNeg) - 1
2866
- else 1
2867
- new java.math.BigInteger (signum, magnitude, 0 , last + 8 )
2849
+ new java.math.BigInteger (s | 1 , magnitude, 0 , last + 8 )
2868
2850
}
2869
2851
2870
2852
@ tailrec
0 commit comments