Skip to content

refactor(json): share wire string writer path - #43

Merged
HaoZeke merged 1 commit into
mainfrom
refactor/json-wire-writer
Nov 19, 2026
Merged

HaoZeke merged 1 commit into
mainfrom
refactor/json-wire-writer

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

writeWireString passes each byte to escapeCp, which wraps the output
list in a new std.Io.Writer.Allocating for that one byte.
writeWireString now wraps the list once and calls writeEscapedCpTo
for each byte, and the private escapeCp goes away. Each byte is still
escaped as the code point of the same value, so the output bytes stay
the same.

test "wire output" {
    var out: std.ArrayList(u8) = .empty;
    defer out.deinit(std.testing.allocator);
    try json.writeWireString(&out, std.testing.allocator, "x\xff\x00");
    try std.testing.expectEqualStrings("\"x\\u00ff\\u0000\"", out.items);
}
// Before and after: passes.

The extended test wire strings are one byte per code point in
src/json.zig runs a table of inputs and adds control bytes next to the
high bytes it checked before. Each row checks the written JSON and the
round trip through decodeWire.

`writeWireString` passes each byte to `escapeCp`, which wraps the output
list in a new `std.Io.Writer.Allocating` for that one byte.
`writeWireString` now wraps the list once and calls `writeEscapedCpTo`
for each byte, and the private `escapeCp` goes away. Each byte is still
escaped as the code point of the same value, so the output bytes stay
the same.

```zig
test "wire output" {
    var out: std.ArrayList(u8) = .empty;
    defer out.deinit(std.testing.allocator);
    try json.writeWireString(&out, std.testing.allocator, "x\xff\x00");
    try std.testing.expectEqualStrings("\"x\\u00ff\\u0000\"", out.items);
}
// Before and after: passes.
```

The extended test `wire strings are one byte per code point` in
`src/json.zig` runs a table of inputs and adds control bytes next to the
high bytes it checked before. Each row checks the written JSON and the
round trip through `decodeWire`.
@HaoZeke HaoZeke changed the title feat(json): add allocation-free cache string writer refactor(json): share wire string writer path Nov 19, 2026
@HaoZeke
HaoZeke changed the base branch from feat/json-cache-writer to main November 19, 2026 05:43
@HaoZeke
HaoZeke merged commit ae925a3 into main Nov 19, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant