authorgravatar for yujiri@disroot.orgYujiri <yujiri@disroot.org> 2022-07-17 06:38:31+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-21 18:41:21+03:00
logb8bf5de75a3ef6c4b757abd0a0d5b5a14b5ee269
tree9740af5106d492336e542d5a90ee09835272959f
parente0548774666cfe5b7396417e1c9bcdea1e37e135

Fix #9184: fmtIntSizeDec/fmtIntSizeBin support FormatOptions


1 files changed, 18 insertions(+), 8 deletions(-)

lib/std/fmt.zig+18-8
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const io = std.io;
2const math = std.math;3const math = std.math;
3const assert = std.debug.assert;4const assert = std.debug.assert;
4const mem = std.mem;5const mem = std.mem;
...@@ -877,8 +878,11 @@ fn formatSizeImpl(comptime radix: comptime_int) type {...@@ -877,8 +878,11 @@ fn formatSizeImpl(comptime radix: comptime_int) type {
877 ) !void {878 ) !void {
878 _ = fmt;879 _ = fmt;
879 if (value == 0) {880 if (value == 0) {
880 return writer.writeAll("0B");881 return formatBuf("0B", options, writer);
881 }882 }
883 // The worst case in terms of space needed is 32 bytes + 3 for the suffix.
884 var buf: [35]u8 = undefined;
885 var bufstream = io.fixedBufferStream(buf[0..]);
882886
883 const mags_si = " kMGTPEZY";887 const mags_si = " kMGTPEZY";
884 const mags_iec = " KMGTPEZY";888 const mags_iec = " KMGTPEZY";
...@@ -896,18 +900,20 @@ fn formatSizeImpl(comptime radix: comptime_int) type {...@@ -896,18 +900,20 @@ fn formatSizeImpl(comptime radix: comptime_int) type {
896 else => unreachable,900 else => unreachable,
897 };901 };
898902
899 try formatFloatDecimal(new_value, options, writer);903 formatFloatDecimal(new_value, options, bufstream.writer()) catch |err| switch (err) {
900904 error.NoSpaceLeft => unreachable, // 35 bytes should be enough
901 if (suffix == ' ') {905 };
902 return writer.writeAll("B");
903 }
904906
905 const buf = switch (radix) {907 bufstream.writer().writeAll(if (suffix == ' ')
908 "B"
909 else switch (radix) {
906 1000 => &[_]u8{ suffix, 'B' },910 1000 => &[_]u8{ suffix, 'B' },
907 1024 => &[_]u8{ suffix, 'i', 'B' },911 1024 => &[_]u8{ suffix, 'i', 'B' },
908 else => unreachable,912 else => unreachable,
913 }) catch |err| switch (err) {
914 error.NoSpaceLeft => unreachable,
909 };915 };
910 return writer.writeAll(buf);916 return formatBuf(bufstream.getWritten(), options, writer);
911 }917 }
912 };918 };
913}919}
...@@ -2155,6 +2161,10 @@ test "filesize" {...@@ -2155,6 +2161,10 @@ test "filesize" {
2155 try expectFmt("file size: 63MiB\n", "file size: {}\n", .{fmtIntSizeBin(63 * 1024 * 1024)});2161 try expectFmt("file size: 63MiB\n", "file size: {}\n", .{fmtIntSizeBin(63 * 1024 * 1024)});
2156 try expectFmt("file size: 66.06MB\n", "file size: {:.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});2162 try expectFmt("file size: 66.06MB\n", "file size: {:.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
2157 try expectFmt("file size: 60.08MiB\n", "file size: {:.2}\n", .{fmtIntSizeBin(63 * 1000 * 1000)});2163 try expectFmt("file size: 60.08MiB\n", "file size: {:.2}\n", .{fmtIntSizeBin(63 * 1000 * 1000)});
2164 try expectFmt("file size: =66.06MB=\n", "file size: {:=^9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
2165 try expectFmt("file size: 66.06MB\n", "file size: {: >9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
2166 try expectFmt("file size: 66.06MB \n", "file size: {: <9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
2167 try expectFmt("file size: 0.01844674407370955ZB\n", "file size: {}\n", .{fmtIntSizeDec(math.maxInt(u64))});
2158}2168}
21592169
2160test "struct" {2170test "struct" {