aboutsummaryrefslogtreecommitdiff
path: root/lib/std/buffer.zig
diff options
context:
space:
mode:
authorMCRusher <Modernwarfare3Minecraft64@gmail.com>2019-11-23 23:08:33 -0500
committerGitHub <noreply@github.com>2019-11-23 23:08:33 -0500
commitd49e0a7b90dabbf486902fa9d4a4306aede55087 (patch)
treee2ac48ed25c54c6e3b13eaddb1876331cd020eec /lib/std/buffer.zig
parent10e6cde083cf9ddd1ae72850d45c25b1258d0d7e (diff)
downloadzig-d49e0a7b90dabbf486902fa9d4a4306aede55087.tar.gz
zig-d49e0a7b90dabbf486902fa9d4a4306aede55087.zip
Added initCapacity, capacity, and 2 tests.
Added Buffer.initCapcity() to buffer to allow preallocation of a block of memory to reduce future allocations. Uses the added ArrayList.initCapacity() function to achieve this. Added Buffer.capacity() to track current usable allocation size, not counting null byte, and returning 0 if empty or created with Buffer.initNull() Added a test for initCapacity() that shows that no further allocation is performed for an append of size smaller than or equal to capacity when initCapacity is used. Added a test for initSize(), since it did not exist already. Also added a comment to better explain the difference between initSize() and initCapacity() note: forgot in the first commit but thanks to mikdusan for helping me brainstorm, through the process, and for drawing up a draft diff which I tweaked.
Diffstat (limited to 'lib/std/buffer.zig')
-rw-r--r--lib/std/buffer.zig36
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig
index 24bd23fa74..7876f2197f 100644
--- a/lib/std/buffer.zig
+++ b/lib/std/buffer.zig
@@ -16,13 +16,22 @@ pub const Buffer = struct {
mem.copy(u8, self.list.items, m);
return self;
}
-
+
+ /// Initialize memory to size bytes of undefined values.
/// Must deinitialize with deinit.
pub fn initSize(allocator: *Allocator, size: usize) !Buffer {
var self = initNull(allocator);
try self.resize(size);
return self;
}
+
+ /// Initialize with capacity to hold at least num bytes.
+ /// Must deinitialize with deinit.
+ pub fn initCapacity(allocator: *Allocator, num: usize) !Buffer {
+ var self = Buffer{ .list = try ArrayList(u8).initCapacity(allocator, num + 1) };
+ self.list.appendAssumeCapacity(0);
+ return self;
+ }
/// Must deinitialize with deinit.
/// None of the other operations are valid until you do one of these:
@@ -98,6 +107,13 @@ pub const Buffer = struct {
pub fn len(self: Buffer) usize {
return self.list.len - 1;
}
+
+ pub fn capacity(self: Buffer) usize {
+ return if (self.list.items.len > 0)
+ self.list.items.len - 1
+ else
+ 0;
+ }
pub fn append(self: *Buffer, m: []const u8) !void {
const old_len = self.len();
@@ -156,3 +172,21 @@ test "simple Buffer" {
try buf2.resize(4);
testing.expect(buf.startsWith(buf2.toSlice()));
}
+
+test "Buffer.initSize" {
+ var buf = try Buffer.initSize(debug.global_allocator, 3);
+ testing.expect(buf.len() == 3);
+ try buf.append("hello");
+ testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));
+}
+
+test "Buffer.initCapacity" {
+ var buf = try Buffer.initCapacity(debug.global_allocator, 10);
+ testing.expect(buf.len() == 0);
+ testing.expect(buf.capacity() >= 10);
+ const old_cap = buf.capacity();
+ try buf.append("hello");
+ testing.expect(buf.len() == 5);
+ testing.expect(buf.capacity() == old_cap);
+ testing.expect(mem.eql(u8, buf.toSliceConst(), "hello"));
+}