authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-08-31 22:31:29+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-02 19:18:36-04:00
logfb3c5b84ede6fa48949c8069bf735ac67ec21091
tree2af903eb8b198e73020126a6a864ec88374fad64
parentc86108dd63349cbd99a8821fb4d628f31958e0ed

std: add fmt option to escape non-printable characters


1 files changed, 17 insertions(+), 0 deletions(-)

lib/std/fmt.zig+17
......@@ -66,6 +66,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
6666/// - output numeric value in hexadecimal notation
6767/// - `s`: print a pointer-to-many as a c-string, use zero-termination
6868/// - `B` and `Bi`: output a memory size in either metric (1000) or power-of-two (1024) based notation. works for both float and integer values.
69/// - `e` and `E`: if printing a string, escape non-printable characters
6970/// - `e`: output floating point value in scientific notation
7071/// - `d`: output numeric value in decimal notation
7172/// - `b`: output integer value in binary notation
......@@ -599,6 +600,16 @@ pub fn formatText(
599600 try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, writer);
600601 }
601602 return;
603 } else if (comptime (std.mem.eql(u8, fmt, "e") or std.mem.eql(u8, fmt, "E"))) {
604 for (bytes) |c| {
605 if (std.ascii.isPrint(c)) {
606 try writer.writeByte(c);
607 } else {
608 try writer.writeAll("\\x");
609 try formatInt(c, 16, fmt[0] == 'E', FormatOptions{ .width = 2, .fill = '0' }, writer);
610 }
611 }
612 return;
602613 } else {
603614 @compileError("Unknown format string: '" ++ fmt ++ "'");
604615 }
......@@ -1319,6 +1330,12 @@ test "slice" {
13191330 try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});
13201331}
13211332
1333test "escape non-printable" {
1334 try testFmt("abc", "{e}", .{"abc"});
1335 try testFmt("ab\\xffc", "{e}", .{"ab\xffc"});
1336 try testFmt("ab\\xFFc", "{E}", .{"ab\xffc"});
1337}
1338
13221339test "pointer" {
13231340 {
13241341 const value = @intToPtr(*align(1) i32, 0xdeadbeef);