Skip to content

Commit 7e5e767

Browse files
LemonBoyandrewrk
authored andcommitted
Fix regression in char printing
Closes #4014
1 parent 0267afa commit 7e5e767

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/std/fmt.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,7 @@ pub fn formatAsciiChar(
585585
comptime Errors: type,
586586
output: fn (@TypeOf(context), []const u8) Errors!void,
587587
) Errors!void {
588-
if (std.ascii.isPrint(c))
589-
return output(context, @as(*const [1]u8, &c)[0..]);
590-
return format(context, Errors, output, "\\x{x:0<2}", .{c});
588+
return output(context, @as(*const [1]u8, &c)[0..]);
591589
}
592590

593591
pub fn formatBuf(

src-self-hosted/translate_c.zig

+8-1
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,14 @@ fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {
17081708
'\n' => "\\n"[0..],
17091709
'\r' => "\\r"[0..],
17101710
'\t' => "\\t"[0..],
1711-
else => std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable,
1711+
else => {
1712+
// Handle the remaining escapes Zig doesn't support by turning them
1713+
// into their respective hex representation
1714+
if (std.ascii.isCntrl(c))
1715+
return std.fmt.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable
1716+
else
1717+
return std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;
1718+
},
17121719
};
17131720
}
17141721

0 commit comments

Comments
 (0)