diff options
-rw-r--r-- | src/bun.js/bindings/exports.zig | 17 | ||||
-rw-r--r-- | src/global.zig | 39 |
2 files changed, 55 insertions, 1 deletions
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index b79c56239..9e7258eb0 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -1763,10 +1763,25 @@ pub const ZigConsoleClient = struct { } }, .Integer => { - writer.print(comptime Output.prettyFmt("<r><yellow>{d}<r>", enable_ansi_colors), .{value.toInt64()}); + const int = value.toInt64(); + if (int < std.math.maxInt(u32)) { + var i = int; + const is_negative = i < 0; + if (is_negative) { + i = -i; + } + const digits = bun.fmt.fastDigitCount(@intCast(u32, i)) + @as(u32, @boolToInt(is_negative)); + this.addForNewLine(digits); + } else { + this.addForNewLine(bun.fmt.count("{d}", .{int})); + } + + writer.print(comptime Output.prettyFmt("<r><yellow>{d}<r>", enable_ansi_colors), .{int}); }, .BigInt => { var out_str = value.getZigString(this.globalThis).slice(); + this.addForNewLine(out_str.len); + writer.print(comptime Output.prettyFmt("<r><yellow>{s}n<r>", enable_ansi_colors), .{out_str}); }, .Double => { diff --git a/src/global.zig b/src/global.zig index 635e759ce..d67f93fb7 100644 --- a/src/global.zig +++ b/src/global.zig @@ -31,6 +31,45 @@ pub const path = @import("./resolver/resolve_path.zig"); pub const fmt = struct { pub usingnamespace std.fmt; + // https://lemire.me/blog/2021/06/03/computing-the-number-of-digits-of-an-integer-even-faster/ + pub fn fastDigitCount(x: u64) u64 { + const table = [_]u64{ + 4294967296, + 8589934582, + 8589934582, + 8589934582, + 12884901788, + 12884901788, + 12884901788, + 17179868184, + 17179868184, + 17179868184, + 21474826480, + 21474826480, + 21474826480, + 21474826480, + 25769703776, + 25769703776, + 25769703776, + 30063771072, + 30063771072, + 30063771072, + 34349738368, + 34349738368, + 34349738368, + 34349738368, + 38554705664, + 38554705664, + 38554705664, + 41949672960, + 41949672960, + 41949672960, + 42949672960, + 42949672960, + }; + return x + table[std.math.log2(x)] >> 32; + } + pub const SizeFormatter = struct { value: usize = 0, pub fn format(self: SizeFormatter, comptime _: []const u8, opts: fmt.FormatOptions, writer: anytype) !void { |