authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-24 15:17:46 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-24 15:17:46 -07:00
log96bbe3de7dd43e3d4f7e6aaaa34697f5f3f61190
treec78ad083275f7fd8c11c5e41f08ba281fb1cabbd
parentab0c8e1b501ab8e048d90ea12ddbf60a75332fb7
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

fmt.allocPrint: use AllocatingWriter instead of count+bufPrint

o(2) -> o(1) runs through of fmt calls

1 files changed, 5 insertions(+), 5 deletions(-)

fmt.zig+5-5
...@@ -937,11 +937,11 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt...@@ -937,11 +937,11 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt
937}937}
938938
939pub fn allocPrint(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![]u8 {939pub fn allocPrint(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![]u8 {
940 const size = std.math.cast(usize, count(fmt, args)) orelse return error.OutOfMemory;940 var aw: nio.AllocatingWriter = .init(allocator);
941 const buf = try allocator.alloc(u8, size);941 errdefer aw.deinit();
942 return bufPrint(buf, fmt, args) catch |err| switch (err) {942 try aw.ensureUnusedCapacity(fmt.len);
943 error.NoSpaceLeft => unreachable, // we just counted the size above943 try format(&aw, fmt, args);
944 };944 return aw.toOwnedSlice();
945}945}
946pub fn allocPrintZ(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![:0]u8 {946pub fn allocPrintZ(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![:0]u8 {
947 const result = try allocPrint(allocator, fmt ++ "\x00", args);947 const result = try allocPrint(allocator, fmt ++ "\x00", args);