| ... | @@ -350,6 +350,11 @@ pub fn formatText( | ... | @@ -350,6 +350,11 @@ pub fn formatText( |
| 350 | comptime var width = 0; | 350 | comptime var width = 0; |
| 351 | if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable); | 351 | if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable); |
| 352 | return formatBuf(bytes, width, context, Errors, output); | 352 | 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; |
| 353 | } else @compileError("Unknown format character: " ++ []u8{fmt[0]}); | 358 | } else @compileError("Unknown format character: " ++ []u8{fmt[0]}); |
| 354 | } | 359 | } |
| 355 | return output(context, bytes); | 360 | return output(context, bytes); |
| ... | @@ -1271,6 +1276,12 @@ test "fmt.format" { | ... | @@ -1271,6 +1276,12 @@ test "fmt.format" { |
| 1271 | | 1276 | |
| 1272 | try testFmt("E.Two", "{}", inst); | 1277 | try testFmt("E.Two", "{}", inst); |
| 1273 | } | 1278 | } |
| | 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 | } |
| 1274 | } | 1285 | } |
| 1275 | | 1286 | |
| 1276 | fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void { | 1287 | fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void { |