Skip to content

Commit 1186a5c

Browse files
committed
More efficient serialization of strings with escaped unicodes for Scala.js
1 parent a42a5de commit 1186a5c

File tree

1 file changed

+13
-15
lines changed
  • jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core

1 file changed

+13
-15
lines changed

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ final class JsonWriter private[jsoniter_scala](
14481448
}
14491449

14501450
private[this] def writeEscapedOrEncodedString(s: String, from: Int, pos: Int, escapedChars: Array[Byte]): Int =
1451-
if (config.escapeUnicode) writeEscapedString(s, from, s.length, pos, limit - 13, escapedChars)
1451+
if (config.escapeUnicode) writeEscapedString(s, from, s.length, pos, limit - 13, escapedChars, lowerCaseHexDigits)
14521452
else writeEncodedString(s, from, s.length, pos, limit - 7, escapedChars)
14531453

14541454
@tailrec
@@ -1466,7 +1466,7 @@ final class JsonWriter private[jsoniter_scala](
14661466
buf(pos) = '\\'
14671467
buf(pos + 1) = esc
14681468
writeEncodedString(s, from + 1, to, pos + 2, posLim, escapedChars)
1469-
} else writeEncodedString(s, from + 1, to, writeEscapedUnicode(ch1.toByte, pos, buf), posLim, escapedChars)
1469+
} else writeEncodedString(s, from + 1, to, writeEscapedUnicode(ch1.toByte, pos, buf, lowerCaseHexDigits), posLim, escapedChars)
14701470
} else if (ch1 < 0x800) { // 00000bbbbbaaaaaa (UTF-16 char) -> 110bbbbb 10aaaaaa (UTF-8 bytes)
14711471
buf(pos) = (ch1 >> 6 | 0xC0).toByte
14721472
buf(pos + 1) = (ch1 & 0x3F | 0x80).toByte
@@ -1492,30 +1492,30 @@ final class JsonWriter private[jsoniter_scala](
14921492
}
14931493

14941494
@tailrec
1495-
private[this] def writeEscapedString(s: String, from: Int, to: Int, pos: Int, posLim: Int, escapedChars: Array[Byte]): Int =
1495+
private[this] def writeEscapedString(s: String, from: Int, to: Int, pos: Int, posLim: Int, escapedChars: Array[Byte], ds: Array[Short]): Int =
14961496
if (from >= to) pos
1497-
else if (pos >= posLim) writeEscapedString(s, from, to, flushAndGrowBuf(13, pos), limit - 12, escapedChars)
1497+
else if (pos >= posLim) writeEscapedString(s, from, to, flushAndGrowBuf(13, pos), limit - 12, escapedChars, ds)
14981498
else {
14991499
val ch1 = s.charAt(from).toInt
15001500
if (ch1 < 0x80) {
15011501
val esc = escapedChars(ch1)
15021502
if (esc == 0) {
15031503
buf(pos) = ch1.toByte
1504-
writeEscapedString(s, from + 1, to, pos + 1, posLim, escapedChars)
1504+
writeEscapedString(s, from + 1, to, pos + 1, posLim, escapedChars, ds)
15051505
} else if (esc > 0) {
15061506
buf(pos) = '\\'
15071507
buf(pos + 1) = esc
1508-
writeEscapedString(s, from + 1, to, pos + 2, posLim, escapedChars)
1509-
} else writeEscapedString(s, from + 1, to, writeEscapedUnicode(ch1.toByte, pos, buf), posLim, escapedChars)
1508+
writeEscapedString(s, from + 1, to, pos + 2, posLim, escapedChars, ds)
1509+
} else writeEscapedString(s, from + 1, to, writeEscapedUnicode(ch1.toByte, pos, buf, ds), posLim, escapedChars, ds)
15101510
} else if ((ch1 & 0xF800) != 0xD800) {
1511-
writeEscapedString(s, from + 1, to, writeEscapedUnicode(ch1, pos, buf), posLim, escapedChars)
1511+
writeEscapedString(s, from + 1, to, writeEscapedUnicode(ch1, pos, buf, ds), posLim, escapedChars, ds)
15121512
} else {
15131513
var ch2 = 0
15141514
if (ch1 >= 0xDC00 || from + 1 >= to || {
15151515
ch2 = s.charAt(from + 1).toInt
15161516
(ch2 & 0xFC00) != 0xDC00
15171517
}) illegalSurrogateError()
1518-
writeEscapedString(s, from + 2, to, writeEscapedUnicode(ch2, writeEscapedUnicode(ch1, pos, buf), buf), posLim, escapedChars)
1518+
writeEscapedString(s, from + 2, to, writeEscapedUnicode(ch2, writeEscapedUnicode(ch1, pos, buf, ds), buf, ds), posLim, escapedChars, ds)
15191519
}
15201520
}
15211521

@@ -1533,10 +1533,10 @@ final class JsonWriter private[jsoniter_scala](
15331533
buf(pos) = '\\'
15341534
buf(pos + 1) = esc
15351535
pos += 2
1536-
} else pos = writeEscapedUnicode(ch.toByte, pos, buf)
1536+
} else pos = writeEscapedUnicode(ch.toByte, pos, buf, lowerCaseHexDigits)
15371537
} else if (config.escapeUnicode) {
15381538
if ((ch & 0xF800) == 0xD800) illegalSurrogateError()
1539-
pos = writeEscapedUnicode(ch, pos, buf)
1539+
pos = writeEscapedUnicode(ch, pos, buf, lowerCaseHexDigits)
15401540
} else if (ch < 0x800) { // 00000bbbbbaaaaaa (UTF-16 char) -> 110bbbbb 10aaaaaa (UTF-8 bytes)
15411541
buf(pos) = (ch >> 6 | 0xC0).toByte
15421542
buf(pos + 1) = (ch & 0x3F | 0x80).toByte
@@ -1551,8 +1551,7 @@ final class JsonWriter private[jsoniter_scala](
15511551
count = pos + 1
15521552
}
15531553

1554-
private[this] def writeEscapedUnicode(ch: Int, pos: Int, buf: Array[Byte]): Int = {
1555-
val ds = lowerCaseHexDigits
1554+
private[this] def writeEscapedUnicode(ch: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
15561555
buf(pos) = '\\'
15571556
buf(pos + 1) = 'u'
15581557
val d1 = ds(ch >> 8)
@@ -1564,8 +1563,7 @@ final class JsonWriter private[jsoniter_scala](
15641563
pos + 6
15651564
}
15661565

1567-
private[this] def writeEscapedUnicode(b: Byte, pos: Int, buf: Array[Byte]): Int = {
1568-
val ds = lowerCaseHexDigits
1566+
private[this] def writeEscapedUnicode(b: Byte, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
15691567
buf(pos) = '\\'
15701568
buf(pos + 1) = 'u'
15711569
buf(pos + 2) = '0'

0 commit comments

Comments
 (0)