Coverage Summary for Class: GhostJsonStringReaderCaptureKt (com.ghost.serialization.parser)
| Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| GhostJsonStringReaderCaptureKt |
100%
(1/1)
|
100%
(4/4)
|
38.7%
(12/31)
|
70.4%
(19/27)
|
57.8%
(89/154)
|
package com.ghost.serialization.parser
import com.ghost.serialization.parser.GhostJsonConstants as C
import com.ghost.serialization.types.RawJson
/**
* Captures the next complete JSON value as owned [RawJson] (UTF-16 source requires encoding).
*/
fun GhostJsonStringReader.captureRawJson(): RawJson =
RawJson.fromUtf8Bytes(captureRawJsonBytes())
/**
* Captures the next complete JSON value as a raw [ByteArray] without decoding.
*
* Since [GhostJsonStringReader] operates on a UTF-16 [String], the captured char range is
* encoded to UTF-8 via [GhostJsonStringReader.sliceUtf8Bytes] (slice-only when no UTF-8
* cache exists; otherwise a zero-copy slice from [GhostJsonStringReader.ensureUtf8Bytes]).
* Prefer [GhostJsonFlatReader.captureRawJsonBytes] when starting from a [ByteArray] source.
*/
fun GhostJsonStringReader.captureRawJsonBytes(): ByteArray {
skipWhitespace()
val start = position
captureStringReaderValueBytes()
nextTokenByte = C.RESET_TOKEN_BYTE
return sliceUtf8Bytes(start, position)
}
private fun GhostJsonStringReader.captureStringReaderValueBytes() {
val chars = rawChars
val localLimit = limit
val first = chars[position++].code
when (first) {
C.OPEN_OBJ_INT, C.OPEN_ARR_INT -> {
var depth = 1
while (position < localLimit && depth > 0) {
when (chars[position++].code) {
C.QUOTE_INT -> captureStringReaderSkipString(chars, localLimit)
C.OPEN_OBJ_INT, C.OPEN_ARR_INT -> depth++
C.CLOSE_OBJ_INT, C.CLOSE_ARR_INT -> depth--
}
}
}
C.QUOTE_INT -> captureStringReaderSkipString(chars, localLimit)
C.TRUE_CHAR_INT -> position += 3
C.FALSE_CHAR_INT -> position += 4
C.NULL_CHAR_INT -> position += 3
else -> {
while (position < localLimit) {
val b = chars[position].code
if (b == C.COMMA_INT || b == C.CLOSE_OBJ_INT || b == C.CLOSE_ARR_INT || b <= C.SPACE_INT) break
position++
}
}
}
}
private fun GhostJsonStringReader.captureStringReaderSkipString(chars: CharArray, localLimit: Int) {
while (position < localLimit) {
when (chars[position++].code) {
C.QUOTE_INT -> return
C.BACKSLASH_INT -> if (position < localLimit) position++
}
}
}