authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-12-19 11:50:29+01:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-12-19 11:50:29+01:00
log260c3d9cc0c85c1c4dce98bda9e15ca49ca87d74
treec32a25793707bead69013249d5a0c1c44a2bb0d3
parentf6a02a427f6f81e531f05624ee353872e2de7d99

formatType can now format comptime_int


1 files changed, 34 insertions(+), 6 deletions(-)

std/fmt/index.zig+34-6
......@@ -117,7 +117,7 @@ pub fn formatType(
117117 return output(context, @errorName(value));
118118 }
119119 switch (@typeInfo(T)) {
120 builtin.TypeId.Int, builtin.TypeId.Float => {
120 builtin.TypeId.ComptimeInt, builtin.TypeId.Int, builtin.TypeId.Float => {
121121 return formatValue(value, fmt, context, Errors, output);
122122 },
123123 builtin.TypeId.Void => {
......@@ -268,11 +268,15 @@ fn formatValue(
268268 }
269269 }
270270
271 comptime var T = @typeOf(value);
271 const T = @typeOf(value);
272272 switch (@typeId(T)) {
273273 builtin.TypeId.Float => return formatFloatValue(value, fmt, context, Errors, output),
274274 builtin.TypeId.Int => return formatIntValue(value, fmt, context, Errors, output),
275 else => unreachable,
275 builtin.TypeId.ComptimeInt => {
276 const Int = math.IntFittingRange(value, value);
277 return formatIntValue(Int(value), fmt, context, Errors, output);
278 },
279 else => comptime unreachable,
276280 }
277281}
278282
......@@ -289,9 +293,10 @@ pub fn formatIntValue(
289293 if (fmt.len > 0) {
290294 switch (fmt[0]) {
291295 'c' => {
292 if (@typeOf(value) == u8) {
293 if (fmt.len > 1) @compileError("Unknown format character: " ++ []u8{fmt[1]});
294 return formatAsciiChar(value, context, Errors, output);
296 if (@typeOf(value).bit_count <= 8) {
297 if (fmt.len > 1)
298 @compileError("Unknown format character: " ++ []u8{fmt[1]});
299 return formatAsciiChar(u8(value), context, Errors, output);
295300 }
296301 },
297302 'b' => {
......@@ -964,6 +969,25 @@ test "fmt.format" {
964969 const value: u8 = 0b1100;
965970 try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", value);
966971 }
972 {
973 var buf1: [32]u8 = undefined;
974 var context = BufPrintContext{ .remaining = buf1[0..] };
975 try formatType(1234, "", &context, error{BufferTooSmall}, bufPrintWrite);
976 var res = buf1[0 .. buf1.len - context.remaining.len];
977 assert(mem.eql(u8, res, "1234"));
978
979 context = BufPrintContext{ .remaining = buf1[0..] };
980 try formatType('a', "c", &context, error{BufferTooSmall}, bufPrintWrite);
981 res = buf1[0 .. buf1.len - context.remaining.len];
982 debug.warn("{}\n", res);
983 assert(mem.eql(u8, res, "a"));
984
985 context = BufPrintContext{ .remaining = buf1[0..] };
986 try formatType(0b1100, "b", &context, error{BufferTooSmall}, bufPrintWrite);
987 res = buf1[0 .. buf1.len - context.remaining.len];
988 debug.warn("{}\n", res);
989 assert(mem.eql(u8, res, "1100"));
990 }
967991 {
968992 const value: [3]u8 = "abc";
969993 try testFmt("array: abc\n", "array: {}\n", value);
......@@ -985,6 +1009,10 @@ test "fmt.format" {
9851009 try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", value);
9861010 try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", value);
9871011 }
1012 {
1013 const value = @intToPtr(fn () void, 0xdeadbeef);
1014 try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", value);
1015 }
9881016 try testFmt("buf: Test \n", "buf: {s5}\n", "Test");
9891017 try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", "Test");
9901018 try testFmt("cstr: Test C\n", "cstr: {s}\n", c"Test C");