@@ -269,8 +269,8 @@ function applyobject(keyvalfunc, x::LazyValues)
269
269
@nextbyte
270
270
b == UInt8 (' }' ) && return pos + 1
271
271
while true
272
- # applystring returns key as a PtrString
273
- key, pos = @inline applystring ( nothing , LazyValue (buf, pos, JSONTypes. STRING, getopts (x), false ))
272
+ # parsestring returns key as a PtrString
273
+ key, pos = @inline parsestring ( LazyValue (buf, pos, JSONTypes. STRING, getopts (x), false ))
274
274
@nextbyte
275
275
if b != UInt8 (' :' )
276
276
error = ExpectedColon
@@ -455,7 +455,7 @@ StructUtils.keyeq(x::PtrString, y::Symbol) = convert(Symbol, x) == y
455
455
# or not. It allows materialize, _binary, etc. to deal
456
456
# with the string data appropriately without forcing a String allocation
457
457
# PtrString should NEVER be visible to users though!
458
- function applystring (f, x:: LazyValue )
458
+ function parsestring ( x:: LazyValue )
459
459
buf, pos = getbuf (x), getpos (x)
460
460
len, b = getlength (buf), getbyte (buf, pos)
461
461
if b != UInt8 (' "' )
@@ -483,12 +483,7 @@ function applystring(f, x::LazyValue)
483
483
@nextbyte (false )
484
484
end
485
485
str = PtrString (pointer (buf, spos), pos - spos, escaped)
486
- if f === nothing
487
- return str, pos + 1
488
- else
489
- f (str)
490
- return pos + 1
491
- end
486
+ return str, pos + 1
492
487
493
488
@label invalid
494
489
invalid (error, buf, pos, " string" )
@@ -525,7 +520,30 @@ macro check_special(special, value)
525
520
end )
526
521
end
527
522
528
- function applynumber (valfunc, x:: LazyValue )
523
+ const INT = 0x00
524
+ const FLOAT = 0x01
525
+ const BIGINT = 0x02
526
+ const BIGFLOAT = 0x03
527
+ const BIG_ZERO = BigInt (0 )
528
+
529
+ struct NumberResult
530
+ tag:: UInt8
531
+ int:: Int64
532
+ float:: Float64
533
+ bigint:: BigInt
534
+ bigfloat:: BigFloat
535
+ NumberResult (int:: Int64 ) = new (INT, int)
536
+ NumberResult (float:: Float64 ) = new (FLOAT, Int64 (0 ), float)
537
+ NumberResult (bigint:: BigInt ) = new (BIGINT, Int64 (0 ), 0.0 , bigint)
538
+ NumberResult (bigfloat:: BigFloat ) = new (BIGFLOAT, Int64 (0 ), 0.0 , BIG_ZERO, bigfloat)
539
+ end
540
+
541
+ isint (x:: NumberResult ) = x. tag == INT
542
+ isfloat (x:: NumberResult ) = x. tag == FLOAT
543
+ isbigint (x:: NumberResult ) = x. tag == BIGINT
544
+ isbigfloat (x:: NumberResult ) = x. tag == BIGFLOAT
545
+
546
+ @inline function parsenumber (x:: LazyValue )
529
547
buf = getbuf (x)
530
548
pos = getpos (x)
531
549
len = getlength (buf)
@@ -610,23 +628,20 @@ function applynumber(valfunc, x::LazyValue)
610
628
# if we overflowed, then let's try BigFloat
611
629
bres = Parsers. xparse2 (BigFloat, buf, startpos, len)
612
630
if ! Parsers. invalid (bres. code)
613
- valfunc (bres. val)
614
- return startpos + bres. tlen
631
+ return NumberResult (bres. val), startpos + bres. tlen
615
632
end
616
633
end
617
634
if Parsers. invalid (res. code)
618
635
error = InvalidNumber
619
636
@goto invalid
620
637
end
621
- valfunc (res. val)
622
- return startpos + res. tlen
638
+ return NumberResult (res. val), startpos + res. tlen
623
639
else
624
640
if overflow
625
- valfunc (isneg ? - bval : bval)
641
+ return NumberResult (isneg ? - bval : bval), pos
626
642
else
627
- valfunc (isneg ? - val : val)
643
+ return NumberResult (isneg ? - val : val), pos
628
644
end
629
- return pos
630
645
end
631
646
632
647
@label invalid
@@ -643,9 +658,11 @@ function skip(x::LazyValues)
643
658
elseif T == JSONTypes. ARRAY
644
659
return applyarray ((i, v) -> 0 , x)
645
660
elseif T == JSONTypes. STRING
646
- return applystring (s -> 0 , x)
661
+ _, pos = parsestring (x)
662
+ return pos
647
663
elseif T == JSONTypes. NUMBER
648
- return applynumber (n -> 0 , x)
664
+ _, pos = parsenumber (x)
665
+ return pos
649
666
elseif T == JSONTypes. TRUE
650
667
return getpos (x) + 4
651
668
elseif T == JSONTypes. FALSE
@@ -716,7 +733,7 @@ function Base.show(io::IO, x::LazyValue)
716
733
show (io, MIME " text/plain" (), la)
717
734
end
718
735
elseif T == JSONTypes. STRING
719
- str, _ = applystring ( nothing , x)
736
+ str, _ = parsestring ( x)
720
737
Base. print (io, " JSON.LazyValue(" , repr (convert (String, str)), " )" )
721
738
elseif T == JSONTypes. NULL
722
739
Base. print (io, " JSON.LazyValue(nothing)" )
0 commit comments