From f37a9103562116a19c21e3f2f392b9217fca63ab Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 20 Aug 2026 15:49:41 -0700 Subject: [PATCH] std.zig.stringEscape: relax the escaping rules pass through everything except characters that have escapes in zig language and ascii control characters. --- lib/std/zig.zig | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/std/zig.zig b/lib/std/zig.zig index 81baffb36bc70dd6551b4acfd360d2a811db2629..6f060d0550047ec569ccb14ef12330f7db45ffc6 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -538,18 +538,24 @@ test fmtChar { } /// Print the string as escaped contents of a double quoted string. +/// +/// The following transformations are made: +/// * escaped: '\n', '\r', '\t', '\\', '"' +/// * hex-encoded: ascii control characters +/// +/// Everything else is passed through unmodified. pub fn stringEscape(bytes: []const u8, w: *Writer) Writer.Error!void { for (bytes) |byte| switch (byte) { + '\t' => try w.writeAll("\\t"), '\n' => try w.writeAll("\\n"), '\r' => try w.writeAll("\\r"), - '\t' => try w.writeAll("\\t"), '\\' => try w.writeAll("\\\\"), '"' => try w.writeAll("\\\""), - ' ', '!', '#'...'[', ']'...'~' => try w.writeByte(byte), - else => { + 0...8, 11, 12, 14...0x1f, 0x7f => { try w.writeAll("\\x"); try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' }); }, + else => try w.writeByte(byte), }; } -- 2.54.0