authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-20 15:49:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-20 17:35:12-07:00
logf37a9103562116a19c21e3f2f392b9217fca63ab
treee73de47bb228095443a803b8bd408200ff45496b
parent7e4d15b0c45858ebd24522676c20fc1cda5306ba

std.zig.stringEscape: relax the escaping rules

pass through everything except characters that have escapes in zig language and ascii control characters.

1 files changed, 9 insertions(+), 3 deletions(-)

lib/std/zig.zig+9-3
...@@ -538,18 +538,24 @@ test fmtChar {...@@ -538,18 +538,24 @@ test fmtChar {
538}538}
539539
540/// Print the string as escaped contents of a double quoted string.540/// Print the string as escaped contents of a double quoted string.
541///
542/// The following transformations are made:
543/// * escaped: '\n', '\r', '\t', '\\', '"'
544/// * hex-encoded: ascii control characters
545///
546/// Everything else is passed through unmodified.
541pub fn stringEscape(bytes: []const u8, w: *Writer) Writer.Error!void {547pub fn stringEscape(bytes: []const u8, w: *Writer) Writer.Error!void {
542 for (bytes) |byte| switch (byte) {548 for (bytes) |byte| switch (byte) {
549 '\t' => try w.writeAll("\\t"),
543 '\n' => try w.writeAll("\\n"),550 '\n' => try w.writeAll("\\n"),
544 '\r' => try w.writeAll("\\r"),551 '\r' => try w.writeAll("\\r"),
545 '\t' => try w.writeAll("\\t"),
546 '\\' => try w.writeAll("\\\\"),552 '\\' => try w.writeAll("\\\\"),
547 '"' => try w.writeAll("\\\""),553 '"' => try w.writeAll("\\\""),
548 ' ', '!', '#'...'[', ']'...'~' => try w.writeByte(byte),554 0...8, 11, 12, 14...0x1f, 0x7f => {
549 else => {
550 try w.writeAll("\\x");555 try w.writeAll("\\x");
551 try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });556 try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });
552 },557 },
558 else => try w.writeByte(byte),
553 };559 };
554}560}
555561