Commit 6fa143355f

vegecode <justin.b.alexander1@gmail.com>
2020-02-04 19:16:20
Add formatted printing directly into std.Buffer
1 parent 5503f3f
Changed files (1)
lib
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"));
+}