aboutsummaryrefslogtreecommitdiff
path: root/src/string_mutable.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-19 22:23:20 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-19 22:23:20 -0800
commit79b6d4734a70c2024af5224b34a09d1ead7d6a4b (patch)
tree0d2fca469caa7ddd682df9937d6154b2829870a3 /src/string_mutable.zig
parentdc26181da44bb3d9de246a8c5ca0288067d39b4b (diff)
downloadbun-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.zig23
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 {