Closed
Labels
standard libraryThis issue involves writing Zig code for the standard library.
Milestone
Description
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\x09barThis 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
Labels
standard libraryThis issue involves writing Zig code for the standard library.
Activity
andrewrk commentedon Dec 31, 2019
I labeled it as a bug, but then I checked the behavior of libc
isprintand it agrees withstd.ascii.isPrint, that\tis not printable. So I'm not yet sure how to resolve this.andrewrk commentedon Dec 31, 2019
cc @Vexu, curious about your thoughts on this.
Vexu commentedon Dec 31, 2019
I think all whitespace with escape sequences (
\t\n,\r) should be printed unescaped. I would've included them in the original change had I realized ascii doesn't consider them printable.andrewrk commentedon Dec 31, 2019
Hmm, how about this proposal:
Change \t, \n, \r to return
trueforstd.ascii.isPrint. This would disagree with what these functions return in libc's isprint. But doesn't that seem like a bug, that they returnfalse? https://en.cppreference.com/w/c/string/byte/isprintthejoshwolfe commentedon Dec 31, 2019
I can't find any specification on what the
%cformat is supposed to do in C or Go. All they say is "the character" or something similarly vague. To me, this means passing through the character without any escaping. There's no mention of "printable" in the%cspecification, unless I'm not looking at the right document. Can someone point me to any specification for the behavior of%cmore detailed than http://www.cplusplus.com/reference/cstdio/printf/ or https://golang.org/pkg/fmt/ ?hryx commentedon Jan 3, 2020
@thejoshwolfe From rummaging around the Go source and package documentation,
%cmeans print everything unescaped, up to the maximum supported codepoint.TheZeus121 commentedon Jan 14, 2020
%c in C outputs the "raw byte" with no checks on whether it's printable or not (which is why you can do terminal escapes with printf).
\t, \n, and \r are considered control characters instead of printable characters because instead of being one character, they do "magic" stuff with terminals usually--\t advances to the next tab stop, \n moves to the next line, \r moves to the beginning of the line.
I'd suggest adding some "raw string" and "raw character" formatting directives or remove the escaping behaviour altogether.
Fix regression in char printing
Fix regression in char printing