motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 26b2e5fda86fe5424ebf86924d294d8dbb972eb6 (tree)
parent 5503f3f7c427d648c3e9a2280cc7a2ba102379d6
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Mon, 24 Feb 2020 22:34:24 -0500

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

closes #4385

Diffstat:
Mlib/std/buffer.zig | 12++++++++++++
1 file changed, 12 insertions(+), 0 deletions(-)

diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig @@ -147,6 +147,10 @@ pub const Buffer = struct { try self.resize(m.len); 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); + } }; test "simple Buffer" { @@ -190,3 +194,11 @@ test "Buffer.initCapacity" { testing.expect(buf.capacity() == old_cap); testing.expect(mem.eql(u8, buf.toSliceConst(), "hello")); } + +test "Buffer.print" { + var buf = try Buffer.init(testing.allocator, ""); + defer buf.deinit(); + + try buf.print("Hello {} the {}", .{ 2, "world" }); + testing.expect(buf.eql("Hello 2 the world")); +}