Skip to content

Commit 3fd755e

Browse files
optimized char check for low surrogate (#1109)
1 parent 573a334 commit 3fd755e

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3541,7 +3541,7 @@ final class JsonReader private[jsoniter_scala](
35413541
if (buf(pos + 6) != '\\') illegalEscapeSequenceError(pos + 6)
35423542
if (buf(pos + 7) != 'u') illegalEscapeSequenceError(pos + 7)
35433543
val ch2 = readEscapedUnicode(pos + 8, buf)
3544-
if (ch1 >= 0xDC00 || ch2 < 0xDC00 || ch2 > 0xDFFF) decodeError("illegal surrogate character pair", pos + 11)
3544+
if (ch1 >= 0xDC00 || (ch2 & 0xFC00) != 0xDC00) decodeError("illegal surrogate character pair", pos + 11)
35453545
charBuf(i + 1) = ch2
35463546
parseEncodedString(i + 2, lim, charBuf, pos + 12)
35473547
} else parseEncodedString(i, lim, charBuf, loadMoreOrError(pos))

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ final class JsonWriter private[jsoniter_scala](
14801480
} else { // 110110uuuuccccbb 110111bbbbaaaaaa (UTF-16 chars) -> 11110ddd 10ddcccc 10bbbbbb 10aaaaaa (UTF-8 bytes), where ddddd = uuuu + 1
14811481
if (ch1 >= 0xDC00 || from + 1 >= to) illegalSurrogateError()
14821482
val ch2 = s.charAt(from + 1)
1483-
if (ch2 < 0xDC00 || ch2 > 0xDFFF) illegalSurrogateError()
1483+
if ((ch2 & 0xFC00) != 0xDC00) illegalSurrogateError()
14841484
val cp = (ch1 << 10) + (ch2 - 56613888) // -56613888 == 0x10000 - (0xD800 << 10) - 0xDC00
14851485
buf(pos) = (cp >> 18 | 0xF0).toByte
14861486
buf(pos + 1) = (cp >> 12 & 0x3F | 0x80).toByte
@@ -1511,7 +1511,7 @@ final class JsonWriter private[jsoniter_scala](
15111511
} else {
15121512
if (ch1 >= 0xDC00 || from + 1 >= to) illegalSurrogateError()
15131513
val ch2 = s.charAt(from + 1).toInt
1514-
if (ch2 < 0xDC00 || ch2 > 0xDFFF) illegalSurrogateError()
1514+
if ((ch2 & 0xFC00) != 0xDC00) illegalSurrogateError()
15151515
writeEscapedString(s, from + 2, to, writeEscapedUnicode(ch2, writeEscapedUnicode(ch1, pos, buf), buf), posLim, escapedChars)
15161516
}
15171517
}

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4035,7 +4035,7 @@ final class JsonReader private[jsoniter_scala](
40354035
if (buf(pos + 6) != '\\') illegalEscapeSequenceError(pos + 6)
40364036
if (buf(pos + 7) != 'u') illegalEscapeSequenceError(pos + 7)
40374037
val ch2 = readEscapedUnicode(pos + 8, buf)
4038-
if (ch1 >= 0xDC00 || ch2 < 0xDC00 || ch2 > 0xDFFF) decodeError("illegal surrogate character pair", pos + 11)
4038+
if (ch1 >= 0xDC00 || (ch2 & 0xFC00) != 0xDC00) decodeError("illegal surrogate character pair", pos + 11)
40394039
charBuf(i + 1) = ch2
40404040
parseEncodedString(i + 2, lim, charBuf, pos + 12)
40414041
} else parseEncodedString(i, lim, charBuf, loadMoreOrError(pos))

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ final class JsonWriter private[jsoniter_scala](
13391339
} else { // 110110uuuuccccbb 110111bbbbaaaaaa (UTF-16 chars) -> 11110ddd 10ddcccc 10bbbbbb 10aaaaaa (UTF-8 bytes), where ddddd = uuuu + 1
13401340
if (ch1 >= 0xDC00 || from + 1 >= to) illegalSurrogateError()
13411341
val ch2 = s.charAt(from + 1)
1342-
if (ch2 < 0xDC00 || ch2 > 0xDFFF) illegalSurrogateError()
1342+
if ((ch2 & 0xFC00) != 0xDC00) illegalSurrogateError()
13431343
val cp = (ch1 << 10) + (ch2 - 56613888) // -56613888 == 0x10000 - (0xD800 << 10) - 0xDC00
13441344
ByteArrayAccess.setInt(buf, pos, cp >> 18 | (cp >> 4 & 0x3F00) | (cp << 10 & 0x3F0000) | (cp << 24 & 0x3F000000) | 0x808080F0)
13451345
writeEncodedString(s, from + 2, to, pos + 4, posLim, escapedChars)
@@ -1366,7 +1366,7 @@ final class JsonWriter private[jsoniter_scala](
13661366
} else {
13671367
if (ch1 >= 0xDC00 || from + 1 >= to) illegalSurrogateError()
13681368
val ch2 = s.charAt(from + 1).toInt
1369-
if (ch2 < 0xDC00 || ch2 > 0xDFFF) illegalSurrogateError()
1369+
if ((ch2 & 0xFC00) != 0xDC00) illegalSurrogateError()
13701370
writeEscapedString(s, from + 2, to, writeEscapedUnicode(ch2, writeEscapedUnicode(ch1, pos, buf), buf), posLim, escapedChars)
13711371
}
13721372
}

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4032,7 +4032,7 @@ final class JsonReader private[jsoniter_scala](
40324032
if (buf(pos + 6) != '\\') illegalEscapeSequenceError(pos + 6)
40334033
if (buf(pos + 7) != 'u') illegalEscapeSequenceError(pos + 7)
40344034
val ch2 = readEscapedUnicode(pos + 8, buf)
4035-
if (ch1 >= 0xDC00 || ch2 < 0xDC00 || ch2 > 0xDFFF) decodeError("illegal surrogate character pair", pos + 11)
4035+
if (ch1 >= 0xDC00 || (ch2 & 0xFC00) != 0xDC00) decodeError("illegal surrogate character pair", pos + 11)
40364036
charBuf(i + 1) = ch2
40374037
parseEncodedString(i + 2, lim, charBuf, pos + 12)
40384038
} else parseEncodedString(i, lim, charBuf, loadMoreOrError(pos))

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ final class JsonWriter private[jsoniter_scala](
13391339
} else { // 110110uuuuccccbb 110111bbbbaaaaaa (UTF-16 chars) -> 11110ddd 10ddcccc 10bbbbbb 10aaaaaa (UTF-8 bytes), where ddddd = uuuu + 1
13401340
if (ch1 >= 0xDC00 || from + 1 >= to) illegalSurrogateError()
13411341
val ch2 = s.charAt(from + 1)
1342-
if (ch2 < 0xDC00 || ch2 > 0xDFFF) illegalSurrogateError()
1342+
if ((ch2 & 0xFC00) != 0xDC00) illegalSurrogateError()
13431343
val cp = (ch1 << 10) + (ch2 - 56613888) // -56613888 == 0x10000 - (0xD800 << 10) - 0xDC00
13441344
ByteArrayAccess.setInt(buf, pos, cp >> 18 | (cp >> 4 & 0x3F00) | (cp << 10 & 0x3F0000) | (cp << 24 & 0x3F000000) | 0x808080F0)
13451345
writeEncodedString(s, from + 2, to, pos + 4, posLim, escapedChars)
@@ -1366,7 +1366,7 @@ final class JsonWriter private[jsoniter_scala](
13661366
} else {
13671367
if (ch1 >= 0xDC00 || from + 1 >= to) illegalSurrogateError()
13681368
val ch2 = s.charAt(from + 1).toInt
1369-
if (ch2 < 0xDC00 || ch2 > 0xDFFF) illegalSurrogateError()
1369+
if ((ch2 & 0xFC00) != 0xDC00) illegalSurrogateError()
13701370
writeEscapedString(s, from + 2, to, writeEscapedUnicode(ch2, writeEscapedUnicode(ch1, pos, buf), buf), posLim, escapedChars)
13711371
}
13721372
}

0 commit comments

Comments
 (0)