Files
bun.sh/src/bun.js/node/buffer.zig
Jarred Sumner c0dd284136 Upgrade to latest Zig (#1610)
* @min and @max

* builtins and some trivial ones

* Most of them

* more

* more!

* More Progress

* wip

* Update tagged_pointer.zig

* Update http_client_async.zig

* Most of the iterable dir changes

* alright

* Remove usages of deprecated formatters

* 📷

* fmt

* Update shimmer.zig

* wip

* wip

* wip

* progress

* more

* Latest

* stuck on error

* latest

* workaround stage2

* wip

* Update string_immutable.zig

* wip

* Migrate `Dirent` and `require("fs')` to use JSC<>Zig bindings

* Fix build errors

* Fixup most of the test failures

* Fix `make headers`

* Fix "outside package path" error

* Fixup aligned alloc

* Add missing file

* linux

* More linux fixes

* use latest peechy

* Fix transpiler test failure

* Forgot about these

* Fixup test failure

* Update node-timers.test.ts

* [node:htt] Fix `undefined is not an object` error

Fixes https://github.com/oven-sh/bun/issues/1618

* Update http.exports.js

* Make this test less flaky

* fix hashes

* Fix hex formatting and zls issues

* Download zig version

* Update Dockerfile

* Update Dockerfile

* Update uws

* Update Dockerfile

* Set llvm version

* Update README.md

* Update uws

* Update Dockerfile

* Update io_linux.zig

* Update bun.zig

* Log output

* workaround strange @cInclude error

* Make ffi tests better

* Don't use cImport

* Update c.zig

* Update c-bindings.cpp

* call setOutputDir

* Update Dockerfile

* Use a longer name

* latest

* Update serve.test.ts

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-12-28 00:51:22 -08:00

102 lines
4.5 KiB
Zig

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;
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;
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)
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)
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)
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)
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)
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)
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)
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)
else
JSC.WebCore.Encoder.writeU8(str.slice().ptr, str.slice().len, buf.ptr, buf.len, JSC.Node.Encoding.hex),
};
if (written <= 0) {
return;
}
var contents = buf[0..@intCast(usize, written)];
buf = buf[@intCast(usize, 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);
std.mem.copy(u8, buf[0..min_len], contents[0..min_len]);
if (buf.len <= contents.len) {
break;
}
buf = buf[min_len..];
contents.len *= 2;
}
while (buf.len > 0) {
const to_fill = @min(minimum_contents.len, buf.len);
std.mem.copy(u8, buf[0..to_fill], minimum_contents[0..to_fill]);
buf = buf[to_fill..];
}
}
};
comptime {
if (!JSC.is_bindgen) {
@export(BufferVectorized.fill, .{ .name = "Bun__Buffer_fill" });
}
}