authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-11 14:08:16-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-11 14:08:16-04:00
log6e821078f625a03eb8b7794c983da0f7793366ab
tree72dfebb643ab61579e3fb8dd58cd4610ffe876fa
parentefa39c5343e13a13e65210f55da5df23ee3feb3e

update std.Buffer API

* remove Buffer.appendFormat * remove Buffer.appendByte * remove Buffer.appendByteNTimes Added test to demo what to use instead of the above functions

2 files changed, 21 insertions(+), 21 deletions(-)

std/buffer.zig+4-20
......@@ -99,26 +99,10 @@ pub const Buffer = struct {
9999 mem.copy(u8, self.list.toSlice()[old_len..], m);
100100 }
101101
102 // TODO: remove, use OutStream for this
103 pub fn appendFormat(self: &Buffer, comptime format: []const u8, args: ...) !void {
104 return fmt.format(self, append, format, args);
105 }
106
107 // TODO: remove, use OutStream for this
108102 pub fn appendByte(self: &Buffer, byte: u8) !void {
109 return self.appendByteNTimes(byte, 1);
110 }
111
112 // TODO: remove, use OutStream for this
113 pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) !void {
114 var prev_size: usize = self.len();
115 const new_size = prev_size + count;
116 try self.resize(new_size);
117
118 var i: usize = prev_size;
119 while (i < new_size) : (i += 1) {
120 self.list.items[i] = byte;
121 }
103 const old_len = self.len();
104 try self.resize(old_len + 1);
105 self.list.toSlice()[old_len] = byte;
122106 }
123107
124108 pub fn eql(self: &const Buffer, m: []const u8) bool {
......@@ -154,7 +138,7 @@ test "simple Buffer" {
154138 var buf = try Buffer.init(debug.global_allocator, "");
155139 assert(buf.len() == 0);
156140 try buf.append("hello");
157 try buf.appendByte(' ');
141 try buf.append(" ");
158142 try buf.append("world");
159143 assert(buf.eql("hello world"));
160144 assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));
std/io_test.zig+17-1
......@@ -1,6 +1,5 @@
11const std = @import("index.zig");
22const io = std.io;
3const allocator = std.debug.global_allocator;
43const DefaultPrng = std.rand.DefaultPrng;
54const assert = std.debug.assert;
65const mem = std.mem;
......@@ -8,6 +7,9 @@ const os = std.os;
87const builtin = @import("builtin");
98
109test "write a file, read it, then delete it" {
10 var raw_bytes: [200 * 1024]u8 = undefined;
11 var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator;
12
1113 var data: [1024]u8 = undefined;
1214 var prng = DefaultPrng.init(1234);
1315 prng.random.bytes(data[0..]);
......@@ -44,3 +46,17 @@ test "write a file, read it, then delete it" {
4446 }
4547 try os.deleteFile(allocator, tmp_file_name);
4648}
49
50test "BufferOutStream" {
51 var bytes: [100]u8 = undefined;
52 var allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
53
54 var buffer = try std.Buffer.initSize(allocator, 0);
55 var buf_stream = &std.io.BufferOutStream.init(&buffer).stream;
56
57 const x: i32 = 42;
58 const y: i32 = 1234;
59 try buf_stream.print("x: {}\ny: {}\n", x, y);
60
61 assert(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n"));
62}