aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-05 11:36:04 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-05 11:36:04 -0800
commit2cbac4bbdd8c34f8bb49cd5dbc8bb773bc63d002 (patch)
treed2d020dd29de02bdea6ea11ddbaeacffb343e999 /src/bun.js
parent320c23543c180d7cd9bfa3218386c890584fa65b (diff)
downloadbun-2cbac4bbdd8c34f8bb49cd5dbc8bb773bc63d002.tar.gz
bun-2cbac4bbdd8c34f8bb49cd5dbc8bb773bc63d002.tar.zst
bun-2cbac4bbdd8c34f8bb49cd5dbc8bb773bc63d002.zip
Introduce `Bun.indexOfLine`
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/api/bun.zig61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index 087d35250..ff8776c4b 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -1143,6 +1143,10 @@ pub const Class = NewClass(
.rfn = @import("../test/jest.zig").Jest.call,
.enumerable = false,
},
+ .indexOfLine = .{
+ .rfn = Bun.indexOfLine,
+ .enumerable = false,
+ },
.gc = .{
.rfn = Bun.runGC,
},
@@ -1301,6 +1305,63 @@ fn dump_mimalloc(
return JSC.JSValue.jsUndefined().asObjectRef();
}
+pub fn indexOfLine(
+ _: void,
+ globalThis: JSC.C.JSContextRef,
+ _: JSC.C.JSObjectRef,
+ _: JSC.C.JSObjectRef,
+ args: []const JSC.C.JSValueRef,
+ _: JSC.C.ExceptionRef,
+) JSC.C.JSValueRef {
+ const arguments = bun.cast([]const JSC.JSValue, args);
+ if (arguments.len == 0) {
+ return JSC.JSValue.jsNumberFromInt32(-1).asObjectRef();
+ }
+
+ var buffer = arguments[0].asArrayBuffer(globalThis) orelse {
+ return JSC.JSValue.jsNumberFromInt32(-1).asObjectRef();
+ };
+
+ var offset: usize = 0;
+ if (arguments.len > 1) {
+ offset = @intCast(
+ usize,
+ @maximum(
+ arguments[1].to(u32),
+ 0,
+ ),
+ );
+ }
+
+ const bytes = buffer.byteSlice();
+ var current_offset = offset;
+ const end = @truncate(u32, bytes.len);
+
+ while (current_offset < end) {
+ if (strings.indexOfNewlineOrNonASCII(bytes, @truncate(u32, current_offset))) |i| {
+ const byte = bytes[i];
+ if (byte > 0x7F) {
+ current_offset += @maximum(strings.wtf8ByteSequenceLength(byte), 1);
+ continue;
+ }
+
+ if (byte == '\r') {
+ if (i + 1 < bytes.len and bytes[i + 1] == '\n') {
+ return JSC.JSValue.jsNumber(i + 1).asObjectRef();
+ }
+ } else if (byte == '\n') {
+ return JSC.JSValue.jsNumber(i).asObjectRef();
+ }
+
+ current_offset = i + 1;
+ } else {
+ break;
+ }
+ }
+
+ return JSC.JSValue.jsNumberFromInt32(-1).asObjectRef();
+}
+
pub const Crypto = struct {
const Hashers = @import("../../sha.zig");