aboutsummaryrefslogtreecommitdiff
path: root/src/string_builder.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-02-25 00:28:25 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-02-25 00:48:36 -0800
commit293a9bc811e35943cc4d1dbdcbf20bbf77ac8c2f (patch)
tree52cd823205ca899e27e91480365bba82c5f6b871 /src/string_builder.zig
parentb8c6865ce0a0385982e51de5614689b43f265562 (diff)
downloadbun-293a9bc811e35943cc4d1dbdcbf20bbf77ac8c2f.tar.gz
bun-293a9bc811e35943cc4d1dbdcbf20bbf77ac8c2f.tar.zst
bun-293a9bc811e35943cc4d1dbdcbf20bbf77ac8c2f.zip
[bun install] Add metadata hash
Diffstat (limited to 'src/string_builder.zig')
-rw-r--r--src/string_builder.zig31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/string_builder.zig b/src/string_builder.zig
index 98ef5ab9b..c3e14fad4 100644
--- a/src/string_builder.zig
+++ b/src/string_builder.zig
@@ -2,6 +2,7 @@ const string = @import("string_types.zig").string;
const Allocator = @import("std").mem.Allocator;
const assert = @import("std").debug.assert;
const copy = @import("std").mem.copy;
+const Env = @import("./env.zig");
const StringBuilder = @This();
@@ -19,28 +20,46 @@ pub fn allocate(this: *StringBuilder, allocator: Allocator) !void {
this.len = 0;
}
+pub fn deinit(this: *StringBuilder, allocator: Allocator) void {
+ if (this.ptr == null or this.cap == 0) return;
+ allocator.free(this.ptr.?[0..this.cap]);
+}
+
pub fn append(this: *StringBuilder, slice: string) string {
- assert(this.len <= this.cap); // didn't count everything
- assert(this.ptr != null); // must call allocate first
+ if (Env.allow_assert) {
+ assert(this.len <= this.cap); // didn't count everything
+ assert(this.ptr != null); // must call allocate first
+ }
copy(u8, this.ptr.?[this.len..this.cap], slice);
const result = this.ptr.?[this.len..this.cap][0..slice.len];
this.len += slice.len;
- assert(this.len <= this.cap);
+ if (Env.allow_assert) {
+ assert(this.len <= this.cap);
+ }
+
return result;
}
const std = @import("std");
pub fn fmt(this: *StringBuilder, comptime str: string, args: anytype) string {
- assert(this.len <= this.cap); // didn't count everything
- assert(this.ptr != null); // must call allocate first
+ if (Env.allow_assert) {
+ assert(this.len <= this.cap); // didn't count everything
+ assert(this.ptr != null); // must call allocate first
+ }
var buf = this.ptr.?[this.len..this.cap];
const out = std.fmt.bufPrint(buf, str, args) catch unreachable;
this.len += out.len;
- assert(this.len <= this.cap);
+ if (Env.allow_assert) {
+ assert(this.len <= this.cap);
+ }
return out;
}
+
+pub fn fmtCount(this: *StringBuilder, comptime str: string, args: anytype) void {
+ this.cap += std.fmt.count(str, args);
+}