Repository navigation
feat(json): add allocation-free cache string writer - #42
Merged
Merged
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
json.writeCacheStringappends to anArrayListthrough an allocator,so a caller on a crash or out-of-memory path cannot use it to write
cache-style JSON strings.
json.writeCacheStringTowrites the samebytes to any
std.Io.Writerand allocates nothing. The escape switchmoves into
writeEscapedCpTowith its arms unchanged.writeCacheStringandescapeCpwrap the caller's list in astd.Io.Writer.Allocatingand call the writer path, so cache escapingkeeps one loop.
The new test
writeCacheStringTo matches writeCacheString over invalid, astral, and control bytesin
src/json.zigcompares both paths on that sample. The new testwriteCacheStringTo reports WriteFailed for a 64-byte fixed writerchecks the error from a writer that runs out of room.