authorgravatar for ben@typius.comBen Sinclair <ben@typius.com> 2024-03-23 03:34:16+11:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-24 01:15:22+02:00
loge90583f5d17a5a841b7ed8e654af9ed8ce119de5
treec8fc78a146130976d8e6b11e9966682db5164714
parent640acf8625cd0024a6234358744167acd6c85f2b

std.fmt.fmtIntSize{Bin,Dec}: Don't add .0... to bytes

These are the fundamental units so they can't have decimal places.

1 files changed, 7 insertions(+), 2 deletions(-)

lib/std/fmt.zig+7-2
......@@ -911,8 +911,11 @@ fn formatSizeImpl(comptime base: comptime_int) type {
911911 else => unreachable,
912912 };
913913
914 const s = formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
915 error.BufferTooSmall => unreachable,
914 const s = switch (magnitude) {
915 0 => buf[0..formatIntBuf(&buf, value, 10, .lower, .{})],
916 else => formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
917 error.BufferTooSmall => unreachable,
918 },
916919 };
917920
918921 var i: usize = s.len;
......@@ -2096,6 +2099,8 @@ test "filesize" {
20962099 try expectFmt("file size: 42B\n", "file size: {}\n", .{fmtIntSizeBin(42)});
20972100 try expectFmt("file size: 63MB\n", "file size: {}\n", .{fmtIntSizeDec(63 * 1000 * 1000)});
20982101 try expectFmt("file size: 63MiB\n", "file size: {}\n", .{fmtIntSizeBin(63 * 1024 * 1024)});
2102 try expectFmt("file size: 42B\n", "file size: {:.2}\n", .{fmtIntSizeDec(42)});
2103 try expectFmt("file size: 42B\n", "file size: {:>9.2}\n", .{fmtIntSizeDec(42)});
20992104 try expectFmt("file size: 66.06MB\n", "file size: {:.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
21002105 try expectFmt("file size: 60.08MiB\n", "file size: {:.2}\n", .{fmtIntSizeBin(63 * 1000 * 1000)});
21012106 try expectFmt("file size: =66.06MB=\n", "file size: {:=^9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});