aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/webcore/encoding.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/webcore/encoding.zig')
-rw-r--r--src/bun.js/webcore/encoding.zig49
1 files changed, 28 insertions, 21 deletions
diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig
index 6729cc4de..8a6e3224b 100644
--- a/src/bun.js/webcore/encoding.zig
+++ b/src/bun.js/webcore/encoding.zig
@@ -795,15 +795,19 @@ pub const Encoder = struct {
switch (comptime encoding) {
.ascii => {
- var to = allocator.alloc(u8, len) catch return ZigString.init("Out of memory").toErrorInstance(global);
-
- @memcpy(to.ptr, input_ptr, to.len);
+ if (bun.simdutf.validate.ascii(input)) {
+ return ZigString.init(input).toValueGC(global);
+ }
- // Hoping this gets auto vectorized
- for (to[0..to.len]) |c, i| {
- to[i] = @as(u8, @truncate(u7, c));
+ if (input.len < 512) {
+ var buf: [512]u8 = undefined;
+ var to = buf[0..input.len];
+ strings.copyLatin1IntoASCII(to, input);
+ return ZigString.init(to).toValueGC(global);
}
+ var to = allocator.alloc(u8, len) catch return ZigString.init("Out of memory").toErrorInstance(global);
+ strings.copyLatin1IntoASCII(to, input);
return ZigString.init(to).toExternalValue(global);
},
.latin1 => {
@@ -857,7 +861,7 @@ pub const Encoder = struct {
}
}
- pub fn writeU8(input: [*]const u8, len: usize, to: [*]u8, to_len: usize, comptime encoding: JSC.Node.Encoding) i64 {
+ pub fn writeU8(input: [*]const u8, len: usize, to_ptr: [*]u8, to_len: usize, comptime encoding: JSC.Node.Encoding) i64 {
if (len == 0 or to_len == 0)
return 0;
@@ -871,39 +875,42 @@ pub const Encoder = struct {
switch (comptime encoding) {
JSC.Node.Encoding.buffer => {
const written = @min(len, to_len);
- @memcpy(to, input, written);
+ @memcpy(to_ptr, input, written);
return @intCast(i64, written);
},
.latin1, .ascii => {
const written = @min(len, to_len);
- @memcpy(to, input, written);
- // Hoping this gets auto vectorized
- for (to[0..written]) |c, i| {
- to[i] = @as(u8, @truncate(u7, c));
+ var to = to_ptr[0..written];
+ var remain = input[0..written];
+
+ if (bun.simdutf.validate.ascii(remain)) {
+ @memcpy(to.ptr, remain.ptr, written);
+ } else {
+ strings.copyLatin1IntoASCII(to, remain);
}
return @intCast(i64, written);
},
.utf8 => {
// need to encode
- return @intCast(i64, strings.copyLatin1IntoUTF8(to[0..to_len], []const u8, input[0..len]).written);
+ return @intCast(i64, strings.copyLatin1IntoUTF8(to_ptr[0..to_len], []const u8, input[0..len]).written);
},
// encode latin1 into UTF16
JSC.Node.Encoding.ucs2, JSC.Node.Encoding.utf16le => {
if (to_len < 2)
return 0;
- if (std.mem.isAligned(@ptrToInt(to), @alignOf([*]u16))) {
+ if (std.mem.isAligned(@ptrToInt(to_ptr), @alignOf([*]u16))) {
var buf = input[0..len];
- var output = @ptrCast([*]u16, @alignCast(@alignOf(u16), to))[0 .. to_len / 2];
+ var output = @ptrCast([*]u16, @alignCast(@alignOf(u16), to_ptr))[0 .. to_len / 2];
var written = strings.copyLatin1IntoUTF16([]u16, output, []const u8, buf).written;
return written * 2;
} else {
var buf = input[0..len];
- var output = @ptrCast([*]align(1) u16, to)[0 .. to_len / 2];
+ var output = @ptrCast([*]align(1) u16, to_ptr)[0 .. to_len / 2];
var written = strings.copyLatin1IntoUTF16([]align(1) u16, output, []const u8, buf).written;
return written * 2;
@@ -911,7 +918,7 @@ pub const Encoder = struct {
},
JSC.Node.Encoding.hex => {
- return @intCast(i64, strings.decodeHexToBytes(to[0..to_len], u8, input[0..len]));
+ return @intCast(i64, strings.decodeHexToBytes(to_ptr[0..to_len], u8, input[0..len]));
},
JSC.Node.Encoding.base64url => {
@@ -919,18 +926,18 @@ pub const Encoder = struct {
if (slice.len == 0)
return 0;
- if (strings.eqlComptime(slice[slice.len - 2 ..][0..2], "==")) {
+ if (strings.endsWithComptime(slice, "==")) {
slice = slice[0 .. slice.len - 2];
} else if (slice[slice.len - 1] == '=') {
slice = slice[0 .. slice.len - 1];
}
- const wrote = bun.base64.decodeURLSafe(to[0..to_len], slice).written;
+ const wrote = bun.base64.decodeURLSafe(to_ptr[0..to_len], slice).written;
return @intCast(i64, wrote);
},
JSC.Node.Encoding.base64 => {
- return @intCast(i64, bun.base64.decode(to[0..to_len], input[0..len]).written);
+ return @intCast(i64, bun.base64.decode(to_ptr[0..to_len], input[0..len]).written);
},
// else => return 0,
}
@@ -1094,7 +1101,7 @@ pub const Encoder = struct {
if (slice.len == 0)
return &[_]u8{};
- if (strings.eqlComptime(slice[slice.len - 2 ..][0..2], "==")) {
+ if (strings.endsWithComptime(slice, "==")) {
slice = slice[0 .. slice.len - 2];
} else if (slice[slice.len - 1] == '=') {
slice = slice[0 .. slice.len - 1];