commit e90583f5d17a5a841b7ed8e654af9ed8ce119de5 (tree)
parent 640acf8625cd0024a6234358744167acd6c85f2b
Author: Ben Sinclair <ben@typius.com>
Date: Sat, 23 Mar 2024 03:34:16 +1100
std.fmt.fmtIntSize{Bin,Dec}: Don't add .0... to bytes
These are the fundamental units so they can't have decimal places.
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
@@ -911,8 +911,11 @@ fn formatSizeImpl(comptime base: comptime_int) type {
else => unreachable,
};
- const s = formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
- error.BufferTooSmall => unreachable,
+ const s = switch (magnitude) {
+ 0 => buf[0..formatIntBuf(&buf, value, 10, .lower, .{})],
+ else => formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
+ error.BufferTooSmall => unreachable,
+ },
};
var i: usize = s.len;
@@ -2096,6 +2099,8 @@ test "filesize" {
try expectFmt("file size: 42B\n", "file size: {}\n", .{fmtIntSizeBin(42)});
try expectFmt("file size: 63MB\n", "file size: {}\n", .{fmtIntSizeDec(63 * 1000 * 1000)});
try expectFmt("file size: 63MiB\n", "file size: {}\n", .{fmtIntSizeBin(63 * 1024 * 1024)});
+ try expectFmt("file size: 42B\n", "file size: {:.2}\n", .{fmtIntSizeDec(42)});
+ try expectFmt("file size: 42B\n", "file size: {:>9.2}\n", .{fmtIntSizeDec(42)});
try expectFmt("file size: 66.06MB\n", "file size: {:.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
try expectFmt("file size: 60.08MiB\n", "file size: {:.2}\n", .{fmtIntSizeBin(63 * 1000 * 1000)});
try expectFmt("file size: =66.06MB=\n", "file size: {:=^9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});