authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-20 13:46:20-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-20 13:46:20-04:00
log073f7ebb0e2168d04212fd855235c3543f86a2be
treec5bed4793b0446fc73cdb9f2142f01c384018a9f
parent9c8dfadbb1b95703a1503ae3a508f5acc29e7695
signaturelock-open Commit is signed but in an unrecognized format.

fix formatInt to handle upcasting to base int size


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

std/fmt/index.zig+11-1
...@@ -723,7 +723,9 @@ fn formatIntUnsigned(...@@ -723,7 +723,9 @@ fn formatIntUnsigned(
723 // max_int_digits accounts for the minus sign. when printing an unsigned723 // max_int_digits accounts for the minus sign. when printing an unsigned
724 // number we don't need to do that.724 // number we don't need to do that.
725 var buf: [max_int_digits - 1]u8 = undefined;725 var buf: [max_int_digits - 1]u8 = undefined;
726 var a = if (@sizeOf(@typeOf(value)) == 1) u8(value) else value;726 const min_int_bits = comptime math.max(@typeOf(value).bit_count, @typeOf(base).bit_count);
727 const MinInt = @IntType(@typeOf(value).is_signed, min_int_bits);
728 var a: MinInt = value;
727 var index: usize = buf.len;729 var index: usize = buf.len;
728730
729 while (true) {731 while (true) {
...@@ -965,6 +967,14 @@ test "fmt.format" {...@@ -965,6 +967,14 @@ test "fmt.format" {
965 try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", value);967 try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", value);
966 try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", &value);968 try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", &value);
967 }969 }
970 {
971 const Struct = struct {
972 a: u0,
973 b: u1,
974 };
975 const value = Struct{ .a = 0, .b = 1 };
976 try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", value);
977 }
968 {978 {
969 const Enum = enum {979 const Enum = enum {
970 One,980 One,