From cd3de9c788bc7d6166071be5a993aae0bf86011c Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Tue, 23 Jan 2024 19:28:54 -0800 Subject: [PATCH] fix(windows): fix `text-decoder.test.js` (#8436) * use with_errors * remove comment * use correct index --- src/string_immutable.zig | 58 +++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 2b2595a0c5..f395731a6e 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -1328,22 +1328,21 @@ pub fn toUTF16Alloc(allocator: std.mem.Allocator, bytes: []const u8, comptime fa var out = try allocator.alloc(u16, out_length); log("toUTF16 {d} UTF8 -> {d} UTF16", .{ bytes.len, out_length }); - // avoid `.with_errors.le()` due to https://github.com/simdutf/simdutf/issues/213 - switch (bun.simdutf.convert.utf8.to.utf16.le(trimmed, out)) { - 0 => { - if (comptime fail_if_invalid) { - allocator.free(out); - return error.InvalidByteSequence; - } - - break :simd .{ - .items = out[0..i], - .capacity = out.len, - .allocator = allocator, - }; - }, - else => return out, + const res = bun.simdutf.convert.utf8.to.utf16.with_errors.le(trimmed, out); + if (res.status == .success) { + return out; } + + if (comptime fail_if_invalid) { + allocator.free(out); + return error.InvalidByteSequence; + } + + break :simd .{ + .items = out[0..i], + .capacity = out.len, + .allocator = allocator, + }; } else null; var output = output_ orelse fallback: { var list = try std.ArrayList(u16).initCapacity(allocator, i + 2); @@ -1443,22 +1442,21 @@ pub fn toUTF16AllocNoTrim(allocator: std.mem.Allocator, bytes: []const u8, compt var out = try allocator.alloc(u16, out_length); log("toUTF16 {d} UTF8 -> {d} UTF16", .{ bytes.len, out_length }); - // avoid `.with_errors.le()` due to https://github.com/simdutf/simdutf/issues/213 - switch (bun.simdutf.convert.utf8.to.utf16.le(bytes, out)) { - 0 => { - if (comptime fail_if_invalid) { - allocator.free(out); - return error.InvalidByteSequence; - } - - break :simd .{ - .items = out[0..i], - .capacity = out.len, - .allocator = allocator, - }; - }, - else => return out, + const res = bun.simdutf.convert.utf8.to.utf16.with_errors.le(bytes, out); + if (res.status == .success) { + return out; } + + if (comptime fail_if_invalid) { + allocator.free(out); + return error.InvalidByteSequence; + } + + break :simd .{ + .items = out[0..i], + .capacity = out.len, + .allocator = allocator, + }; } else null; var output = output_ orelse fallback: { var list = try std.ArrayList(u16).initCapacity(allocator, i + 2);