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 {
538538}
539539
540540/// 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.
541547pub fn stringEscape(bytes: []const u8, w: *Writer) Writer.Error!void {
542548 for (bytes) |byte| switch (byte) {
549 '\t' => try w.writeAll("\\t"),
543550 '\n' => try w.writeAll("\\n"),
544551 '\r' => try w.writeAll("\\r"),
545 '\t' => try w.writeAll("\\t"),
546552 '\\' => try w.writeAll("\\\\"),
547553 '"' => try w.writeAll("\\\""),
548 ' ', '!', '#'...'[', ']'...'~' => try w.writeByte(byte),
549 else => {
554 0...8, 11, 12, 14...0x1f, 0x7f => {
550555 try w.writeAll("\\x");
551556 try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });
552557 },
558 else => try w.writeByte(byte),
553559 };
554560}
555561