aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-07-03 13:16:45 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-07-03 13:16:57 -0700
commit983039a18afccb2f7d78dfdb06724d1ea58edde6 (patch)
treeef2380e26e558747ae2d25af4ada78513ff0787f /src/bun.js
parenta7a01bd52f20e7908f06d4de9a1814902b838a4b (diff)
downloadbun-983039a18afccb2f7d78dfdb06724d1ea58edde6.tar.gz
bun-983039a18afccb2f7d78dfdb06724d1ea58edde6.tar.zst
bun-983039a18afccb2f7d78dfdb06724d1ea58edde6.zip
Fixes #3508
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/bindings.zig22
-rw-r--r--src/bun.js/webcore/encoding.zig8
2 files changed, 29 insertions, 1 deletions
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig
index 777860d3c..277172b81 100644
--- a/src/bun.js/bindings/bindings.zig
+++ b/src/bun.js/bindings/bindings.zig
@@ -291,7 +291,27 @@ pub const ZigString = extern struct {
return this.len * 2;
}
- /// Count the number of code points in the string.
+ pub fn utf16ByteLength(this: ZigString) usize {
+ if (this.isUTF8()) {
+ return bun.simdutf.length.utf16.from.utf8.le(this.slice());
+ }
+
+ if (this.is16Bit()) {
+ return this.len * 2;
+ }
+
+ return JSC.WebCore.Encoder.byteLengthU8(this.slice().ptr, this.slice().len, .utf16le);
+ }
+
+ pub fn latin1ByteLength(this: ZigString) usize {
+ if (this.isUTF8()) {
+ @panic("TODO");
+ }
+
+ return this.len;
+ }
+
+ /// Count the number of bytes in the UTF-8 version of the string.
/// This function is slow. Use maxUITF8ByteLength() to get a quick estimate
pub fn utf8ByteLength(this: ZigString) usize {
if (this.isUTF8()) {
diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig
index 061a25eed..bb1180acb 100644
--- a/src/bun.js/webcore/encoding.zig
+++ b/src/bun.js/webcore/encoding.zig
@@ -985,6 +985,14 @@ pub const Encoder = struct {
}
}
+ pub fn encodeIntoFrom16(input: []const u16, to: []u8, comptime encoding: JSC.Node.Encoding, comptime allow_partial_write: bool) !usize {
+ return writeU16(input.ptr, input.len, to.ptr, to.len, encoding, allow_partial_write);
+ }
+
+ pub fn encodeIntoFrom8(input: []const u8, to: []u8, comptime encoding: JSC.Node.Encoding) !usize {
+ return writeU8(input.ptr, input.len, to.ptr, to.len, encoding);
+ }
+
pub fn writeU16(input: [*]const u16, len: usize, to: [*]u8, to_len: usize, comptime encoding: JSC.Node.Encoding, comptime allow_partial_write: bool) !usize {
if (len == 0)
return 0;