Skip to content

Commit 5942eae

Browse files
authored
Improved Json class
1 parent f6f45a1 commit 5942eae

File tree

1 file changed

+20
-32
lines changed
  • src/main/java/com/github/underscore

1 file changed

+20
-32
lines changed

src/main/java/com/github/underscore/Json.kt

+20-32
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,7 @@ object Json {
402402
}
403403

404404
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 ->
408406
when (ch) {
409407
'"' -> sb.append("\\\"")
410408
'\\' -> sb.append("\\\\")
@@ -414,15 +412,8 @@ object Json {
414412
'\r' -> sb.append("\\r")
415413
'\t' -> sb.append("\\t")
416414
'' -> 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()}")
426417
} else {
427418
sb.append(ch)
428419
}
@@ -444,7 +435,7 @@ object Json {
444435
private var line = 1
445436
private var lineOffset = 0
446437
private var current = 0
447-
private var captureBuffer: StringBuilder? = null
438+
private var captureBuffer: StringBuilder = StringBuilder()
448439
private var captureStart: Int
449440

450441
init {
@@ -577,12 +568,12 @@ object Json {
577568
private fun readEscape() {
578569
read()
579570
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')
586577
'u' -> {
587578
val hexChars = CharArray(4)
588579
var isHexCharsDigits = true
@@ -596,14 +587,14 @@ object Json {
596587
i++
597588
}
598589
if (isHexCharsDigits) {
599-
captureBuffer!!.append(String(hexChars).toInt(16).toChar())
590+
captureBuffer.append(String(hexChars).toInt(16).toChar())
600591
} else {
601592
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])
607598
}
608599
}
609600

@@ -710,24 +701,21 @@ object Json {
710701
}
711702

712703
private fun startCapture() {
713-
if (captureBuffer == null) {
714-
captureBuffer = StringBuilder()
715-
}
716704
captureStart = index - 1
717705
}
718706

719707
private fun pauseCapture() {
720-
captureBuffer!!.append(json, captureStart, index - 1)
708+
captureBuffer.append(json, captureStart, index - 1)
721709
captureStart = -1
722710
}
723711

724712
private fun endCapture(): String {
725713
val end = if (current == -1) index else index - 1
726714
val captured: String
727-
if (captureBuffer!!.isNotEmpty()) {
728-
captureBuffer!!.append(json, captureStart, end)
715+
if (captureBuffer.isNotEmpty()) {
716+
captureBuffer.append(json, captureStart, end)
729717
captured = captureBuffer.toString()
730-
captureBuffer!!.setLength(0)
718+
captureBuffer.setLength(0)
731719
} else {
732720
captured = json.substring(captureStart, end)
733721
}

0 commit comments

Comments
 (0)