Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

escapeUnprintables

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.

MacroTranslator.escapeUnprintables
fn escapeUnprintables(mt: *MacroTranslator) ![]const u8

File

lib/compiler/translate-c/MacroTranslator.zig:576

Code

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,
    };
}