std/fmt/index.zig: support printing hex bytes on slices;

This commit is contained in:
kristopher tate 2018-11-15 01:12:10 +09:00 committed by Andrew Kelley
parent be9bb0a857
commit e198417687

View File

@ -229,6 +229,9 @@ pub fn formatType(
return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)); return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
}, },
builtin.TypeInfo.Pointer.Size.Slice => { builtin.TypeInfo.Pointer.Size.Slice => {
if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) {
return formatText(value, fmt, context, Errors, output);
}
const casted_value = ([]const u8)(value); const casted_value = ([]const u8)(value);
return output(context, casted_value); return output(context, casted_value);
}, },
@ -1293,6 +1296,9 @@ test "fmt.format" {
const some_bytes = "\xCA\xFE\xBA\xBE"; const some_bytes = "\xCA\xFE\xBA\xBE";
try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes); try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);
try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", some_bytes); try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", some_bytes);
//Test Slices
try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", some_bytes[0..2]);
try testFmt("lowercase: babe\n", "lowercase: {x}\n", some_bytes[2..]);
const bytes_with_zeros = "\x00\x0E\xBA\xBE"; const bytes_with_zeros = "\x00\x0E\xBA\xBE";
try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", bytes_with_zeros); try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", bytes_with_zeros);
} }