| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | const reduceNumber = extras.reduceNumber; |
| 5 | |
| 6 | pub fn fmtByteCountIEC(b: u64) std.fmt.Alt(u64, formatByteCountIEC) { |
| 7 | return .{ .data = b }; |
| 8 | } |
| 9 | |
| 10 | fn formatByteCountIEC(bytes: u64, writer: *std.Io.Writer) !void { |
| 11 | try reduceNumber(writer, bytes, 1024, "B", "KMGTPEZYRQ"); |
| 12 | } |
| 13 | |
| 14 | test { |
| 15 | try std.testing.expectFmt("1 B", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 0))}); |
| 16 | } |
| 17 | test { |
| 18 | try std.testing.expectFmt("1.000 KB", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 1))}); |
| 19 | } |
| 20 | test { |
| 21 | try std.testing.expectFmt("1.000 MB", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 2))}); |
| 22 | } |
| 23 | test { |
| 24 | try std.testing.expectFmt("1.000 GB", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 3))}); |
| 25 | } |