aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-09-01 10:44:54 -0400
committerGitHub <noreply@github.com>2018-09-01 10:44:54 -0400
commit1a5c3e4501589d50920489caed01ac344bc6f72a (patch)
tree73085cb85a5f8b108ff88ac020401d62557294b1 /std
parent19004cd5db1f0c629ff32e8b62b2e1dd57aadc5a (diff)
parent7a633f472daba84b03dac18557f411c949f6b875 (diff)
downloadzig-1a5c3e4501589d50920489caed01ac344bc6f72a.tar.gz
zig-1a5c3e4501589d50920489caed01ac344bc6f72a.zip
Merge pull request #1451 from kristate/fmt-hexbytes-issue1358
allow bytes to be printed-out as hex (#1358)
Diffstat (limited to 'std')
-rw-r--r--std/fmt/index.zig11
1 files changed, 11 insertions, 0 deletions
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 6d23eebd0b..8e34bd43c4 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -350,6 +350,11 @@ pub fn formatText(
comptime var width = 0;
if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
return formatBuf(bytes, width, context, Errors, output);
+ } else if ((fmt[0] == 'x') or (fmt[0] == 'X') ) {
+ for (bytes) |c| {
+ try formatInt(c, 16, fmt[0] == 'X', 0, context, Errors, output);
+ }
+ return;
} else @compileError("Unknown format character: " ++ []u8{fmt[0]});
}
return output(context, bytes);
@@ -1271,6 +1276,12 @@ test "fmt.format" {
try testFmt("E.Two", "{}", inst);
}
+ //print bytes as hex
+ {
+ const some_bytes = "\xCA\xFE\xBA\xBE";
+ try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);
+ try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", some_bytes);
+ }
}
fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void {