Skip to content

Commit

Permalink
Fix regression in char printing
Browse files Browse the repository at this point in the history
Closes #4014
  • Loading branch information
LemonBoy authored and andrewrk committed Jan 16, 2020
1 parent 0267afa commit 7e5e767
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 1 addition & 3 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,7 @@ pub fn formatAsciiChar(
comptime Errors: type,
output: fn (@TypeOf(context), []const u8) Errors!void,
) Errors!void {
if (std.ascii.isPrint(c))
return output(context, @as(*const [1]u8, &c)[0..]);
return format(context, Errors, output, "\\x{x:0<2}", .{c});
return output(context, @as(*const [1]u8, &c)[0..]);
}

pub fn formatBuf(
Expand Down
9 changes: 8 additions & 1 deletion src-self-hosted/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,14 @@ fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {
'\n' => "\\n"[0..],
'\r' => "\\r"[0..],
'\t' => "\\t"[0..],
else => std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable,
else => {
// Handle the remaining escapes Zig doesn't support by turning them
// into their respective hex representation
if (std.ascii.isCntrl(c))
return std.fmt.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable
else
return std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;
},
};
}

Expand Down

0 comments on commit 7e5e767

Please sign in to comment.