From a2d96f9040043f1ecfd6fb32c9b765912c012367 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sun, 11 Dec 2022 13:01:24 -0800 Subject: Fix checking the UTF-16 length twice --- src/string_immutable.zig | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/string_immutable.zig') diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 646917840..76b8b7073 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -2419,18 +2419,33 @@ pub fn latin1ToCodepointBytesAssumeNotASCII16(char: u32) u16 { } pub fn copyUTF16IntoUTF8(buf: []u8, comptime Type: type, utf16: Type) EncodeIntoResult { - var remaining = buf; - var utf16_remaining = utf16; - var ended_on_non_ascii = false; - if (comptime Type == []const u16) { if (bun.FeatureFlags.use_simdutf) { - const trimmed = bun.simdutf.trim.utf16(utf16_remaining); + if (utf16.len == 0) + return .{ .read = 0, .written = 0 }; + const trimmed = bun.simdutf.trim.utf16(utf16); + if (trimmed.len == 0) + return .{ .read = 0, .written = 0 }; + const out_len = if (buf.len <= (trimmed.len * 3 + 2)) bun.simdutf.length.utf8.from.utf16.le(trimmed) else buf.len; + return copyUTF16IntoUTF8WithBuffer(buf, Type, utf16, trimmed, out_len); + } + } + + return copyUTF16IntoUTF8WithBuffer(buf, Type, utf16, utf16, utf16.len); +} + +pub fn copyUTF16IntoUTF8WithBuffer(buf: []u8, comptime Type: type, utf16: Type, trimmed: Type, out_len: usize) EncodeIntoResult { + var remaining = buf; + var utf16_remaining = utf16; + var ended_on_non_ascii = false; + + if (comptime Type == []const u16) { + if (bun.FeatureFlags.use_simdutf) { log("UTF16 {d} -> UTF8 {d}", .{ utf16.len, out_len }); if (remaining.len >= out_len) { -- cgit v1.2.3