aboutsummaryrefslogtreecommitdiff
path: root/std/buffer.zig
diff options
context:
space:
mode:
Diffstat (limited to 'std/buffer.zig')
-rw-r--r--std/buffer.zig13
1 files changed, 13 insertions, 0 deletions
diff --git a/std/buffer.zig b/std/buffer.zig
index aff7fa86ef..3b58002aba 100644
--- a/std/buffer.zig
+++ b/std/buffer.zig
@@ -54,6 +54,19 @@ pub const Buffer = struct {
return result;
}
+ pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: ...) !Buffer {
+ const countSize = struct {
+ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
+ size.* += bytes.len;
+ }
+ }.countSize;
+ var size: usize = 0;
+ std.fmt.format(&size, error{}, countSize, format, args) catch |err| switch (err) {};
+ var self = try Buffer.initSize(allocator, size);
+ assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size);
+ return self;
+ }
+
pub fn deinit(self: *Buffer) void {
self.list.deinit();
}