Repository navigation
refactor(json): share wire string writer path - #43
Merged
Merged
Conversation
`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`.
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.
writeWireStringpasses each byte toescapeCp, which wraps the outputlist in a new
std.Io.Writer.Allocatingfor that one byte.writeWireStringnow wraps the list once and callswriteEscapedCpTofor each byte, and the private
escapeCpgoes away. Each byte is stillescaped as the code point of the same value, so the output bytes stay
the same.
The extended test
wire strings are one byte per code pointinsrc/json.zigruns a table of inputs and adds control bytes next to thehigh bytes it checked before. Each row checks the written JSON and the
round trip through
decodeWire.