Files
bun.sh/src/string_builder.zig
Jarred Sumner e75c711c68 Upgrade to latest Zig, remove dependency on patched version of Zig (#96)
* Prepare to upgrade zig

* zig fmt

* AllocGate

* Update data_url.zig

* wip

* few files

* just headers now?

* I think everything works?

* Update mimalloc

* Update hash_map.zig

* Perf improvements to compensate for Allocgate

* Bump

* 📷

* Update bun.lockb

* Less branching

* [js parser] Slightly reduce memory usage

* Update js_parser.zig

* WIP remove unused

* [JS parser] WIP support for `with` keyword

* Remove more dead code

* Fix all the build errors!

* cleanup

* Move `network_thread` up

* Bump peechy

* Update README.md
2021-12-30 21:12:32 -08:00

33 lines
901 B
Zig

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 StringBuilder = @This();
len: usize = 0,
cap: usize = 0,
ptr: ?[*]u8 = null,
pub fn count(this: *StringBuilder, slice: string) void {
this.cap += slice.len;
}
pub fn allocate(this: *StringBuilder, allocator: Allocator) !void {
var slice = try allocator.alloc(u8, this.cap);
this.ptr = slice.ptr;
this.len = 0;
}
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
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);
return result;
}