diff options
Diffstat (limited to 'src/string_builder.zig')
-rw-r--r-- | src/string_builder.zig | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/string_builder.zig b/src/string_builder.zig index 23e83917b..01447242b 100644 --- a/src/string_builder.zig +++ b/src/string_builder.zig @@ -13,6 +13,18 @@ ptr: ?[*]u8 = null, debug_only_checker: DebugHashTable = DebugHashTable{}, +pub fn initCapacity( + allocator: std.mem.Allocator, + cap: usize, +) !StringBuilder { + return StringBuilder{ + .cap = cap, + .len = 0, + .ptr = (try allocator.alloc(u8, cap)).ptr, + .debug_only_checker = if (comptime Env.allow_assert) DebugHashTable.init(bun.default_allocator) else DebugHashTable{}, + }; +} + pub fn count(this: *StringBuilder, slice: string) void { this.cap += slice.len; if (comptime Env.allow_assert) { @@ -29,6 +41,7 @@ pub fn allocate(this: *StringBuilder, allocator: Allocator) !void { pub fn deinit(this: *StringBuilder, allocator: Allocator) void { if (this.ptr == null or this.cap == 0) return; allocator.free(this.ptr.?[0..this.cap]); + this.ptr = null; if (comptime Env.allow_assert) { this.debug_only_checker.deinit(bun.default_allocator); this.debug_only_checker = .{}; @@ -42,7 +55,8 @@ pub fn append(this: *StringBuilder, slice: string) string { } if (comptime Env.allow_assert) { - assert(this.debug_only_checker.contains(bun.hash(slice))); + if (this.debug_only_checker.count() > 0) + assert(this.debug_only_checker.contains(bun.hash(slice))); } bun.copy(u8, this.ptr.?[this.len..this.cap], slice); |