diff options
author | 2021-05-13 17:44:50 -0700 | |
---|---|---|
committer | 2021-05-13 17:44:50 -0700 | |
commit | ca4120afec54fc20295a4a7d3ce6f8c29eccd84c (patch) | |
tree | 6619890c616bde65b3798b28fb99018b1eac71e3 /src/string_immutable.zig | |
parent | 2eb09c1fec3a11067066602e2d6613e817a06630 (diff) | |
download | bun-ca4120afec54fc20295a4a7d3ce6f8c29eccd84c.tar.gz bun-ca4120afec54fc20295a4a7d3ce6f8c29eccd84c.tar.zst bun-ca4120afec54fc20295a4a7d3ce6f8c29eccd84c.zip |
bug fixes galore
Former-commit-id: 72439452918caa05a32d4e3607910901fa2f4f85
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r-- | src/string_immutable.zig | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 55d94917b..663b714ea 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -142,6 +142,25 @@ pub fn eqlUtf16(comptime self: string, other: JavascriptString) bool { return std.mem.eql(u16, std.unicode.utf8ToUtf16LeStringLiteral(self), other); } +pub fn toUTF8Alloc(allocator: *std.mem.Allocator, js: JavascriptString) !string { + var temp = std.mem.zeroes([4]u8); + var list = std.ArrayList(u8).initCapacity(allocator, js.len) catch unreachable; + var i: usize = 0; + while (i < js.len) : (i += 1) { + var r1 = @intCast(i32, js[i]); + if (r1 >= 0xD800 and r1 <= 0xDBFF and i + 1 < js.len) { + const r2 = @intCast(i32, js[i] + 1); + if (r2 >= 0xDC00 and r2 <= 0xDFFF) { + r1 = (r1 - 0xD800) << 10 | (r2 - 0xDC00) + 0x10000; + i += 1; + } + } + const width = encodeWTF8Rune(&temp, r1); + list.appendSlice(temp[0..width]) catch unreachable; + } + return list.toOwnedSlice(); +} + // Check utf16 string equals utf8 string without allocating extra memory pub fn utf16EqlString(text: []u16, str: string) bool { if (text.len > str.len) { |