diff options
| author | vegecode <justin.b.alexander1@gmail.com> | 2020-02-04 12:16:20 -0600 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-02-24 22:30:23 -0500 |
| commit | 6fa143355f256e354a2df67a42e3e9208d1a71e9 (patch) | |
| tree | 18e470f775ca42d068aed730131a32e40555607e /lib/std/buffer.zig | |
| parent | 5503f3f7c427d648c3e9a2280cc7a2ba102379d6 (diff) | |
| download | zig-6fa143355f256e354a2df67a42e3e9208d1a71e9.tar.gz zig-6fa143355f256e354a2df67a42e3e9208d1a71e9.zip | |
Add formatted printing directly into std.Buffer
Diffstat (limited to 'lib/std/buffer.zig')
| -rw-r--r-- | lib/std/buffer.zig | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig index 077b2b615f..6bf03472f6 100644 --- a/lib/std/buffer.zig +++ b/lib/std/buffer.zig @@ -147,6 +147,16 @@ 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 { + try std.fmt.format( + self, + @typeInfo(@TypeOf(Buffer.append)).Fn.return_type.?.ErrorSet, + Buffer.append, + fmt, + args, + ); + } }; test "simple Buffer" { @@ -190,3 +200,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")); +} |
