aboutsummaryrefslogtreecommitdiff
path: root/src/string_joiner.zig
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-05-04 22:44:02 -0700
committerGravatar GitHub <noreply@github.com> 2023-05-04 22:44:02 -0700
commitd9386a68a4316052f355235a7ff4f4b5eb0a2722 (patch)
tree183845717e420645e2ab3b5705dc87b393b726b9 /src/string_joiner.zig
parent945d069290fcf0ce852120b809ed8aa6cdd1e245 (diff)
downloadbun-d9386a68a4316052f355235a7ff4f4b5eb0a2722.tar.gz
bun-d9386a68a4316052f355235a7ff4f4b5eb0a2722.tar.zst
bun-d9386a68a4316052f355235a7ff4f4b5eb0a2722.zip
fix source map line offsets (#2791)
* find codepoint at `i` * fix allocator usage * missing comment * switch to `Cursor` iterator
Diffstat (limited to 'src/string_joiner.zig')
-rw-r--r--src/string_joiner.zig37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/string_joiner.zig b/src/string_joiner.zig
index 7896743cb..c1b42dee7 100644
--- a/src/string_joiner.zig
+++ b/src/string_joiner.zig
@@ -60,6 +60,43 @@ pub fn done(this: *Joiner, allocator: Allocator) ![]u8 {
return slice[0 .. slice.len - remaining.len];
}
+pub fn doneWithEnd(this: *Joiner, allocator: Allocator, end: []const u8) ![]u8 {
+ if (this.head == null and end.len == 0) {
+ return &[_]u8{};
+ }
+
+ if (this.head == null) {
+ var slice = try allocator.alloc(u8, end.len);
+ @memcpy(slice.ptr, end.ptr, end.len);
+ return slice;
+ }
+
+ var slice = try allocator.alloc(u8, this.len + end.len);
+ var remaining = slice;
+ var el_ = this.head;
+ while (el_) |join| {
+ const to_join = join.data.slice[join.data.offset..];
+ @memcpy(remaining.ptr, to_join.ptr, to_join.len);
+
+ remaining = remaining[@min(remaining.len, to_join.len)..];
+
+ var prev = join;
+ el_ = join.next;
+ if (prev.data.needs_deinit) {
+ prev.data.allocator.free(prev.data.slice);
+ prev.data = Joinable{};
+ }
+
+ if (this.use_pool) prev.release();
+ }
+
+ @memcpy(remaining.ptr, end.ptr, end.len);
+
+ remaining = remaining[@min(remaining.len, end.len)..];
+
+ return slice[0 .. slice.len - remaining.len];
+}
+
pub fn lastByte(this: *const Joiner) u8 {
if (this.tail) |tail| {
const slice = tail.data.slice[tail.data.offset..];