Files
ziran/std/json_scan.zi
T
waozi 4838ce9b2f Improve runtimes, add an experimental direct backend, and prune Ziran tests
Prepare short portable helpers and scalar record reads, improve JSON SIMD scanning, expose one-line execution, and publish qualified Alpha benchmark artifacts. Add the experimental verified lowered execution, serialization and Linux x86-64 backend with source maps.

Physically remove 263 redundant test entry points and unused helpers, migrate retained helpers to Ziran, and execute the full retained suite without sharding. Allow ignored backup state for the managed physical Alpha workflow.

Local validation: all 268 retained tests passed across the full run and corrected targeted reruns; bootstrap matches. Current assembly verifier, x86-64, executor and serializer tests passed. Website examples and all 4661 local links passed. Six Alpha JSON/text workloads retain verified artifacts, raw samples and explicit unsupported Wasm diagnostics. The mobile sub-500 ns target remains pending.
2026-11-11 18:46:34 -03:00

567 lines
22 KiB
Ziran

#import "text"
#import "vec"
JsonBytes :: #import "json_bytes"
// A small allocation-free JSON scanner. Positions are UTF-8 byte offsets.
// String spans retain escapes; member lookup matches unescaped ASCII names.
JsonSpan :: struct {
start: s32
end: s32
after: s32
valid: bool
}
JsonNumber :: struct {
value: float64
after: s32
valid: bool
}
// A complete JSON value, including string quotes or container delimiters.
// The range borrows its document; end is the first byte after the value.
JsonRange :: struct { start: s32; end: s32 }
// Owned decoded bytes. The caller may consume value with BuilderFinish or
// let normal vector cleanup release it; valid distinguishes an empty string
// from malformed input or a decoded value exceeding the requested limit.
JsonText :: struct { value: Vec(u8); valid: bool }
#program_export
Space :: (text: string, at: s32) -> s32 {
cursor: s32 = at
while cursor >= 0 && cursor < (cast(s32)text.count) {
ch: u8 = text[cursor]
if ch != cast(u8)32 && ch != cast(u8)9 && ch != cast(u8)10 && ch != cast(u8)13 {
break
}
cursor = cursor + 1
}
return cursor
}
#program_export
Digit :: (ch: u8) -> bool {
return ch >= cast(u8)48 && ch <= cast(u8)57
}
#program_export
Hex :: (ch: u8) -> bool {
return Digit(ch) || (ch >= cast(u8)65 && ch <= cast(u8)70) ||
(ch >= cast(u8)97 && ch <= cast(u8)102)
}
#program_export
ReadString :: (text: string, at: s32) -> JsonSpan {
result: JsonSpan
if text.count > 2147483647 || at < 0 || at >= (cast(s32)text.count) || text[at] != cast(u8)34 { return result }
result.start = at + 1
cursor: s32 = cast(s32)JsonBytes.JsonStringRunEnd(text, result.start)
while cursor < (cast(s32)text.count) {
ch: u8 = text[cursor]
if ch == cast(u8)34 {
result.end = cursor
result.after = cursor + 1
result.valid = true
return result
}
if ch < cast(u8)32 { return result }
if ch == cast(u8)92 {
cursor = cursor + 1
if cursor >= (cast(s32)text.count) { return result }
escaped: u8 = text[cursor]
if escaped == cast(u8)117 {
index: s32 = 0
while index < 4 {
cursor = cursor + 1
if cursor >= (cast(s32)text.count) || !Hex(text[cursor]) { return result }
index = index + 1
}
} else if escaped != cast(u8)34 && escaped != cast(u8)92 &&
escaped != cast(u8)47 && escaped != cast(u8)98 &&
escaped != cast(u8)102 && escaped != cast(u8)110 &&
escaped != cast(u8)114 && escaped != cast(u8)116 {
return result
}
}
cursor = cast(s32)JsonBytes.JsonStringRunEnd(text, cast(s64)cursor + 1)
}
return result
}
HexValue :: (ch: u8) -> s32 {
if ch >= cast(u8)48 && ch <= cast(u8)57 { return cast(s32)ch - 48 }
if ch >= cast(u8)97 && ch <= cast(u8)102 { return cast(s32)ch - 87 }
if ch >= cast(u8)65 && ch <= cast(u8)70 { return cast(s32)ch - 55 }
return -1
}
// The four hex digits at `at` as a number, or -1.
ReadHex4 :: (text: string, at: s32, end: s32) -> s32 {
if at < 0 || at > end - 4 { return -1 }
value: s32 = 0
index: s32 = 0
while index < 4 {
digit: s32 = HexValue(text[at + index])
if digit < 0 { return -1 }
value = value * 16 + digit
index = index + 1
}
return value
}
// Appends codepoint as UTF-8, leaving room for a NUL. Returns the new length,
// or -1 when it does not fit.
AppendUtf8 :: (output: []u8, length: s32, codepoint: s32) -> s32 {
size: s32 = 1
if codepoint >= 128 { size = 2 }
if codepoint >= 2048 { size = 3 }
if codepoint >= 65536 { size = 4 }
if length < 0 || length + size + 1 > (cast(s32)output.count) { return -1 }
if size == 1 {
output[length] = cast(u8)codepoint
} else if size == 2 {
output[length] = cast(u8)(192 + codepoint / 64)
output[length + 1] = cast(u8)(128 + codepoint % 64)
} else if size == 3 {
output[length] = cast(u8)(224 + codepoint / 4096)
output[length + 1] = cast(u8)(128 + (codepoint / 64) % 64)
output[length + 2] = cast(u8)(128 + codepoint % 64)
} else {
output[length] = cast(u8)(240 + codepoint / 262144)
output[length + 1] = cast(u8)(128 + (codepoint / 4096) % 64)
output[length + 2] = cast(u8)(128 + (codepoint / 64) % 64)
output[length + 3] = cast(u8)(128 + codepoint % 64)
}
return length + size
}
// Copies the JSON string at `at` into output as UTF-8 with its escapes
// decoded, including \uXXXX surrogate pairs; a lone surrogate becomes
// U+FFFD. Returns the byte length, or -1 when the value is not a string or
// does not fit. Output is always NUL-terminated.
#program_export
ReadStringText :: (text: string, at: s32, output: []u8) -> s32 {
if output.count == 0 { return -1 }
output[0] = cast(u8)0
end := cast(s32)text.count
if at < 0 || at >= end || text[at] != cast(u8)34 { return -1 }
length: s32 = 0
cursor: s32 = at + 1
while cursor < end && length >= 0 {
ch: u8 = text[cursor]
if ch == cast(u8)34 { output[length] = cast(u8)0; return length }
if ch < cast(u8)32 { break }
codepoint: s32 = cast(s32)ch
if ch == cast(u8)92 {
if cursor + 1 >= end { break }
escaped: u8 = text[cursor + 1]
if escaped != cast(u8)34 && escaped != cast(u8)92 && escaped != cast(u8)47 &&
escaped != cast(u8)98 && escaped != cast(u8)102 && escaped != cast(u8)110 &&
escaped != cast(u8)114 && escaped != cast(u8)116 && escaped != cast(u8)117 { break }
cursor = cursor + 2
codepoint = cast(s32)escaped
if escaped == cast(u8)98 { codepoint = 8 }
if escaped == cast(u8)102 { codepoint = 12 }
if escaped == cast(u8)110 { codepoint = 10 }
if escaped == cast(u8)114 { codepoint = 13 }
if escaped == cast(u8)116 { codepoint = 9 }
if escaped == cast(u8)117 {
codepoint = ReadHex4(text, cursor, end)
if codepoint < 0 { break }
cursor = cursor + 4
if codepoint >= 55296 && codepoint <= 56319 &&
cursor + 6 <= end && text[cursor] == cast(u8)92 &&
text[cursor + 1] == cast(u8)117 {
low: s32 = ReadHex4(text, cursor + 2, end)
if low >= 56320 && low <= 57343 {
codepoint = 65536 + (codepoint - 55296) * 1024 + (low - 56320)
cursor = cursor + 6
}
}
if codepoint >= 55296 && codepoint <= 57343 { codepoint = 65533 }
}
length = AppendUtf8(output, length, codepoint)
} else {
// Validate and locate a run once, then copy its raw UTF-8 bytes.
// The decoder owns validation; an earlier ReadString pass would
// scan every byte again before reaching this same run.
start: s32 = cursor
while cursor < end {
byte := text[cursor]
if byte == cast(u8)34 || byte == cast(u8)92 || byte < cast(u8)32 { break }
cursor += 1
}
size: s32 = cursor - start
if size >= cast(s32)output.count - length { length = -1 }
else {
for i: 0..size - 1 { output[length + i] = text[start + i] }
length += size
}
}
}
output[0] = cast(u8)0
return -1
}
// Decode into owned storage without a maximum-size temporary buffer. Runs of
// ordinary UTF-8 append directly; escape fragments need at most four bytes.
// max_bytes bounds decoded bytes, not the serialized span (escapes shrink).
#program_export
ReadStringValue :: (text: string, at: s32, max_bytes: s32) -> JsonText {
span := ReadString(text, at)
if !span.valid || max_bytes < 0 { return .{} }
out: Vec(u8); cursor := span.start; start := cursor
while cursor < span.end {
if text[cursor] != cast(u8)92 { cursor += 1; continue }
if out.count + cursor - start > max_bytes || !BuilderAppend(out, text[start:cursor]) { return .{} }
escaped := text[cursor + 1]; cursor += 2
codepoint := cast(s32)escaped
if escaped == cast(u8)98 { codepoint = 8 }
if escaped == cast(u8)102 { codepoint = 12 }
if escaped == cast(u8)110 { codepoint = 10 }
if escaped == cast(u8)114 { codepoint = 13 }
if escaped == cast(u8)116 { codepoint = 9 }
if escaped == cast(u8)117 {
codepoint = ReadHex4(text, cursor, span.end); cursor += 4
if codepoint >= 55296 && codepoint <= 56319 && cursor + 6 <= span.end &&
text[cursor] == cast(u8)92 && text[cursor + 1] == cast(u8)117 {
low := ReadHex4(text, cursor + 2, span.end)
if low >= 56320 && low <= 57343 {
codepoint = 65536 + (codepoint - 55296) * 1024 + low - 56320; cursor += 6
}
}
if codepoint >= 55296 && codepoint <= 57343 { codepoint = 65533 }
}
encoded: [5]u8; size := AppendUtf8(encoded[:], 0, codepoint)
if size < 0 || out.count + size > max_bytes || !BuilderAppend(out, TextView(encoded[:size])) { return .{} }
start = cursor
}
if out.count + span.end - start > max_bytes || !BuilderAppend(out, text[start:span.end]) { return .{} }
return .{out, true}
}
#program_export
SpanEqualsASCII :: (text: string, span: JsonSpan, expected: string) -> bool {
if !span.valid || span.end - span.start != (cast(s32)expected.count) { return false }
index: s32 = 0
while index < (cast(s32)expected.count) {
if text[span.start + index] != expected[index] { return false }
index = index + 1
}
return true
}
#program_export
SpanContainsFoldASCII :: (text: string, span: JsonSpan,
needle: string) -> bool {
if !span.valid { return false }
if (cast(s32)needle.count) == 0 { return true }
if (cast(s32)needle.count) > span.end - span.start { return false }
start: s32 = span.start
while start <= span.end - (cast(s32)needle.count) {
index: s32 = 0
same: bool = true
while index < (cast(s32)needle.count) {
if LowerASCII(text[start + index]) != LowerASCII(needle[index]) {
same = false
break
}
index = index + 1
}
if same { return true }
start = start + 1
}
return false
}
#program_export
ReadNumber :: (text: string, at: s32) -> JsonNumber {
result: JsonNumber
cursor: s32 = at
if cursor < 0 || cursor >= (cast(s32)text.count) { return result }
negative: bool = false
if text[cursor] == cast(u8)45 {
negative = true
cursor = cursor + 1
if cursor >= (cast(s32)text.count) { return result }
}
if text[cursor] == cast(u8)48 {
cursor = cursor + 1
if cursor < (cast(s32)text.count) && Digit(text[cursor]) { return result }
} else {
if text[cursor] < cast(u8)49 || text[cursor] > cast(u8)57 { return result }
while cursor < (cast(s32)text.count) && Digit(text[cursor]) {
result.value = result.value * 10.0 + cast(float64)(text[cursor] - cast(u8)48)
cursor = cursor + 1
}
}
if cursor < (cast(s32)text.count) && text[cursor] == cast(u8)46 {
cursor = cursor + 1
if cursor >= (cast(s32)text.count) || !Digit(text[cursor]) { return result }
scale: float64 = 0.1
while cursor < (cast(s32)text.count) && Digit(text[cursor]) {
result.value = result.value +
cast(float64)(text[cursor] - cast(u8)48) * scale
scale = scale * 0.1
cursor = cursor + 1
}
}
if cursor < (cast(s32)text.count) && (text[cursor] == cast(u8)101 || text[cursor] == cast(u8)69) {
cursor = cursor + 1
exponent_negative: bool = false
if cursor < (cast(s32)text.count) && (text[cursor] == cast(u8)45 || text[cursor] == cast(u8)43) {
exponent_negative = text[cursor] == cast(u8)45
cursor = cursor + 1
}
if cursor >= (cast(s32)text.count) || !Digit(text[cursor]) { return result }
exponent: s32 = 0
while cursor < (cast(s32)text.count) && Digit(text[cursor]) {
if exponent > 30 { return result }
exponent = exponent * 10 + cast(s32)(text[cursor] - cast(u8)48)
if exponent > 308 { return result }
cursor = cursor + 1
}
while exponent > 0 {
if exponent_negative {
result.value = result.value / 10.0
} else {
result.value = result.value * 10.0
}
exponent = exponent - 1
}
}
// JSON has no infinities; a magnitude that overflowed float64 in the
// digit or exponent loops is rejected rather than handed out as valid.
if result.value > 1.7976931348623157e308 { return result }
if negative { result.value = -result.value }
result.after = cursor
result.valid = true
return result
}
#program_export
MatchLiteral :: (text: string, at: s32, literal: string) -> bool {
if at < 0 || (cast(s32)literal.count) > (cast(s32)text.count) - at { return false }
index: s32 = 0
while index < (cast(s32)literal.count) {
if text[at + index] != literal[index] { return false }
index = index + 1
}
return true
}
#program_export
ValueEnd :: (text: string, at: s32, depth: s32) -> s32 {
cursor: s32 = Space(text, at)
if depth > 64 || cursor < 0 || cursor >= (cast(s32)text.count) { return -1 }
ch: u8 = text[cursor]
if ch == cast(u8)34 {
span: JsonSpan = ReadString(text, cursor)
if span.valid { return span.after }
return -1
}
if ch == cast(u8)123 || ch == cast(u8)91 {
object: bool = ch == cast(u8)123
close: u8 = cast(u8)93
if object { close = cast(u8)125 }
cursor = Space(text, cursor + 1)
if cursor < (cast(s32)text.count) && text[cursor] == close { return cursor + 1 }
while cursor < (cast(s32)text.count) {
if object {
key: JsonSpan = ReadString(text, cursor)
if !key.valid { return -1 }
cursor = Space(text, key.after)
if cursor >= (cast(s32)text.count) || text[cursor] != cast(u8)58 { return -1 }
cursor = cursor + 1
}
cursor = ValueEnd(text, cursor, depth + 1)
if cursor < 0 { return -1 }
cursor = Space(text, cursor)
if cursor >= (cast(s32)text.count) { return -1 }
if text[cursor] == close { return cursor + 1 }
if text[cursor] != cast(u8)44 { return -1 }
cursor = Space(text, cursor + 1)
}
return -1
}
if MatchLiteral(text, cursor, "true") { return cursor + 4 }
if MatchLiteral(text, cursor, "false") { return cursor + 5 }
if MatchLiteral(text, cursor, "null") { return cursor + 4 }
number: JsonNumber = ReadNumber(text, cursor)
if number.valid { return number.after }
return -1
}
#program_export
FieldAt :: (text: string, object_at: s32, name: string) -> s32 {
cursor: s32 = Space(text, object_at)
if cursor < 0 || cursor >= (cast(s32)text.count) || text[cursor] != cast(u8)123 {
return -1
}
cursor = Space(text, cursor + 1)
while cursor < (cast(s32)text.count) && text[cursor] != cast(u8)125 {
key: JsonSpan = ReadString(text, cursor)
if !key.valid { return -1 }
cursor = Space(text, key.after)
if cursor >= (cast(s32)text.count) || text[cursor] != cast(u8)58 { return -1 }
cursor = Space(text, cursor + 1)
if SpanEqualsASCII(text, key, name) { return cursor }
cursor = ValueEnd(text, cursor, 0)
if cursor < 0 { return -1 }
cursor = Space(text, cursor)
if cursor >= (cast(s32)text.count) || text[cursor] != cast(u8)44 { return -1 }
cursor = Space(text, cursor + 1)
}
return -1
}
// Index several unescaped ASCII member names in one validated pass. Missing
// members receive -1; duplicate keys keep their first value, like FieldAt.
// Positions borrow the current text and are usable only when this returns true.
// The output must have room for every requested name; extra cells are untouched.
#program_export
ObjectPositions :: (text: string, object_at: s32, names: []string,
output: []s32) -> bool {
if output.count < names.count { return false }
for i: 0..cast(s32)names.count - 1 { output[i] = -1 }
cursor: s32 = Space(text, object_at)
if cursor < 0 || cursor >= cast(s32)text.count || text[cursor] != cast(u8)123 { return false }
cursor = Space(text, cursor + 1)
if cursor < cast(s32)text.count && text[cursor] == cast(u8)125 { return true }
while cursor < cast(s32)text.count {
key: JsonSpan = ReadString(text, cursor)
if !key.valid { return false }
cursor = Space(text, key.after)
if cursor >= cast(s32)text.count || text[cursor] != cast(u8)58 { return false }
cursor = Space(text, cursor + 1)
for i: 0..cast(s32)names.count - 1 {
if output[i] < 0 && SpanEqualsASCII(text, key, names[i]) { output[i] = cursor }
}
cursor = ValueEnd(text, cursor, 0)
if cursor < 0 { return false }
cursor = Space(text, cursor)
if cursor >= cast(s32)text.count { return false }
if text[cursor] == cast(u8)125 { return true }
if text[cursor] != cast(u8)44 { return false }
cursor = Space(text, cursor + 1)
}
return false
}
#program_export
ElementAt :: (text: string, array_at: s32, index: s32) -> s32 {
cursor: s32 = Space(text, array_at)
if cursor < 0 || cursor >= (cast(s32)text.count) || text[cursor] != cast(u8)91 || index < 0 {
return -1
}
cursor = Space(text, cursor + 1)
count: s32 = 0
while cursor < (cast(s32)text.count) && text[cursor] != cast(u8)93 {
if count == index { return cursor }
cursor = ValueEnd(text, cursor, 0)
if cursor < 0 { return -1 }
cursor = Space(text, cursor)
if cursor >= (cast(s32)text.count) || text[cursor] != cast(u8)44 { return -1 }
cursor = Space(text, cursor + 1)
count = count + 1
}
return -1
}
// Counts and validates an array while recording the first output.count element
// offsets. Returns the complete count, even when output is shorter, or -1 for
// malformed input. Offsets are borrowed positions, never cached string storage;
// callers use them only after success and clamp access to their buffer length.
#program_export
ArrayPositions :: (text: string, array_at: s32, output: []s32) -> s32 {
ranges: []JsonRange
previous: []JsonRange
return WalkArray(text, array_at, output, ranges, "", previous)
}
// Counts and validates the complete array, retaining the first output.count
// value ranges. Compare or reuse values only after a nonnegative return;
// ranges are byte offsets and do not own the underlying document.
#program_export
ArrayRanges :: (text: string, array_at: s32, output: []JsonRange) -> s32 {
positions: []s32
previous: []JsonRange
return WalkArray(text, array_at, positions, output, "", previous)
}
// Like ArrayRanges, using ranges from a successful scan of previous_text.
// The caller must preserve those bytes and ranges until this call returns.
// Only complete, byte-identical values reuse validation; changed values and
// uncached trailing elements are parsed normally. Array delimiters are always
// checked, including when the output slice is empty or shorter than the array.
// Output must not alias previous_ranges, whose offsets belong to the old text.
#program_export
ArrayRangesReuse :: (text: string, array_at: s32, output: []JsonRange,
previous_text: string, previous_ranges: []JsonRange) -> s32 {
positions: []s32
return WalkArray(text, array_at, positions, output, previous_text, previous_ranges)
}
WalkArray :: (text: string, array_at: s32, positions: []s32,
ranges: []JsonRange, previous_text: string,
previous_ranges: []JsonRange) -> s32 {
cursor: s32 = Space(text, array_at)
if cursor < 0 || cursor >= (cast(s32)text.count) ||
text[cursor] != cast(u8)91 {
return -1
}
cursor = Space(text, cursor + 1)
if cursor < (cast(s32)text.count) && text[cursor] == cast(u8)93 {
return 0
}
count: s32 = 0
while cursor < (cast(s32)text.count) {
start := cursor
if count < cast(s32)positions.count { positions[count] = start }
after: s32 = -1
if count < cast(s32)previous_ranges.count {
old := previous_ranges[count]
if old.start >= 0 && old.end > old.start && old.end <= cast(s32)previous_text.count {
size := old.end - old.start
if size <= cast(s32)text.count - start &&
text[start:start + size] == previous_text[old.start:old.end] {
boundary := Space(text, start + size)
if boundary < cast(s32)text.count &&
(text[boundary] == cast(u8)44 || text[boundary] == cast(u8)93) {
after = start + size
}
}
}
}
// A matching prefix is not a complete value: 1 may have become 12.
cursor = ifx after >= 0 then after else ValueEnd(text, cursor, 0)
if cursor < 0 {
return -1
}
if count < cast(s32)ranges.count { ranges[count] = .{start, cursor} }
count += 1
cursor = Space(text, cursor)
if cursor >= (cast(s32)text.count) {
return -1
}
if text[cursor] == cast(u8)93 {
return count
}
if text[cursor] != cast(u8)44 {
return -1
}
cursor = Space(text, cursor + 1)
if cursor >= (cast(s32)text.count) || text[cursor] == cast(u8)93 {
return -1
}
}
return -1
}
#program_export
ArrayCount :: (text: string, array_at: s32) -> s32 {
positions: []s32
return ArrayPositions(text, array_at, positions)
}