JS: improve json parsestring
This commit is contained in:
@@ -133,8 +133,8 @@ fun parseJson(file: File): JsonNode = parseJson(file.readText(Charsets.UTF_8))
|
|||||||
fun parseJson(text: String): JsonNode = JsonParser(text).parse()
|
fun parseJson(text: String): JsonNode = JsonParser(text).parse()
|
||||||
|
|
||||||
private class JsonParser(val content: String) {
|
private class JsonParser(val content: String) {
|
||||||
private var index = 0
|
private var index = -1
|
||||||
private var charCode = content.getOrNull(index++)?.toInt() ?: -1
|
private var charCode = content.getOrNull(++index)?.toInt() ?: -1
|
||||||
private var offset = 0
|
private var offset = 0
|
||||||
private var line = 0
|
private var line = 0
|
||||||
private var col = 0
|
private var col = 0
|
||||||
@@ -236,21 +236,30 @@ private class JsonParser(val content: String) {
|
|||||||
expectCharAndAdvance('"')
|
expectCharAndAdvance('"')
|
||||||
|
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
while (true) {
|
|
||||||
|
var leftIndex = index
|
||||||
|
while (index < content.length) {
|
||||||
|
charCode = content[index].toInt()
|
||||||
|
|
||||||
|
if (charCode < ' '.toInt()) {
|
||||||
|
error("Invalid character in string literal")
|
||||||
|
}
|
||||||
|
|
||||||
when (charCode) {
|
when (charCode) {
|
||||||
'"'.toInt() -> { advance(); return sb.toString() }
|
'"'.toInt() -> {
|
||||||
'\\'.toInt() -> sb.append(parseEscapeSequence())
|
sb.append(content, leftIndex, index)
|
||||||
else -> {
|
advance()
|
||||||
if (charCode >= ' '.toInt()) {
|
return sb.toString()
|
||||||
sb.append(charCode.toChar())
|
|
||||||
advance()
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
error("Invalid character in string literal")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
'\\'.toInt() -> {
|
||||||
|
sb.append(content, leftIndex, index)
|
||||||
|
sb.append(parseEscapeSequence())
|
||||||
|
leftIndex = index
|
||||||
|
}
|
||||||
|
else -> ++index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
error("Unexpected end of file")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseEscapeSequence(): Char {
|
private fun parseEscapeSequence(): Char {
|
||||||
@@ -365,7 +374,7 @@ private class JsonParser(val content: String) {
|
|||||||
wasCR = false
|
wasCR = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
charCode = content.getOrNull(index++)?.toInt() ?: -1
|
charCode = content.getOrNull(++index)?.toInt() ?: -1
|
||||||
offset++
|
offset++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user