| ... | ... | @@ -930,3 +930,15 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt |
| 930 | 930 | invalidFmtError(fmt, value); |
| 931 | 931 | } |
| 932 | 932 | } |
| 933 | |
| 934 | pub 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 | } |
| 941 | pub 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 | } |