diff options
Diffstat (limited to 'src/walk.zig')
| -rw-r--r-- | src/walk.zig | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/walk.zig b/src/walk.zig index e3738bde..8747937d 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -378,15 +378,15 @@ fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { return switch (@typeInfo(T)) { .void, .null, .undefined => "v", .bool => "b", - .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }), - .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), + .int => |i| @tagName(i.signedness) ++ decimal(i.bits), + .float => |f| "f" ++ decimal(f.bits), .@"enum" => |e| blk: { var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{"; - for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v }); + for (e.field_names, e.field_values) |n, v| out = out ++ n ++ "=" ++ decimal(v) ++ ","; break :blk out ++ "}"; }, .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1), - .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1), + .array => |a| "[" ++ decimal(a.len) ++ "]" ++ shapeOf(a.child, depth + 1), .pointer => |p| switch (p.size) { .one => shapeOf(p.child, depth + 1), .slice => "[]" ++ shapeOf(p.child, depth + 1), @@ -413,6 +413,15 @@ fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { } } +/// `n` in decimal, as `{d}` writes it: memoized, where comptimePrint (an +/// inline function) formats again at every call, 5e6 of the compiler's +/// instructions each. +fn decimal(comptime n: comptime_int) []const u8 { + if (n < 0) return "-" ++ decimal(-n); + if (n < 10) return &.{'0' + n}; + return decimal(n / 10) ++ &[_]u8{'0' + n % 10}; +} + /// An error by its name. An `anyerror` must be one `decode` can read back /// (`errorNamed`): a Debug build stops at one that is not, as it is taped. fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void { @@ -2584,3 +2593,8 @@ test "a blob of 4 KiB and up is digested by its length, what it holds told by it small[1] = 'x'; try std.testing.expect(digest(File, &f) != was); } + +test "a shape's numbers are written as {d} writes them" { + inline for (.{ 0, 7, 10, 65535, -1, -300, std.math.minInt(i128), std.math.maxInt(u128) }) |n| + try std.testing.expectEqualStrings(std.fmt.comptimePrint("{d}", .{n}), comptime decimal(n)); +} |
