Skip to content

feat(json): add allocation-free cache string writer - #42

Merged
HaoZeke merged 1 commit into
mainfrom
feat/json-cache-writer
Nov 19, 2026
Merged

HaoZeke merged 1 commit into
mainfrom
feat/json-cache-writer

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

json.writeCacheString appends to an ArrayList through an allocator,
so a caller on a crash or out-of-memory path cannot use it to write
cache-style JSON strings. json.writeCacheStringTo writes the same
bytes to any std.Io.Writer and allocates nothing. The escape switch
moves into writeEscapedCpTo with its arms unchanged.
writeCacheString and escapeCp wrap the caller's list in a
std.Io.Writer.Allocating and call the writer path, so cache escaping
keeps one loop.

test "cache writer parity" {
    const sample = "a\"\xff\u{1F600}\n\x01";
    var out: std.ArrayList(u8) = .empty;
    defer out.deinit(std.testing.allocator);
    try json.writeCacheString(&out, std.testing.allocator, sample);
    var buf: [256]u8 = undefined;
    var w: std.Io.Writer = .fixed(&buf);
    try json.writeCacheStringTo(&w, sample);
    try std.testing.expectEqualStrings(out.items, buf[0..w.end]);
}
// Before: writeCacheStringTo does not exist.
// After: passes; both give "a\"\udcff\ud83d\ude00\n\u0001".

The new test
writeCacheStringTo matches writeCacheString over invalid, astral, and control bytes
in src/json.zig compares both paths on that sample. The new test
writeCacheStringTo reports WriteFailed for a 64-byte fixed writer
checks the error from a writer that runs out of room.

`json.writeCacheString` appends to an `ArrayList` through an allocator,
so a caller on a crash or out-of-memory path cannot use it to write
cache-style JSON strings. `json.writeCacheStringTo` writes the same
bytes to any `std.Io.Writer` and allocates nothing. The escape switch
moves into `writeEscapedCpTo` with its arms unchanged.
`writeCacheString` and `escapeCp` wrap the caller's list in a
`std.Io.Writer.Allocating` and call the writer path, so cache escaping
keeps one loop.

```zig
test "cache writer parity" {
    const sample = "a\"\xff\u{1F600}\n\x01";
    var out: std.ArrayList(u8) = .empty;
    defer out.deinit(std.testing.allocator);
    try json.writeCacheString(&out, std.testing.allocator, sample);
    var buf: [256]u8 = undefined;
    var w: std.Io.Writer = .fixed(&buf);
    try json.writeCacheStringTo(&w, sample);
    try std.testing.expectEqualStrings(out.items, buf[0..w.end]);
}
// Before: writeCacheStringTo does not exist.
// After: passes; both give "a\"\udcff\ud83d\ude00\n\u0001".
```

The new test
`writeCacheStringTo matches writeCacheString over invalid, astral, and control bytes`
in `src/json.zig` compares both paths on that sample. The new test
`writeCacheStringTo reports WriteFailed for a 64-byte fixed writer`
checks the error from a writer that runs out of room.
@HaoZeke
HaoZeke merged commit ae80e10 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