| ... | @@ -16,13 +16,22 @@ pub const Buffer = struct { | ... | @@ -16,13 +16,22 @@ pub const Buffer = struct { |
| 16 | mem.copy(u8, self.list.items, m); | 16 | mem.copy(u8, self.list.items, m); |
| 17 | return self; | 17 | return self; |
| 18 | } | 18 | } |
| 19 | | 19 | |
| | 20 | /// Initialize memory to size bytes of undefined values. |
| 20 | /// Must deinitialize with deinit. | 21 | /// Must deinitialize with deinit. |
| 21 | pub fn initSize(allocator: *Allocator, size: usize) !Buffer { | 22 | pub fn initSize(allocator: *Allocator, size: usize) !Buffer { |
| 22 | var self = initNull(allocator); | 23 | var self = initNull(allocator); |
| 23 | try self.resize(size); | 24 | try self.resize(size); |
| 24 | return self; | 25 | return self; |
| 25 | } | 26 | } |
| | 27 | |
| | 28 | /// Initialize with capacity to hold at least num bytes. |
| | 29 | /// Must deinitialize with deinit. |
| | 30 | pub fn initCapacity(allocator: *Allocator, num: usize) !Buffer { |
| | 31 | var self = Buffer{ .list = try ArrayList(u8).initCapacity(allocator, num + 1) }; |
| | 32 | self.list.appendAssumeCapacity(0); |
| | 33 | return self; |
| | 34 | } |
| 26 | | 35 | |
| 27 | /// Must deinitialize with deinit. | 36 | /// Must deinitialize with deinit. |
| 28 | /// None of the other operations are valid until you do one of these: | 37 | /// None of the other operations are valid until you do one of these: |
| ... | @@ -98,6 +107,13 @@ pub const Buffer = struct { | ... | @@ -98,6 +107,13 @@ pub const Buffer = struct { |
| 98 | pub fn len(self: Buffer) usize { | 107 | pub fn len(self: Buffer) usize { |
| 99 | return self.list.len - 1; | 108 | return self.list.len - 1; |
| 100 | } | 109 | } |
| | 110 | |
| | 111 | pub fn capacity(self: Buffer) usize { |
| | 112 | return if (self.list.items.len > 0) |
| | 113 | self.list.items.len - 1 |
| | 114 | else |
| | 115 | 0; |
| | 116 | } |
| 101 | | 117 | |
| 102 | pub fn append(self: *Buffer, m: []const u8) !void { | 118 | pub fn append(self: *Buffer, m: []const u8) !void { |
| 103 | const old_len = self.len(); | 119 | const old_len = self.len(); |
| ... | @@ -151,3 +167,21 @@ test "simple Buffer" { | ... | @@ -151,3 +167,21 @@ test "simple Buffer" { |
| 151 | try buf2.resize(4); | 167 | try buf2.resize(4); |
| 152 | testing.expect(buf.startsWith(buf2.toSlice())); | 168 | testing.expect(buf.startsWith(buf2.toSlice())); |
| 153 | } | 169 | } |
| | 170 | |
| | 171 | test "Buffer.initSize" { |
| | 172 | var buf = try Buffer.initSize(debug.global_allocator, 3); |
| | 173 | testing.expect(buf.len() == 3); |
| | 174 | try buf.append("hello"); |
| | 175 | testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello")); |
| | 176 | } |
| | 177 | |
| | 178 | test "Buffer.initCapacity" { |
| | 179 | var buf = try Buffer.initCapacity(debug.global_allocator, 10); |
| | 180 | testing.expect(buf.len() == 0); |
| | 181 | testing.expect(buf.capacity() >= 10); |
| | 182 | const old_cap = buf.capacity(); |
| | 183 | try buf.append("hello"); |
| | 184 | testing.expect(buf.len() == 5); |
| | 185 | testing.expect(buf.capacity() == old_cap); |
| | 186 | testing.expect(mem.eql(u8, buf.toSliceConst(), "hello")); |
| | 187 | } |