authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-06-02 02:37:19 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-06-02 02:37:19 -07:00
log94af3029c54900384512739110423f2ca9a0e317
treed6058a293a8cb4a65d13cb3a2fc920de44e2018a
parente7ae48cfacb0510ba610e72a9bb064e675e943ea

make fmtByteCountIEC and reduceNumber use a writer instead of allocator


2 files changed, 31 insertions(+), 33 deletions(-)

src/fmtByteCountIEC.zig+12-18
...@@ -3,31 +3,25 @@ const string = []const u8;...@@ -3,31 +3,25 @@ const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const reduceNumber = extras.reduceNumber;4const reduceNumber = extras.reduceNumber;
55
6pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string {6pub fn fmtByteCountIEC(b: u64) std.fmt.Formatter(formatByteCountIEC) {
7 return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZYRQ");7 return .{ .data = b };
8}
9
10fn formatByteCountIEC(bytes: u64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
11 _ = fmt;
12 _ = options;
13 try reduceNumber(writer, bytes, 1024, "B", "KMGTPEZYRQ");
8}14}
915
10test {16test {
11 const allocator = std.testing.allocator;17 try std.testing.expectFmt("1 B", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 0))});
12 const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 0));
13 defer allocator.free(actual);
14 try std.testing.expect(std.mem.eql(u8, actual, "1 B"));
15}18}
16test {19test {
17 const allocator = std.testing.allocator;20 try std.testing.expectFmt("1.000 KB", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 1))});
18 const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 1));
19 defer allocator.free(actual);
20 try std.testing.expect(std.mem.eql(u8, actual, "1.000 KB"));
21}21}
22test {22test {
23 const allocator = std.testing.allocator;23 try std.testing.expectFmt("1.000 MB", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 2))});
24 const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 2));
25 defer allocator.free(actual);
26 try std.testing.expect(std.mem.eql(u8, actual, "1.000 MB"));
27}24}
28test {25test {
29 const allocator = std.testing.allocator;26 try std.testing.expectFmt("1.000 GB", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 3))});
30 const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 3));
31 defer allocator.free(actual);
32 try std.testing.expect(std.mem.eql(u8, actual, "1.000 GB"));
33}27}
src/reduceNumber.zig+19-15
...@@ -2,9 +2,9 @@ const std = @import("std");...@@ -2,9 +2,9 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
44
5pub fn reduceNumber(alloc: std.mem.Allocator, input: u64, comptime unit: u64, comptime base: string, comptime prefixes: string) !string {5pub fn reduceNumber(writer: anytype, input: u64, comptime unit: u64, comptime base: string, comptime prefixes: string) !void {
6 if (input < unit) {6 if (input < unit) {
7 return std.fmt.allocPrint(alloc, "{d} {s}", .{ input, base });7 return try writer.print("{d} {s}", .{ input, base });
8 }8 }
9 var div = unit;9 var div = unit;
10 var exp: usize = 0;10 var exp: usize = 0;
...@@ -16,30 +16,34 @@ pub fn reduceNumber(alloc: std.mem.Allocator, input: u64, comptime unit: u64, co...@@ -16,30 +16,34 @@ pub fn reduceNumber(alloc: std.mem.Allocator, input: u64, comptime unit: u64, co
16 }16 }
17 const input_f: f64 = @floatFromInt(input);17 const input_f: f64 = @floatFromInt(input);
18 const div_f: f64 = @floatFromInt(div);18 const div_f: f64 = @floatFromInt(div);
19 return try std.fmt.allocPrint(alloc, "{d:.3} {s}{s}", .{ input_f / div_f, prefixes[exp..][0..1], base });19 return try writer.print("{d:.3} {s}{s}", .{ input_f / div_f, prefixes[exp..][0..1], base });
20}20}
2121
22test {22test {
23 const allocator = std.testing.allocator;23 const allocator = std.testing.allocator;
24 const actual = try reduceNumber(allocator, 1, 60, "s", "mh");24 var list = std.ArrayList(u8).init(allocator);
25 defer allocator.free(actual);25 defer list.deinit();
26 try std.testing.expect(std.mem.eql(u8, actual, "1 s"));26 try reduceNumber(list.writer(), 1, 60, "s", "mh");
27 try std.testing.expect(std.mem.eql(u8, list.items, "1 s"));
27}28}
28test {29test {
29 const allocator = std.testing.allocator;30 const allocator = std.testing.allocator;
30 const actual = try reduceNumber(allocator, 60, 60, "s", "mh");31 var list = std.ArrayList(u8).init(allocator);
31 defer allocator.free(actual);32 defer list.deinit();
32 try std.testing.expect(std.mem.eql(u8, actual, "1.000 ms"));33 try reduceNumber(list.writer(), 60, 60, "s", "mh");
34 try std.testing.expect(std.mem.eql(u8, list.items, "1.000 ms"));
33}35}
34test {36test {
35 const allocator = std.testing.allocator;37 const allocator = std.testing.allocator;
36 const actual = try reduceNumber(allocator, 3600, 60, "s", "mh");38 var list = std.ArrayList(u8).init(allocator);
37 defer allocator.free(actual);39 defer list.deinit();
38 try std.testing.expect(std.mem.eql(u8, actual, "1.000 hs"));40 try reduceNumber(list.writer(), 3600, 60, "s", "mh");
41 try std.testing.expect(std.mem.eql(u8, list.items, "1.000 hs"));
39}42}
40test {43test {
41 const allocator = std.testing.allocator;44 const allocator = std.testing.allocator;
42 const actual = try reduceNumber(allocator, 216000, 60, "s", "mh");45 var list = std.ArrayList(u8).init(allocator);
43 defer allocator.free(actual);46 defer list.deinit();
44 try std.testing.expect(std.mem.eql(u8, actual, "60.000 hs"));47 try reduceNumber(list.writer(), 216000, 60, "s", "mh");
48 try std.testing.expect(std.mem.eql(u8, list.items, "60.000 hs"));
45}49}