authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-24 22:34:24-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-24 22:34:24-05:00
log26b2e5fda86fe5424ebf86924d294d8dbb972eb6
tree73c2787224185f08f686bd286b2ed36d64eafc7b
parent5503f3f7c427d648c3e9a2280cc7a2ba102379d6
parent7cfe854359bcacb038654903fbe8a17c4144c3d5
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'vegecode-formatted-print-to-std.Buffer'

closes #4385

1 files changed, 12 insertions(+), 0 deletions(-)

lib/std/buffer.zig+12
...@@ -147,6 +147,10 @@ pub const Buffer = struct {...@@ -147,6 +147,10 @@ pub const Buffer = struct {
147 try self.resize(m.len);147 try self.resize(m.len);
148 mem.copy(u8, self.list.toSlice(), m);148 mem.copy(u8, self.list.toSlice(), m);
149 }149 }
150
151 pub fn print(self: *Buffer, comptime fmt: []const u8, args: var) !void {
152 return std.fmt.format(self, error{OutOfMemory}, Buffer.append, fmt, args);
153 }
150};154};
151155
152test "simple Buffer" {156test "simple Buffer" {
...@@ -190,3 +194,11 @@ test "Buffer.initCapacity" {...@@ -190,3 +194,11 @@ test "Buffer.initCapacity" {
190 testing.expect(buf.capacity() == old_cap);194 testing.expect(buf.capacity() == old_cap);
191 testing.expect(mem.eql(u8, buf.toSliceConst(), "hello"));195 testing.expect(mem.eql(u8, buf.toSliceConst(), "hello"));
192}196}
197
198test "Buffer.print" {
199 var buf = try Buffer.init(testing.allocator, "");
200 defer buf.deinit();
201
202 try buf.print("Hello {} the {}", .{ 2, "world" });
203 testing.expect(buf.eql("Hello 2 the world"));
204}