From 143e3db17dca31d456b30d53ce6184488a331f70 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 22 May 2026 13:50:31 -0700 Subject: [PATCH] fmt: add allocPrint and allocPrintZ --- fmt.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fmt.zig b/fmt.zig index 1d68fc5e6b22cdd8f6975bd544747c7d3464decc..c7d3a7d145566549bc2c74a9a90393ef5ed61e89 100644 --- a/fmt.zig +++ b/fmt.zig @@ -930,3 +930,15 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt invalidFmtError(fmt, value); } } + +pub fn allocPrint(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![]u8 { + const size = std.math.cast(usize, count(fmt, args)) orelse return error.OutOfMemory; + const buf = try allocator.alloc(u8, size); + return bufPrint(buf, fmt, args) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, // we just counted the size above + }; +} +pub fn allocPrintZ(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![:0]u8 { + const result = try allocPrint(allocator, fmt ++ "\x00", args); + return result[0 .. result.len - 1 :0]; +} -- 2.54.0