From 87316a2f86ee9b85756297d588c7e24b3c010c03 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Thu, 19 Nov 2026 04:21:07 +0000 Subject: [PATCH] feat(json): add allocation-free cache string writer `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. --- .../+json-write-cache-string-to.feature.md | 1 + src/json.zig | 65 ++++++++++++++----- 2 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 changelog.d/+json-write-cache-string-to.feature.md diff --git a/changelog.d/+json-write-cache-string-to.feature.md b/changelog.d/+json-write-cache-string-to.feature.md new file mode 100644 index 0000000..3d3f8be --- /dev/null +++ b/changelog.d/+json-write-cache-string-to.feature.md @@ -0,0 +1 @@ +`json.writeCacheStringTo` writes cache-style JSON strings to a writer without allocation. diff --git a/src/json.zig b/src/json.zig index cb4c014..0d8962e 100644 --- a/src/json.zig +++ b/src/json.zig @@ -356,39 +356,53 @@ pub fn decodeWire(arena: Allocator, raw: []const u8) DecodeError![]u8 { } fn escapeCp(out: *std.ArrayList(u8), gpa: Allocator, cp: u21) Allocator.Error!void { + var aw: std.Io.Writer.Allocating = .fromArrayList(gpa, out); + defer out.* = aw.toArrayList(); + writeEscapedCpTo(&aw.writer, cp) catch return error.OutOfMemory; +} + +fn writeEscapedCpTo(w: *std.Io.Writer, cp: u21) std.Io.Writer.Error!void { switch (cp) { - '"' => try out.appendSlice(gpa, "\\\""), - '\\' => try out.appendSlice(gpa, "\\\\"), - '\n' => try out.appendSlice(gpa, "\\n"), - '\r' => try out.appendSlice(gpa, "\\r"), - '\t' => try out.appendSlice(gpa, "\\t"), - 0x08 => try out.appendSlice(gpa, "\\b"), - 0x0c => try out.appendSlice(gpa, "\\f"), - ' '...'!', '#'...'[', ']'...'~' => try out.append(gpa, @intCast(cp)), + '"' => try w.writeAll("\\\""), + '\\' => try w.writeAll("\\\\"), + '\n' => try w.writeAll("\\n"), + '\r' => try w.writeAll("\\r"), + '\t' => try w.writeAll("\\t"), + 0x08 => try w.writeAll("\\b"), + 0x0c => try w.writeAll("\\f"), + ' '...'!', '#'...'[', ']'...'~' => try w.writeByte(@intCast(cp)), else => { var b: [12]u8 = undefined; if (cp >= 0x10000) { const v = cp - 0x10000; const s = std.mem.print(&b, "\\u{x:0>4}\\u{x:0>4}", .{ 0xD800 + (v >> 10), 0xDC00 + (v & 0x3FF) }) catch unreachable; - try out.appendSlice(gpa, s); + try w.writeAll(s); } else { const s = std.mem.print(&b, "\\u{x:0>4}", .{cp}) catch unreachable; - try out.appendSlice(gpa, s); + try w.writeAll(s); } }, } } -/// A JSON string of `bytes` as the Python guard wrote cache text. -pub fn writeCacheString(out: *std.ArrayList(u8), gpa: Allocator, bytes: []const u8) Allocator.Error!void { - try out.append(gpa, '"'); +/// A JSON string of `bytes` as the Python guard wrote cache text, written to +/// `w` without allocating. +pub fn writeCacheStringTo(w: *std.Io.Writer, bytes: []const u8) std.Io.Writer.Error!void { + try w.writeByte('"'); var i: usize = 0; while (i < bytes.len) { const ch = text.nextChar(bytes[i..]); - if (ch.cp) |cp| try escapeCp(out, gpa, cp) else try escapeCp(out, gpa, 0xDC00 + @as(u21, bytes[i])); + if (ch.cp) |cp| try writeEscapedCpTo(w, cp) else try writeEscapedCpTo(w, 0xDC00 + @as(u21, bytes[i])); i += ch.len; } - try out.append(gpa, '"'); + try w.writeByte('"'); +} + +/// A JSON string of `bytes` as the Python guard wrote cache text. +pub fn writeCacheString(out: *std.ArrayList(u8), gpa: Allocator, bytes: []const u8) Allocator.Error!void { + var aw: std.Io.Writer.Allocating = .fromArrayList(gpa, out); + defer out.* = aw.toArrayList(); + writeCacheStringTo(&aw.writer, bytes) catch return error.OutOfMemory; } /// A JSON string of `bytes` as wire text: one code point per byte. @@ -429,6 +443,27 @@ test "wire strings are one byte per code point" { try std.testing.expectError(error.Encoding, decodeWire(a, "\\u0100")); } +test "writeCacheStringTo matches writeCacheString over invalid, astral, and control bytes" { + const gpa = std.testing.allocator; + const sample = "a\"\xff\u{1F600}\n\x01"; + + var list: std.ArrayList(u8) = .empty; + defer list.deinit(gpa); + try writeCacheString(&list, gpa, sample); + + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try writeCacheStringTo(&w, sample); + try std.testing.expectEqualStrings(list.items, buf[0..w.end]); +} + +test "writeCacheStringTo reports WriteFailed for a 64-byte fixed writer" { + const long = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + var buf: [64]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try std.testing.expectError(error.WriteFailed, writeCacheStringTo(&w, long)); +} + test "a truncated or malformed escape is an encoding error" { const gpa = std.testing.allocator; for ([_][]const u8{ "abc\\", "\\u12", "\\ud83d\\ude0", "\\u+123", "\\x0041" }) |raw| {