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 {...@@ -99,26 +99,10 @@ pub const Buffer = struct {
99 mem.copy(u8, self.list.toSlice()[old_len..], m);99 mem.copy(u8, self.list.toSlice()[old_len..], m);
100 }100 }
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
108 pub fn appendByte(self: &Buffer, byte: u8) !void {102 pub fn appendByte(self: &Buffer, byte: u8) !void {
109 return self.appendByteNTimes(byte, 1);103 const old_len = self.len();
110 }104 try self.resize(old_len + 1);
111105 self.list.toSlice()[old_len] = byte;
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 }
122 }106 }
123107
124 pub fn eql(self: &const Buffer, m: []const u8) bool {108 pub fn eql(self: &const Buffer, m: []const u8) bool {
...@@ -154,7 +138,7 @@ test "simple Buffer" {...@@ -154,7 +138,7 @@ test "simple Buffer" {
154 var buf = try Buffer.init(debug.global_allocator, "");138 var buf = try Buffer.init(debug.global_allocator, "");
155 assert(buf.len() == 0);139 assert(buf.len() == 0);
156 try buf.append("hello");140 try buf.append("hello");
157 try buf.appendByte(' ');141 try buf.append(" ");
158 try buf.append("world");142 try buf.append("world");
159 assert(buf.eql("hello world"));143 assert(buf.eql("hello world"));
160 assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));144 assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));
std/io_test.zig+17-1
...@@ -1,6 +1,5 @@...@@ -1,6 +1,5 @@
1const std = @import("index.zig");1const std = @import("index.zig");
2const io = std.io;2const io = std.io;
3const allocator = std.debug.global_allocator;
4const DefaultPrng = std.rand.DefaultPrng;3const DefaultPrng = std.rand.DefaultPrng;
5const assert = std.debug.assert;4const assert = std.debug.assert;
6const mem = std.mem;5const mem = std.mem;
...@@ -8,6 +7,9 @@ const os = std.os;...@@ -8,6 +7,9 @@ const os = std.os;
8const builtin = @import("builtin");7const builtin = @import("builtin");
98
10test "write a file, read it, then delete it" {9test "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
11 var data: [1024]u8 = undefined;13 var data: [1024]u8 = undefined;
12 var prng = DefaultPrng.init(1234);14 var prng = DefaultPrng.init(1234);
13 prng.random.bytes(data[0..]);15 prng.random.bytes(data[0..]);
...@@ -44,3 +46,17 @@ test "write a file, read it, then delete it" {...@@ -44,3 +46,17 @@ test "write a file, read it, then delete it" {
44 }46 }
45 try os.deleteFile(allocator, tmp_file_name);47 try os.deleteFile(allocator, tmp_file_name);
46}48}
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}