authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-22 13:50:31 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-22 13:50:31 -07:00
log143e3db17dca31d456b30d53ce6184488a331f70
tree1b00402161f136076d9876fe3818dfe9c2fbb4fc
parent3e18f16a27faad6344444685124d7a25c512b56b
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

fmt: add allocPrint and allocPrintZ


1 files changed, 12 insertions(+), 0 deletions(-)

fmt.zig+12
...@@ -930,3 +930,15 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt...@@ -930,3 +930,15 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt
930 invalidFmtError(fmt, value);930 invalidFmtError(fmt, value);
931 }931 }
932}932}
933
934pub fn allocPrint(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![]u8 {
935 const size = std.math.cast(usize, count(fmt, args)) orelse return error.OutOfMemory;
936 const buf = try allocator.alloc(u8, size);
937 return bufPrint(buf, fmt, args) catch |err| switch (err) {
938 error.NoSpaceLeft => unreachable, // we just counted the size above
939 };
940}
941pub fn allocPrintZ(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![:0]u8 {
942 const result = try allocPrint(allocator, fmt ++ "\x00", args);
943 return result[0 .. result.len - 1 :0];
944}