diff options
author | 2022-11-19 22:23:20 -0800 | |
---|---|---|
committer | 2022-11-19 22:23:20 -0800 | |
commit | 79b6d4734a70c2024af5224b34a09d1ead7d6a4b (patch) | |
tree | 0d2fca469caa7ddd682df9937d6154b2829870a3 /src/string_mutable.zig | |
parent | dc26181da44bb3d9de246a8c5ca0288067d39b4b (diff) | |
download | bun-79b6d4734a70c2024af5224b34a09d1ead7d6a4b.tar.gz bun-79b6d4734a70c2024af5224b34a09d1ead7d6a4b.tar.zst bun-79b6d4734a70c2024af5224b34a09d1ead7d6a4b.zip |
[internal] Add a function for appending without growing extra data
Diffstat (limited to 'src/string_mutable.zig')
-rw-r--r-- | src/string_mutable.zig | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/string_mutable.zig b/src/string_mutable.zig index 2849a4695..9d89ea9db 100644 --- a/src/string_mutable.zig +++ b/src/string_mutable.zig @@ -30,6 +30,20 @@ pub const MutableString = struct { } } + pub fn owns(this: *const MutableString, buffer: []const u8) bool { + if (this.list.capacity < buffer.len) { + return false; + } + + if (@ptrToInt(this.list.items.ptr) <= @ptrToInt(buffer.ptr) and + @ptrToInt(buffer.ptr) + buffer.len <= @ptrToInt(this.list.items.ptr) + this.list.capacity) + { + return true; + } + + return false; + } + pub fn growIfNeeded(self: *MutableString, amount: usize) !void { try self.list.ensureUnusedCapacity(self.allocator, amount); } @@ -156,6 +170,15 @@ pub const MutableString = struct { try self.list.appendSlice(self.allocator, slice); } + pub inline fn appendSliceExact(self: *MutableString, slice: []const u8) !void { + if (slice.len == 0) return; + + try self.list.ensureTotalCapacityPrecise(self.allocator, self.list.items.len + slice.len); + var end = self.list.items.ptr + self.list.items.len; + self.list.items.len += slice.len; + @memcpy(end, slice.ptr, slice.len); + } + pub inline fn reset( self: *MutableString, ) void { |