-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More efficient encoding of primitives and
java.time._
values (#1293)
- Loading branch information
1 parent
84cc739
commit f12c952
Showing
11 changed files
with
1,658 additions
and
360 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
zio-json/js/src/main/scala/zio/json/internal/FastStringWrite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package zio.json.internal | ||
|
||
final class FastStringWrite(initial: Int) extends Write { | ||
require(initial >= 8) | ||
private[this] var chars: String = "" | ||
|
||
@inline def reset(): Unit = chars = "" | ||
|
||
@inline private[internal] def length: Int = chars.length | ||
|
||
@inline private[internal] def getChars: Array[Char] = chars.toCharArray | ||
|
||
@inline def write(s: String): Unit = chars += s | ||
|
||
@inline def write(c: Char): Unit = chars += c | ||
|
||
@inline override def write(cs: Array[Char], from: Int, to: Int): Unit = { | ||
var i = from | ||
while (i < to) { | ||
chars += cs(i) | ||
i += 1 | ||
} | ||
} | ||
|
||
@inline override def write(c1: Char, c2: Char): Unit = { | ||
chars += c1 | ||
chars += c2 | ||
} | ||
|
||
@inline override def write(c1: Char, c2: Char, c3: Char): Unit = { | ||
chars += c1 | ||
chars += c2 | ||
chars += c3 | ||
} | ||
|
||
@inline override def write(c1: Char, c2: Char, c3: Char, c4: Char): Unit = { | ||
chars += c1 | ||
chars += c2 | ||
chars += c3 | ||
chars += c4 | ||
} | ||
|
||
@inline override def write(c1: Char, c2: Char, c3: Char, c4: Char, c5: Char): Unit = { | ||
chars += c1 | ||
chars += c2 | ||
chars += c3 | ||
chars += c4 | ||
chars += c5 | ||
} | ||
|
||
@inline override def write(s: Short): Unit = { | ||
chars += (s & 0xff).toChar | ||
chars += (s >> 8).toChar | ||
} | ||
|
||
@inline override def write(s1: Short, s2: Short): Unit = { | ||
chars += (s1 & 0xff).toChar | ||
chars += (s1 >> 8).toChar | ||
chars += (s2 & 0xff).toChar | ||
chars += (s2 >> 8).toChar | ||
} | ||
|
||
@inline override def write(s1: Short, s2: Short, s3: Short): Unit = { | ||
chars += (s1 & 0xff).toChar | ||
chars += (s1 >> 8).toChar | ||
chars += (s2 & 0xff).toChar | ||
chars += (s2 >> 8).toChar | ||
chars += (s3 & 0xff).toChar | ||
chars += (s3 >> 8).toChar | ||
} | ||
|
||
@inline override def write(s1: Short, s2: Short, s3: Short, s4: Short): Unit = { | ||
chars += (s1 & 0xff).toChar | ||
chars += (s1 >> 8).toChar | ||
chars += (s2 & 0xff).toChar | ||
chars += (s2 >> 8).toChar | ||
chars += (s3 & 0xff).toChar | ||
chars += (s3 >> 8).toChar | ||
chars += (s4 & 0xff).toChar | ||
chars += (s4 >> 8).toChar | ||
} | ||
|
||
@inline def buffer: CharSequence = chars | ||
} |
Oops, something went wrong.