@@ -369,16 +369,20 @@ internal static string ParseString(string json, ref int index)
369
369
internal static StringSegment ParseString ( StringSegment json , ref int index )
370
370
{
371
371
var jsonLength = json . Length ;
372
- if ( json . GetChar ( index ) != JsonUtils . QuoteChar )
372
+ var buffer = json . Buffer ;
373
+ var offset = json . Offset ;
374
+
375
+ if ( buffer [ offset + index ] != JsonUtils . QuoteChar )
373
376
throw new Exception ( "Invalid unquoted string starting with: " + json . Value . SafeSubstring ( 50 ) ) ;
374
377
375
378
var startIndex = ++ index ;
376
379
do
377
380
{
378
- char c = json . GetChar ( index ) ;
381
+ char c = buffer [ offset + index ] ;
379
382
if ( c == JsonUtils . QuoteChar ) break ;
380
383
if ( c != JsonUtils . EscapeChar ) continue ;
381
- c = json . GetChar ( index ++ ) ;
384
+ c = buffer [ offset + index ] ;
385
+ index ++ ;
382
386
if ( c == 'u' )
383
387
{
384
388
index += 4 ;
@@ -387,7 +391,7 @@ internal static StringSegment ParseString(StringSegment json, ref int index)
387
391
if ( index == jsonLength )
388
392
throw new Exception ( "Invalid unquoted string ending with: " + json . Value . SafeSubstring ( json . Length - 50 , 50 ) ) ;
389
393
index ++ ;
390
- return json . Subsegment ( startIndex , Math . Min ( index , jsonLength ) - startIndex - 1 ) ;
394
+ return new StringSegment ( buffer , offset + startIndex , Math . Min ( index , jsonLength ) - startIndex - 1 ) ;
391
395
}
392
396
393
397
public string UnescapeString ( string value )
@@ -435,8 +439,10 @@ private static StringSegment UnEscapeJsonString(StringSegment json, ref int inde
435
439
{
436
440
if ( json . IsNullOrEmpty ( ) ) return json ;
437
441
var jsonLength = json . Length ;
442
+ var buffer = json . Buffer ;
443
+ var offset = json . Offset ;
438
444
439
- var firstChar = json . GetChar ( index ) ;
445
+ var firstChar = buffer [ offset + index ] ;
440
446
if ( firstChar == JsonUtils . QuoteChar )
441
447
{
442
448
index ++ ;
@@ -455,7 +461,7 @@ private static StringSegment UnEscapeJsonString(StringSegment json, ref int inde
455
461
else
456
462
{
457
463
var strEndPos = json . IndexOfAny ( IsSafeJsonChars , index ) ;
458
- if ( strEndPos == - 1 ) return json . Subsegment ( index , jsonLength - index ) ;
464
+ if ( strEndPos == - 1 ) return new StringSegment ( buffer , offset + index , jsonLength - index ) ;
459
465
}
460
466
461
467
return Unescape ( json ) ;
@@ -708,7 +714,7 @@ public StringSegment EatValue(StringSegment value, ref int i)
708
714
var offset = value . Offset ;
709
715
if ( i == valueLength ) return default ( StringSegment ) ;
710
716
711
- for ( ; i < value . Length ; i ++ ) { var c = buf [ offset + i ] ; if ( ! JsonUtils . IsWhiteSpace ( c ) ) break ; } //Whitespace inline
717
+ while ( i < valueLength && JsonUtils . IsWhiteSpace ( buf [ offset + i ] ) ) i ++ ; //Whitespace inline
712
718
if ( i == valueLength ) return default ( StringSegment ) ;
713
719
714
720
var tokenStartPos = i ;
@@ -729,7 +735,7 @@ public StringSegment EatValue(StringSegment value, ref int i)
729
735
730
736
//Is Type/Map, i.e. {...}
731
737
case JsWriter . MapStartChar :
732
- while ( ++ i < valueLength && endsToEat > 0 )
738
+ while ( ++ i < valueLength )
733
739
{
734
740
valueChar = buf [ offset + i ] ;
735
741
@@ -748,14 +754,17 @@ public StringSegment EatValue(StringSegment value, ref int i)
748
754
if ( valueChar == JsWriter . MapStartChar )
749
755
endsToEat ++ ;
750
756
751
- if ( valueChar == JsWriter . MapEndChar )
752
- endsToEat -- ;
757
+ if ( valueChar == JsWriter . MapEndChar && -- endsToEat == 0 )
758
+ {
759
+ i ++ ;
760
+ break ;
761
+ }
753
762
}
754
763
return value . Subsegment ( tokenStartPos , i - tokenStartPos ) ;
755
764
756
765
//Is List, i.e. [...]
757
766
case JsWriter . ListStartChar :
758
- while ( ++ i < valueLength && endsToEat > 0 )
767
+ while ( ++ i < valueLength )
759
768
{
760
769
valueChar = buf [ offset + i ] ;
761
770
@@ -774,8 +783,11 @@ public StringSegment EatValue(StringSegment value, ref int i)
774
783
if ( valueChar == JsWriter . ListStartChar )
775
784
endsToEat ++ ;
776
785
777
- if ( valueChar == JsWriter . ListEndChar )
778
- endsToEat -- ;
786
+ if ( valueChar == JsWriter . ListEndChar && -- endsToEat == 0 )
787
+ {
788
+ i ++ ;
789
+ break ;
790
+ }
779
791
}
780
792
return value . Subsegment ( tokenStartPos , i - tokenStartPos ) ;
781
793
}
0 commit comments