aboutsummaryrefslogtreecommitdiff
path: root/lib/std/buffer.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-03-13 12:07:06 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-03-13 12:07:06 -0400
commit3eff77bfb52accbc16eb831753ff4917fc2b4873 (patch)
tree49fbf58b5b43ebaffc71eabb7aaa1eb4197044f4 /lib/std/buffer.zig
parenta9297f22671dff800821ff940395411f2adb8582 (diff)
parent4905102901e7d798860f8346faeae505a7268968 (diff)
downloadzig-3eff77bfb52accbc16eb831753ff4917fc2b4873.tar.gz
zig-3eff77bfb52accbc16eb831753ff4917fc2b4873.zip
Merge branch 'fengb-format-stream'
Diffstat (limited to 'lib/std/buffer.zig')
-rw-r--r--lib/std/buffer.zig16
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"));
}