Skip to content

Commit 9b91c76

Browse files
committed
std.fmt.format supports ints smaller than u8
closes #546 thanks to @dimenus for the fix
1 parent b3d12d2 commit 9b91c76

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

std/fmt/index.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
312312
// max_int_digits accounts for the minus sign. when printing an unsigned
313313
// number we don't need to do that.
314314
var buf: [max_int_digits - 1]u8 = undefined;
315-
var a = value;
315+
var a = if (@sizeOf(@typeOf(value)) == 1) u8(value) else value;
316316
var index: usize = buf.len;
317317

318318
while (true) {
@@ -508,6 +508,12 @@ test "fmt.format" {
508508
const result = bufPrint(buf1[0..], "error union: {}\n", value);
509509
assert(mem.eql(u8, result, "error union: error.InvalidChar\n"));
510510
}
511+
{
512+
var buf1: [32]u8 = undefined;
513+
const value: u3 = 0b101;
514+
const result = bufPrint(buf1[0..], "u3: {}\n", value);
515+
assert(mem.eql(u8, result, "u3: 5\n"));
516+
}
511517
}
512518

513519
pub fn trim(buf: []const u8) -> []const u8 {

0 commit comments

Comments
 (0)