Skip to content

Commit

Permalink
[code] performance fixes and better compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
fab1an committed Jul 14, 2023
1 parent 51b9561 commit cc3821f
Show file tree
Hide file tree
Showing 7 changed files with 604 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package com.fab1an.kotlinjsonstream
import okio.BufferedSource
import okio.ByteString
import okio.ByteString.Companion.encodeUtf8
import okio.Options

private val BYTESTRING_ZERO = ByteString.of('0'.code.toByte())
internal val BYTESTRING_ZERO = ByteString.of('0'.code.toByte())
private val BYTESTRING_ONE = ByteString.of('1'.code.toByte())
private val BYTESTRING_TWO = ByteString.of('2'.code.toByte())
private val BYTESTRING_THREE = ByteString.of('3'.code.toByte())
Expand All @@ -14,13 +15,14 @@ private val BYTESTRING_SIX = ByteString.of('6'.code.toByte())
private val BYTESTRING_SEVEN = ByteString.of('7'.code.toByte())
private val BYTESTRING_EIGHT = ByteString.of('8'.code.toByte())
private val BYTESTRING_NINE = ByteString.of('9'.code.toByte())
private val BYTESTRING_HYPHEN = ByteString.of('-'.code.toByte())
internal val BYTESTRING_HYPHEN = ByteString.of('-'.code.toByte())

private val BYTESTRING_TAB = ByteString.of('\t'.code.toByte())
private val BYTESTRING_CARRIAGE_RETURN = ByteString.of('\r'.code.toByte())
internal val BYTESTRING_TAB = ByteString.of('\t'.code.toByte())
internal val BYTESTRING_CARRIAGERETURN = ByteString.of('\r'.code.toByte())
internal val BYTESTRING_NEWLINE = ByteString.of('\n'.code.toByte())
internal val BYTESTRING_SPACE = ByteString.of(' '.code.toByte())

internal val BYTESTRING_BACKSLASH_OR_DOUBLEQUOTE = "\\\"".encodeUtf8()
internal val BYTESTRING_SQUAREBRACKET_OPEN = ByteString.of('['.code.toByte())
internal val BYTESTRING_SQUAREBRACKET_CLOSE = ByteString.of(']'.code.toByte())
internal val BYTESTRING_CURLYBRACKET_OPEN = ByteString.of('{'.code.toByte())
Expand All @@ -29,11 +31,44 @@ internal val BYTESTRING_COLON = ByteString.of(':'.code.toByte())
internal val BYTESTRING_DOUBLEQUOTE = ByteString.of('"'.code.toByte())
internal val BYTESTRING_DOT = ByteString.of('.'.code.toByte())
internal val BYTESTRING_COMMA = ByteString.of(','.code.toByte())
internal val BYTESTRING_BACKSLASH = ByteString.of('\\'.code.toByte())
internal val BYTESTRING_TRUE = "true".encodeUtf8()
internal val BYTESTRING_FALSE = "false".encodeUtf8()
internal val BYTESTRING_NULL = "null".encodeUtf8()
internal val BYTESTRING_FORWARDSLASH = "/".encodeUtf8()
internal val BYTESTRING_BACKSPACE = "\b".encodeUtf8()
internal val BYTESTRING_FORMFEED = "\u000c".encodeUtf8()

internal val BYTESTRING_ESCAPED_DOUBLEQUOTE = """\"""".encodeUtf8()
internal val BYTESTRING_ESCAPED_FORWARD_SLASH = """\\""".encodeUtf8()
internal val BYTESTRING_ESCAPED_BACKSLASH = """\\""".encodeUtf8()
internal val BYTESTRING_ESCAPED_FORWARDSLASH = """\/""".encodeUtf8()
internal val BYTESTRING_ESCAPED_BACKSPACE = """\b""".encodeUtf8()
internal val BYTESTRING_ESCAPED_FORMFEED = """\f""".encodeUtf8()
internal val BYTESTRING_ESCAPED_NEWLINE = """\n""".encodeUtf8()
internal val BYTESTRING_ESCAPED_CARRIAGERETURN = """\r""".encodeUtf8()
internal val BYTESTRING_ESCAPED_TAB = """\t""".encodeUtf8()
internal val BYTESTRING_START_OF_UNICODE_ESCAPE = """\u""".encodeUtf8()

private val booleanOptions = Options.of(
BYTESTRING_TRUE,
BYTESTRING_FALSE
)

internal fun BufferedSource.readJsonBoolean(): Boolean {
return when (select(booleanOptions)) {
0 -> true
1 -> false
else -> error("expected true/false, but got: ${readUtf8CodePoint()}")
}
}

internal fun BufferedSource.nextIsHyphenOrAsciiDigit(): Boolean {
return when {
nextIsAsciiDigit() -> true
rangeEquals(0, BYTESTRING_HYPHEN) -> true
else -> false
}
}

internal fun BufferedSource.nextIsAsciiDigit(): Boolean {
return when {
Expand All @@ -47,21 +82,24 @@ internal fun BufferedSource.nextIsAsciiDigit(): Boolean {
rangeEquals(0, BYTESTRING_SEVEN) -> true
rangeEquals(0, BYTESTRING_EIGHT) -> true
rangeEquals(0, BYTESTRING_NINE) -> true
rangeEquals(0, BYTESTRING_HYPHEN) -> true
else -> false
}
}

internal fun BufferedSource.nextIsJsonWhiteSpace(): Boolean {
return when {
rangeEquals(0, BYTESTRING_TAB) -> true
rangeEquals(0, BYTESTRING_NEWLINE) -> true
rangeEquals(0, BYTESTRING_CARRIAGE_RETURN) -> true
rangeEquals(0, BYTESTRING_SPACE) -> true
else -> false
private val whitespaceOptions = Options.of(
BYTESTRING_TAB,
BYTESTRING_CARRIAGERETURN,
BYTESTRING_NEWLINE,
BYTESTRING_SPACE
)

internal fun BufferedSource.skipWhitespace() {
while (select(whitespaceOptions) != -1) {
//
}
}


internal fun BufferedSource.nextIs(byteString: ByteString): Boolean {
return rangeEquals(0, byteString)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fab1an.kotlinjsonstream

/**
* Writes the [list] of [E] to this [JsonWriter], using the supplied [writerFn].
* Writes a [list] of [E] to this [JsonWriter], using the supplied [writerFn].
*
* @param E the type of the objects
* @param list the list of objects
Expand Down Expand Up @@ -34,11 +34,11 @@ inline fun <E : Any> JsonReader.nextList(readerFn: JsonReader.() -> E): List<E>
}

/**
* Writes the [set] of [E] to this [JsonWriter], using the supplied [writerFn].
* Writes a [Set] of [E] to this [JsonWriter], using the supplied [writerFn].
*
* @param E the type of the objects
* @param set the set of objects
* @param writerFn the writer function that is called using each item of the [list] as input
* @param writerFn the writer function that is called using each item of the [set] as input
*/
inline fun <E : Any> JsonWriter.value(set: Set<E>, writerFn: JsonWriter.(E) -> Unit) {
beginArray()
Expand All @@ -47,7 +47,7 @@ inline fun <E : Any> JsonWriter.value(set: Set<E>, writerFn: JsonWriter.(E) -> U
}

/**
* Reads a [Set] of [E] from this [JsonReader]. This function assumes it is called at the start
* Reads a [set] of [E] from this [JsonReader]. This function assumes it is called at the start
* of a JSON-array and reads each element using the supplied [readerFn].
*
* @param E the type of the objects
Expand Down
Loading

0 comments on commit cc3821f

Please sign in to comment.