aboutsummaryrefslogtreecommitdiff
path: root/lib/std/fmt.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-09-03 23:52:19 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-09-03 23:52:19 -0700
commit338f155a02b72117ff710f72c8578e7d2f8eb296 (patch)
treea902526d5dc901de7458ef318f52f5ac0dad77e7 /lib/std/fmt.zig
parentc354f074fa91d3d1672469ba4bbc49a1730e1d01 (diff)
parent88724b2a89157ecc3a8eea03aa0f8a6b66829915 (diff)
downloadzig-338f155a02b72117ff710f72c8578e7d2f8eb296.tar.gz
zig-338f155a02b72117ff710f72c8578e7d2f8eb296.zip
Merge remote-tracking branch 'origin/master' into llvm11
Diffstat (limited to 'lib/std/fmt.zig')
-rw-r--r--lib/std/fmt.zig17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index 16d0eaa07a..3067a55759 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -66,6 +66,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
/// - output numeric value in hexadecimal notation
/// - `s`: print a pointer-to-many as a c-string, use zero-termination
/// - `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.
+/// - `e` and `E`: if printing a string, escape non-printable characters
/// - `e`: output floating point value in scientific notation
/// - `d`: output numeric value in decimal notation
/// - `b`: output integer value in binary notation
@@ -599,6 +600,16 @@ pub fn formatText(
try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, writer);
}
return;
+ } else if (comptime (std.mem.eql(u8, fmt, "e") or std.mem.eql(u8, fmt, "E"))) {
+ for (bytes) |c| {
+ if (std.ascii.isPrint(c)) {
+ try writer.writeByte(c);
+ } else {
+ try writer.writeAll("\\x");
+ try formatInt(c, 16, fmt[0] == 'E', FormatOptions{ .width = 2, .fill = '0' }, writer);
+ }
+ }
+ return;
} else {
@compileError("Unknown format string: '" ++ fmt ++ "'");
}
@@ -1319,6 +1330,12 @@ test "slice" {
try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});
}
+test "escape non-printable" {
+ try testFmt("abc", "{e}", .{"abc"});
+ try testFmt("ab\\xffc", "{e}", .{"ab\xffc"});
+ try testFmt("ab\\xFFc", "{E}", .{"ab\xffc"});
+}
+
test "pointer" {
{
const value = @intToPtr(*align(1) i32, 0xdeadbeef);