diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 57fb3a4074108a2ec29f20cf4f4abd3b1945d414..548ef8ccce4eb635af1204a590a6197951f429f2 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -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( diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index a876b20b68ed92577fc4e78c36c38c622a067af8..b45d0f61f0092ac5dfef9d2cf4766c7efce71fc4 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -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; + }, }; }