diff options
author | 2021-11-15 22:56:32 -0800 | |
---|---|---|
committer | 2021-12-16 19:18:51 -0800 | |
commit | dff23f5a7b073cb9fb2462c6cfb2c0fcbdc62308 (patch) | |
tree | 5d504fec17b483b09150d344903ec1fd19809ac8 /src/builder.zig | |
parent | bcdff7f6f0dbd8df1147da1da10a43a2b585c804 (diff) | |
download | bun-dff23f5a7b073cb9fb2462c6cfb2c0fcbdc62308.tar.gz bun-dff23f5a7b073cb9fb2462c6cfb2c0fcbdc62308.tar.zst bun-dff23f5a7b073cb9fb2462c6cfb2c0fcbdc62308.zip |
wip
Diffstat (limited to 'src/builder.zig')
-rw-r--r-- | src/builder.zig | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/builder.zig b/src/builder.zig new file mode 100644 index 000000000..3811a7878 --- /dev/null +++ b/src/builder.zig @@ -0,0 +1,33 @@ +const Allocator = @import("std").mem.Allocator; +const assert = @import("std").debug.assert; +const copy = @import("std").mem.copy; + +pub fn Builder(comptime Type: type) type { + return struct { + const This = @This(); + + len: usize = 0, + cap: usize = 0, + ptr: ?[*]Type = null, + + pub fn count(this: *This, slice: Type) void { + this.cap += slice.len; + } + + pub fn allocate(this: *This, allocator: *Allocator) !void { + var slice = try allocator.alloc(Type, this.cap); + this.ptr = slice.ptr; + this.len = 0; + } + + pub fn append(this: *This, item: Type) *const Type { + assert(this.len <= this.cap); // didn't count everything + assert(this.ptr != null); // must call allocate first + var result = &this.ptr.?[this.len]; + result.* = item; + this.len += 1; + assert(this.len <= this.cap); + return result; + } + }; +} |