aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-27 22:09:56 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-11-27 22:09:56 -0800
commit4ee11d922cb9fb4b97ae07028c409ffba153a1d4 (patch)
tree014ece7037771eefacbf4cf3bf7df76dce0988db /src/bun.js
parent885049831f9c09f9e41e80d66062d0b5d2525257 (diff)
downloadbun-4ee11d922cb9fb4b97ae07028c409ffba153a1d4.tar.gz
bun-4ee11d922cb9fb4b97ae07028c409ffba153a1d4.tar.zst
bun-4ee11d922cb9fb4b97ae07028c409ffba153a1d4.zip
Cleanup some of the encoding code
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/bun-simdutf.zig48
-rw-r--r--src/bun.js/webcore/encoding.zig11
2 files changed, 31 insertions, 28 deletions
diff --git a/src/bun.js/bindings/bun-simdutf.zig b/src/bun.js/bindings/bun-simdutf.zig
index f84ce56ce..531e9c3ef 100644
--- a/src/bun.js/bindings/bun-simdutf.zig
+++ b/src/bun.js/bindings/bun-simdutf.zig
@@ -292,48 +292,52 @@ pub const length = struct {
pub const trim = struct {
pub fn utf8_len(buf: []const u8) usize {
- if (buf.len < 3) {
- switch (buf.len) {
+ const len = buf.len;
+
+ if (len < 3) {
+ switch (len) {
2 => {
- if (buf[buf.len - 1] >= 0b11000000) {
- return buf.len - 1;
+ if (buf[len - 1] >= 0b11000000) {
+ return len - 1;
} // 2-, 3- and 4-byte characters with only 1 byte left
- if (buf[buf.len - 2] >= 0b11100000) {
- return buf.len - 2;
+ if (buf[len - 2] >= 0b11100000) {
+ return len - 2;
} // 3- and 4-byte characters with only 2 bytes left
- return buf.len;
+ return len;
},
1 => {
- if (buf[buf.len - 1] >= 0b11000000) {
- return buf.len - 1;
+ if (buf[len - 1] >= 0b11000000) {
+ return len - 1;
} // 2-, 3- and 4-byte characters with only 1 byte left
- return buf.len;
+ return len;
},
- 0 => return buf.len,
+ 0 => return len,
else => unreachable,
}
}
- if (buf[buf.len - 1] >= 0b11000000) {
- return buf.len - 1;
+ if (buf[len - 1] >= 0b11000000) {
+ return len - 1;
} // 2-, 3- and 4-byte characters with only 1 byte left
- if (buf[buf.len - 2] >= 0b11100000) {
- return buf.len - 2;
+ if (buf[len - 2] >= 0b11100000) {
+ return len - 2;
} // 3- and 4-byte characters with only 1 byte left
- if (buf[buf.len - 3] >= 0b11110000) {
- return buf.len - 3;
+ if (buf[len - 3] >= 0b11110000) {
+ return len - 3;
} // 4-byte characters with only 3 bytes left
- return buf.len;
+ return len;
}
pub fn utf16_len(buf: []const u16) usize {
- if (buf.len == 0) {
+ const len = buf.len;
+
+ if (len == 0) {
return 0;
}
- if ((buf[buf.len - 1] >= 0xD800) and (buf[buf.len - 1] <= 0xDBFF)) {
- return buf.len - 1;
+ if ((buf[len - 1] >= 0xD800) and (buf[len - 1] <= 0xDBFF)) {
+ return len - 1;
}
- return buf.len;
+ return len;
}
pub fn utf16(buf: []const u16) []const u16 {
diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig
index 99b304bda..297f3cc0a 100644
--- a/src/bun.js/webcore/encoding.zig
+++ b/src/bun.js/webcore/encoding.zig
@@ -875,7 +875,6 @@ pub const Encoder = struct {
return ZigString.init(to).toExternalValue(global);
},
.buffer, .utf8 => {
- // JSC only supports UTF-16 strings for non-ascii text
const converted = strings.toUTF16Alloc(allocator, input, false) catch return ZigString.init("Out of memory").toErrorInstance(global);
if (converted) |utf16| {
return ZigString.toExternalU16(utf16.ptr, utf16.len, global);
@@ -886,11 +885,11 @@ pub const Encoder = struct {
return ZigString.init(input).toValueGC(global);
},
.ucs2, .utf16le => {
- var output = allocator.alloc(u16, len / 2) catch return ZigString.init("Out of memory").toErrorInstance(global);
- var i: usize = 0;
- while (i < len / 2) : (i += 1) {
- output[i] = (@intCast(u16, input[2 * i + 1]) << 8) + @intCast(u16, input[2 * i]);
- }
+ var output = allocator.alloc(u16, @maximum(len / 2, 1)) catch return ZigString.init("Out of memory").toErrorInstance(global);
+ var output_bytes = std.mem.sliceAsBytes(output);
+ output_bytes[output_bytes.len - 1] = 0;
+
+ @memcpy(output_bytes.ptr, input_ptr, output_bytes.len);
return ZigString.toExternalU16(output.ptr, output.len, global);
},