diff options
author | 2023-06-28 21:11:06 -0700 | |
---|---|---|
committer | 2023-06-28 21:11:06 -0700 | |
commit | 68e6fe00a4be7857f474cb3aa80e0d876499e3f8 (patch) | |
tree | c529c7be459ebe32000b043aac3aa7f9179e9076 /src/string.zig | |
parent | 3258bed1c03a7808b9f2e4970012668e984ed390 (diff) | |
download | bun-68e6fe00a4be7857f474cb3aa80e0d876499e3f8.tar.gz bun-68e6fe00a4be7857f474cb3aa80e0d876499e3f8.tar.zst bun-68e6fe00a4be7857f474cb3aa80e0d876499e3f8.zip |
Use `bun.String` for `ZigException` (#3451)
* Use `bun.String` for `ZigException`
* woopsie
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/string.zig')
-rw-r--r-- | src/string.zig | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/string.zig b/src/string.zig index 54af2ba68..3c0c99ce5 100644 --- a/src/string.zig +++ b/src/string.zig @@ -247,6 +247,26 @@ pub const String = extern struct { extern fn BunString__fromLatin1(bytes: [*]const u8, len: usize) String; extern fn BunString__fromBytes(bytes: [*]const u8, len: usize) String; + pub fn toOwnedSlice(this: String, allocator: std.mem.Allocator) ![]u8 { + switch (this.tag) { + .ZigString => return try this.value.ZigString.toOwnedSlice(allocator), + .WTFStringImpl => { + var utf8_slice = this.value.WTFStringImpl.toUTF8(allocator); + + if (utf8_slice.allocator.get()) |alloc| { + if (isWTFAllocator(alloc)) { + return @constCast((try utf8_slice.clone(allocator)).slice()); + } + } + + return @constCast(utf8_slice.slice()); + }, + .StaticZigString => return try this.value.StaticZigString.toOwnedSlice(allocator), + .Empty => return &[_]u8{}, + else => unreachable, + } + } + pub fn createLatin1(bytes: []const u8) String { JSC.markBinding(@src()); return BunString__fromLatin1(bytes.ptr, bytes.len); @@ -429,6 +449,10 @@ pub const String = extern struct { return .latin1; } + pub fn githubAction(self: String) ZigString.GithubActionFormatter { + return self.toZigString().githubAction(); + } + pub fn byteSlice(this: String) []const u8 { return switch (this.tag) { .ZigString, .StaticZigString => this.value.ZigString.byteSlice(), |