diff options
Diffstat (limited to 'lib/std/buffer.zig')
| -rw-r--r-- | lib/std/buffer.zig | 16 |
1 files changed, 4 insertions, 12 deletions
diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig index 28ce2a5610..a1e29ef51a 100644 --- a/lib/std/buffer.zig +++ b/lib/std/buffer.zig @@ -65,13 +65,9 @@ pub const Buffer = struct { } pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: var) !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) {}; + const size = std.math.cast(usize, std.fmt.count(format, args)) catch |err| switch (err) { + error.Overflow => return error.OutOfMemory, + }; var self = try Buffer.initSize(allocator, size); assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size); return self; @@ -154,10 +150,6 @@ pub const Buffer = struct { mem.copy(u8, self.list.toSlice(), m); } - pub fn print(self: *Buffer, comptime fmt: []const u8, args: var) !void { - return std.fmt.format(self, error{OutOfMemory}, Buffer.append, fmt, args); - } - pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) { return .{ .context = self }; } @@ -216,7 +208,7 @@ test "Buffer.print" { var buf = try Buffer.init(testing.allocator, ""); defer buf.deinit(); - try buf.print("Hello {} the {}", .{ 2, "world" }); + try buf.outStream().print("Hello {} the {}", .{ 2, "world" }); testing.expect(buf.eql("Hello 2 the world")); } |
