@@ -402,9 +402,7 @@ object Json {
402
402
}
403
403
404
404
private fun escape (s : String , sb : StringBuilder ) {
405
- val len = s.length
406
- for (i in 0 until len) {
407
- val ch = s[i]
405
+ s.forEach { ch ->
408
406
when (ch) {
409
407
' "' -> sb.append(" \\\" " )
410
408
' \\ ' -> sb.append(" \\\\ " )
@@ -414,15 +412,8 @@ object Json {
414
412
' \r ' -> sb.append(" \\ r" )
415
413
' \t ' -> sb.append(" \\ t" )
416
414
' €' -> sb.append(' €' )
417
- else -> if (ch <= ' \u001F ' || ch >= ' \u007F ' && ch <= ' \u009F ' || ch >= ' \u2000 ' && ch <= ' \u20FF ' ) {
418
- val ss = Integer .toHexString(ch.code)
419
- sb.append(" \\ u" )
420
- var k = 0
421
- while (k < 4 - ss.length) {
422
- sb.append(" 0" )
423
- k++
424
- }
425
- sb.append(ss.uppercase(Locale .getDefault()))
415
+ else -> if (ch <= ' \u001F ' || ch in ' \u007F ' .. ' \u009F ' || ch in ' \u2000 ' .. ' \u20FF ' ) {
416
+ sb.append(" \\ u${ch.code.toString(16 ).padStart(4 , ' 0' ).uppercase()} " )
426
417
} else {
427
418
sb.append(ch)
428
419
}
@@ -444,7 +435,7 @@ object Json {
444
435
private var line = 1
445
436
private var lineOffset = 0
446
437
private var current = 0
447
- private var captureBuffer: StringBuilder ? = null
438
+ private var captureBuffer: StringBuilder = StringBuilder ()
448
439
private var captureStart: Int
449
440
450
441
init {
@@ -577,12 +568,12 @@ object Json {
577
568
private fun readEscape () {
578
569
read()
579
570
when (current.toChar()) {
580
- ' "' , ' /' , ' \\ ' -> captureBuffer!! .append(current.toChar())
581
- ' b' -> captureBuffer!! .append(' \b ' )
582
- ' f' -> captureBuffer!! .append(' \u000c ' )
583
- ' n' -> captureBuffer!! .append(' \n ' )
584
- ' r' -> captureBuffer!! .append(' \r ' )
585
- ' t' -> captureBuffer!! .append(' \t ' )
571
+ ' "' , ' /' , ' \\ ' -> captureBuffer.append(current.toChar())
572
+ ' b' -> captureBuffer.append(' \b ' )
573
+ ' f' -> captureBuffer.append(' \u000c ' )
574
+ ' n' -> captureBuffer.append(' \n ' )
575
+ ' r' -> captureBuffer.append(' \r ' )
576
+ ' t' -> captureBuffer.append(' \t ' )
586
577
' u' -> {
587
578
val hexChars = CharArray (4 )
588
579
var isHexCharsDigits = true
@@ -596,14 +587,14 @@ object Json {
596
587
i++
597
588
}
598
589
if (isHexCharsDigits) {
599
- captureBuffer!! .append(String (hexChars).toInt(16 ).toChar())
590
+ captureBuffer.append(String (hexChars).toInt(16 ).toChar())
600
591
} else {
601
592
captureBuffer
602
- ? .append(" \\ u" )
603
- ? .append(hexChars[0 ])
604
- ? .append(hexChars[1 ])
605
- ? .append(hexChars[2 ])
606
- ? .append(hexChars[3 ])
593
+ .append(" \\ u" )
594
+ .append(hexChars[0 ])
595
+ .append(hexChars[1 ])
596
+ .append(hexChars[2 ])
597
+ .append(hexChars[3 ])
607
598
}
608
599
}
609
600
@@ -710,24 +701,21 @@ object Json {
710
701
}
711
702
712
703
private fun startCapture () {
713
- if (captureBuffer == null ) {
714
- captureBuffer = StringBuilder ()
715
- }
716
704
captureStart = index - 1
717
705
}
718
706
719
707
private fun pauseCapture () {
720
- captureBuffer!! .append(json, captureStart, index - 1 )
708
+ captureBuffer.append(json, captureStart, index - 1 )
721
709
captureStart = - 1
722
710
}
723
711
724
712
private fun endCapture (): String {
725
713
val end = if (current == - 1 ) index else index - 1
726
714
val captured: String
727
- if (captureBuffer!! .isNotEmpty()) {
728
- captureBuffer!! .append(json, captureStart, end)
715
+ if (captureBuffer.isNotEmpty()) {
716
+ captureBuffer.append(json, captureStart, end)
729
717
captured = captureBuffer.toString()
730
- captureBuffer!! .setLength(0 )
718
+ captureBuffer.setLength(0 )
731
719
} else {
732
720
captured = json.substring(captureStart, end)
733
721
}
0 commit comments