Skip to content

Printing whitespace characters with {c} #4014

@lukechampine

Description

@lukechampine

21bc335 changed the behavior of the {c} formatting directive:

-    return output(context, @as(*const [1]u8, &c)[0..]);
+    if (std.ascii.isPrint(c))
+        return output(context, @as(*const [1]u8, &c)[0..]);
+    return format(context, Errors, output, "\\x{x:0<2}", .{c});

The effect of this is:

fn printChars(s: []const u8) void {
    for (s) |c| {
        std.debug.warn("{c}", .{c});
    }
}
// old behavior:
// printChars("foo\tbar") -> foo    bar

// new behavior:
// printChars("foo\tbar") -> foo\x09bar

This is because std.ascii.isPrint returns false on whitespace other than 0x20 ( ).

This behavior does not match the behavior of the %c directive in C, Go, and other languages. Is this departure intentional? If so, what's the proper way to print a string character-by-character?

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions