non-ASCII characters (mt > 127) are also treated as non-printable by fmtSliceEscapeLower. If a C string literal or char literal in a macro is not valid UTF-8, we need to escape non-ASCII characters so that the Zig source we output will itself be UTF-8.
fn escapeUnprintables(mt: *MacroTranslator) ![]const u8
fn escapeUnprintables(mt: *MacroTranslator) ![]const u8 {
const slice = mt.tokSlice();
mt.i += 1;
const zigified = try mt.zigifyEscapeSequences(slice);
if (std.unicode.utf8ValidateSlice(zigified)) return zigified;
const formatter = std.ascii.hexEscape(zigified, .lower);
const encoded_size = @as(usize, @intCast(std.fmt.count("{f}", .{formatter})));
const output = try mt.t.arena.alloc(u8, encoded_size);
return std.fmt.bufPrint(output, "{f}", .{formatter}) catch |err| switch (err) {
error.NoSpaceLeft => unreachable,
else => |e| return e,
};
}