aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/node/buffer.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/node/buffer.zig')
-rw-r--r--src/bun.js/node/buffer.zig114
1 files changed, 45 insertions, 69 deletions
diff --git a/src/bun.js/node/buffer.zig b/src/bun.js/node/buffer.zig
index 5a7d64955..f3cc3c2c2 100644
--- a/src/bun.js/node/buffer.zig
+++ b/src/bun.js/node/buffer.zig
@@ -1,96 +1,72 @@
-const std = @import("std");
const bun = @import("bun");
-const strings = bun.strings;
-const string = bun.string;
-const AsyncIO = @import("bun").AsyncIO;
-const JSC = @import("bun").JSC;
-const PathString = JSC.PathString;
-const Environment = bun.Environment;
-const C = bun.C;
-const Syscall = @import("./syscall.zig");
-const os = std.os;
-
-const JSGlobalObject = JSC.JSGlobalObject;
-const ArgumentsSlice = JSC.Node.ArgumentsSlice;
+const JSC = bun.JSC;
+const Encoder = JSC.WebCore.Encoder;
pub const BufferVectorized = struct {
- extern fn memset_pattern16(b: *anyopaque, pattern16: *const anyopaque, len: usize) void;
-
pub fn fill(
str: *JSC.ZigString,
buf_ptr: [*]u8,
fill_length: usize,
encoding: JSC.Node.Encoding,
- ) callconv(.C) void {
- if (str.len == 0) return;
+ ) callconv(.C) bool {
+ if (str.len == 0) return true;
var buf = buf_ptr[0..fill_length];
const written = switch (encoding) {
- JSC.Node.Encoding.utf8 => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.utf8, true)
+ .utf8 => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .utf8, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.utf8),
- JSC.Node.Encoding.ascii => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.ascii, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .utf8),
+ .ascii => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .ascii, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.ascii),
- JSC.Node.Encoding.latin1 => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.latin1, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .ascii),
+ .latin1 => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .latin1, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.latin1),
- JSC.Node.Encoding.buffer => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.buffer, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .latin1),
+ .buffer => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .buffer, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.buffer),
- JSC.Node.Encoding.utf16le,
- JSC.Node.Encoding.ucs2,
- => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.utf16le, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .buffer),
+ .utf16le, .ucs2 => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .utf16le, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.utf16le),
- JSC.Node.Encoding.base64 => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.base64, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .utf16le),
+ .base64 => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .base64, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.base64),
- JSC.Node.Encoding.base64url => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.base64url, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .base64),
+ .base64url => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .base64url, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.base64url),
- JSC.Node.Encoding.hex => if (str.is16Bit())
- JSC.WebCore.Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, JSC.Node.Encoding.hex, true)
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .base64url),
+ .hex => if (str.is16Bit())
+ Encoder.writeU16(str.utf16SliceAligned().ptr, str.utf16SliceAligned().len, buf.ptr, buf.len, .hex, true)
else
- JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.hex),
- };
-
- if (written <= 0) {
- return;
- }
+ Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, .hex),
+ } catch return false;
- var contents = buf[0..@intCast(usize, written)];
- buf = buf[@intCast(usize, written)..];
+ switch (written) {
+ 0 => {},
+ 1 => @memset(buf.ptr, buf[0], buf.len),
+ else => {
+ var contents = buf[0..written];
+ buf = buf[written..];
- if (contents.len == 1) {
- @memset(buf.ptr, contents[0], buf.len);
- return;
- }
-
- const minimum_contents = contents;
- while (buf.len >= contents.len) {
- const min_len = @min(contents.len, buf.len);
- bun.copy(u8, buf, contents[0..min_len]);
- if (buf.len <= contents.len) {
- break;
- }
- buf = buf[min_len..];
- contents.len *= 2;
- }
+ while (buf.len >= contents.len) {
+ bun.copy(u8, buf, contents);
+ buf = buf[contents.len..];
+ contents.len *= 2;
+ }
- while (buf.len > 0) {
- const to_fill = @min(minimum_contents.len, buf.len);
- bun.copy(u8, buf, minimum_contents[0..to_fill]);
- buf = buf[to_fill..];
+ if (buf.len > 0) {
+ bun.copy(u8, buf, contents[0..buf.len]);
+ }
+ },
}
+ return true;
}
};