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

closes #4385
This commit is contained in:
Andrew Kelley 2020-02-24 22:34:24 -05:00
commit 26b2e5fda8
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9

View File

@ -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"));
}