mirror of
https://github.com/ziglang/zig.git
synced 2026-01-12 10:25:13 +00:00
Merge pull request #1451 from kristate/fmt-hexbytes-issue1358
allow bytes to be printed-out as hex (#1358)
This commit is contained in:
commit
1a5c3e4501
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user