diff options
| -rw-r--r-- | src/CHANGELOG.md | 5 | ||||
| -rw-r--r-- | src/walk.zig | 22 |
2 files changed, 21 insertions, 6 deletions
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 1bece4b2..ce98a862 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -42,13 +42,14 @@ or in a window by the rule above it. - An edit to the core recompiles it about 1.5 s sooner again (`core-test`, - 13 % fewer of the compiler's instructions, 4.4 MB less code in a Debug + 14 % fewer of the compiler's instructions, 4.4 MB less code in a Debug test): a load's note on a field (another build's dump) is written from the type's and the field's names at run time, where every type and field had its own copy of the code that writes one; one byte dump's reader serves a session, a pane and the rest, where each had its own; and what tells a replica's build from another's (`wire.semantics`) - joins its two long parts on, where it formatted them, at every compile. + joins its two long parts on, and writes the tape's numbers itself, + where it formatted them, at every compile. - An edit to the core recompiles it about 3 s sooner (`core-test`, 13 s to 10 s): the build keeps each grammar's highlight patterns that pardes 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)); +} |
