From 94af3029c54900384512739110423f2ca9a0e317 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 2 Jun 2025 02:37:19 -0700 Subject: [PATCH] make fmtByteCountIEC and reduceNumber use a writer instead of allocator --- src/fmtByteCountIEC.zig | 30 ++++++++++++------------------ src/reduceNumber.zig | 34 +++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/fmtByteCountIEC.zig b/src/fmtByteCountIEC.zig index 0ca9067906dfe9f71185cb3711c70f8a36cffd4f..84954a9147e060617721b403c17b6a006deae124 100644 --- a/src/fmtByteCountIEC.zig +++ b/src/fmtByteCountIEC.zig @@ -3,31 +3,25 @@ const string = []const u8; const extras = @import("./lib.zig"); const reduceNumber = extras.reduceNumber; -pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string { - return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZYRQ"); +pub fn fmtByteCountIEC(b: u64) std.fmt.Formatter(formatByteCountIEC) { + return .{ .data = b }; +} + +fn formatByteCountIEC(bytes: u64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + try reduceNumber(writer, bytes, 1024, "B", "KMGTPEZYRQ"); } test { - const allocator = std.testing.allocator; - const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 0)); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1 B")); + try std.testing.expectFmt("1 B", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 0))}); } test { - const allocator = std.testing.allocator; - const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 1)); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1.000 KB")); + try std.testing.expectFmt("1.000 KB", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 1))}); } test { - const allocator = std.testing.allocator; - const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 2)); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1.000 MB")); + try std.testing.expectFmt("1.000 MB", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 2))}); } test { - const allocator = std.testing.allocator; - const actual = try fmtByteCountIEC(allocator, std.math.pow(u64, 1024, 3)); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1.000 GB")); + try std.testing.expectFmt("1.000 GB", "{}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 3))}); } diff --git a/src/reduceNumber.zig b/src/reduceNumber.zig index 972b9b9939d5de4341a2c8ef83a31263bd5fe11c..ad4a73cc8e2a85394bd90730779b1bd49be00d45 100644 --- a/src/reduceNumber.zig +++ b/src/reduceNumber.zig @@ -2,9 +2,9 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); -pub fn reduceNumber(alloc: std.mem.Allocator, input: u64, comptime unit: u64, comptime base: string, comptime prefixes: string) !string { +pub fn reduceNumber(writer: anytype, input: u64, comptime unit: u64, comptime base: string, comptime prefixes: string) !void { if (input < unit) { - return std.fmt.allocPrint(alloc, "{d} {s}", .{ input, base }); + return try writer.print("{d} {s}", .{ input, base }); } var div = unit; var exp: usize = 0; @@ -16,30 +16,34 @@ pub fn reduceNumber(alloc: std.mem.Allocator, input: u64, comptime unit: u64, co } const input_f: f64 = @floatFromInt(input); const div_f: f64 = @floatFromInt(div); - return try std.fmt.allocPrint(alloc, "{d:.3} {s}{s}", .{ input_f / div_f, prefixes[exp..][0..1], base }); + return try writer.print("{d:.3} {s}{s}", .{ input_f / div_f, prefixes[exp..][0..1], base }); } test { const allocator = std.testing.allocator; - const actual = try reduceNumber(allocator, 1, 60, "s", "mh"); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1 s")); + var list = std.ArrayList(u8).init(allocator); + defer list.deinit(); + try reduceNumber(list.writer(), 1, 60, "s", "mh"); + try std.testing.expect(std.mem.eql(u8, list.items, "1 s")); } test { const allocator = std.testing.allocator; - const actual = try reduceNumber(allocator, 60, 60, "s", "mh"); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1.000 ms")); + var list = std.ArrayList(u8).init(allocator); + defer list.deinit(); + try reduceNumber(list.writer(), 60, 60, "s", "mh"); + try std.testing.expect(std.mem.eql(u8, list.items, "1.000 ms")); } test { const allocator = std.testing.allocator; - const actual = try reduceNumber(allocator, 3600, 60, "s", "mh"); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "1.000 hs")); + var list = std.ArrayList(u8).init(allocator); + defer list.deinit(); + try reduceNumber(list.writer(), 3600, 60, "s", "mh"); + try std.testing.expect(std.mem.eql(u8, list.items, "1.000 hs")); } test { const allocator = std.testing.allocator; - const actual = try reduceNumber(allocator, 216000, 60, "s", "mh"); - defer allocator.free(actual); - try std.testing.expect(std.mem.eql(u8, actual, "60.000 hs")); + var list = std.ArrayList(u8).init(allocator); + defer list.deinit(); + try reduceNumber(list.writer(), 216000, 60, "s", "mh"); + try std.testing.expect(std.mem.eql(u8, list.items, "60.000 hs")); } -- 2.54.0