Files
ziran/std/json_quote.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

74 lines
3.6 KiB
Ziran

// JSON string serialization without a document tree. Appends into owned
// byte vectors; ASCII runs are copied in bulk and UTF-8 escapes preserve the
// document writer semantics on native and portable targets.
using V :: #import "vec"
JsonBytes :: #import "json_bytes"
#scope_file
HEX :: "0123456789abcdef";
Escape :: (output: *Vec(u8), point: s32) -> bool {
if !BuilderAppend(<<output, "\\u") { return false }
if !VecPush(<<output, HEX[(point >> 12) & 15]) { return false }
if !VecPush(<<output, HEX[(point >> 8) & 15]) { return false }
if !VecPush(<<output, HEX[(point >> 4) & 15]) { return false }
if !VecPush(<<output, HEX[point & 15]) { return false }
return true
}
#scope_export
// Appends VALUE as a quoted JSON string, escaped as json.dumps escapes it.
JsonQuoteInto :: (output: *Vec(u8), value: string, ascii: bool) -> bool {
if output == null || output.count > 9223372036854775805 || value.count > (9223372036854775807 - output.count - 2) / 6 ||
!VecReserve(<<output, output.count + 2 + value.count) { return false }
if !VecPush(<<output, #char "\"") { return false }
at: s64 = 0
while at < value.count {
byte := value[at]
if byte>=32 && byte!=34 && byte!=92 && (!ascii||byte<127) {
end:=JsonBytes.JsonQuoteRunEnd(value,at,ascii)
if !BuilderAppend(<<output,value[at:end]) { return false }
at=end; continue
}
if byte == #char "\"" { if !BuilderAppend(<<output, "\\\"") { return false }; at += 1; continue }
if byte == #char "\\" { if !BuilderAppend(<<output, "\\\\") { return false }; at += 1; continue }
if byte == 10 { if !BuilderAppend(<<output, "\\n") { return false }; at += 1; continue }
if byte == 13 { if !BuilderAppend(<<output, "\\r") { return false }; at += 1; continue }
if byte == 9 { if !BuilderAppend(<<output, "\\t") { return false }; at += 1; continue }
if byte == 8 { if !BuilderAppend(<<output, "\\b") { return false }; at += 1; continue }
if byte == 12 { if !BuilderAppend(<<output, "\\f") { return false }; at += 1; continue }
if byte < 32 || (ascii && byte == 127) { if !Escape(output, cast(s32)byte) { return false }; at += 1; continue }
if byte < 128 || !ascii { if !VecPush(<<output, byte) { return false }; at += 1; continue }
// A UTF-8 sequence as one code point; an invalid byte stands alone.
point: s32 = byte
length: s64 = 1
if byte >= 240 && at + 3 < value.count {
point = ((cast(s32)byte & 7) << 18) | ((cast(s32)value[at + 1] & 63) << 12) |
((cast(s32)value[at + 2] & 63) << 6) | (cast(s32)value[at + 3] & 63)
length = 4
} else if byte >= 224 && at + 2 < value.count {
point = ((cast(s32)byte & 15) << 12) | ((cast(s32)value[at + 1] & 63) << 6) | (cast(s32)value[at + 2] & 63)
length = 3
} else if byte >= 192 && at + 1 < value.count {
point = ((cast(s32)byte & 31) << 6) | (cast(s32)value[at + 1] & 63)
length = 2
}
if point >= 65536 {
point -= 65536
if !Escape(output, 55296 + (point >> 10)) { return false }
if !Escape(output, 56320 + (point & 1023)) { return false }
} else {
if !Escape(output, point) { return false }
}
at += length
}
if !VecPush(<<output, #char "\"") { return false }
return true
}
JsonQuote :: (value: string, ascii: bool = true) -> Text {
output: Vec(u8)
if !JsonQuoteInto(*output, value, ascii) { return .{} }
return BuilderFinish(output)
}