authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-01 10:44:54-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-09-01 10:44:54-04:00
log1a5c3e4501589d50920489caed01ac344bc6f72a
tree73085cb85a5f8b108ff88ac020401d62557294b1
parent19004cd5db1f0c629ff32e8b62b2e1dd57aadc5a
parent7a633f472daba84b03dac18557f411c949f6b875
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1451 from kristate/fmt-hexbytes-issue1358

allow bytes to be printed-out as hex (#1358)

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

std/fmt/index.zig+11
......@@ -350,6 +350,11 @@ pub fn formatText(
350350 comptime var width = 0;
351351 if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
352352 return formatBuf(bytes, width, context, Errors, output);
353 } else if ((fmt[0] == 'x') or (fmt[0] == 'X') ) {
354 for (bytes) |c| {
355 try formatInt(c, 16, fmt[0] == 'X', 0, context, Errors, output);
356 }
357 return;
353358 } else @compileError("Unknown format character: " ++ []u8{fmt[0]});
354359 }
355360 return output(context, bytes);
......@@ -1271,6 +1276,12 @@ test "fmt.format" {
12711276
12721277 try testFmt("E.Two", "{}", inst);
12731278 }
1279 //print bytes as hex
1280 {
1281 const some_bytes = "\xCA\xFE\xBA\xBE";
1282 try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);
1283 try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", some_bytes);
1284 }
12741285}
12751286
12761287fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void {