mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 13:51:47 +00:00
Cleanup how some of the server components stuff works
This commit is contained in:
@@ -232,10 +232,7 @@ pub inline fn lastIndexOf(self: string, str: string) ?usize {
|
||||
}
|
||||
|
||||
pub inline fn indexOf(self: string, str: string) ?usize {
|
||||
const self_ptr = self.ptr;
|
||||
const self_len = self.len;
|
||||
|
||||
const str_ptr = str.ptr;
|
||||
const str_len = str.len;
|
||||
|
||||
// > Both old and new libc's have the bug that if needle is empty,
|
||||
@@ -245,6 +242,12 @@ pub inline fn indexOf(self: string, str: string) ?usize {
|
||||
if (self_len == 0 or str_len == 0 or self_len < str_len)
|
||||
return null;
|
||||
|
||||
const self_ptr = self.ptr;
|
||||
const str_ptr = str.ptr;
|
||||
|
||||
if (str_len == 1)
|
||||
return indexOfCharUsize(self, str_ptr[0]);
|
||||
|
||||
const start = bun.C.memmem(self_ptr, self_len, str_ptr, str_len) orelse return null;
|
||||
|
||||
const i = @ptrToInt(start) - @ptrToInt(self_ptr);
|
||||
@@ -3317,6 +3320,10 @@ pub fn indexOfCharZ(sliceZ: [:0]const u8, char: u8) ?u63 {
|
||||
}
|
||||
|
||||
pub fn indexOfChar(slice: []const u8, char: u8) ?u32 {
|
||||
return @truncate(u32, indexOfCharUsize(slice, char) orelse return null);
|
||||
}
|
||||
|
||||
pub fn indexOfCharUsize(slice: []const u8, char: u8) ?usize {
|
||||
if (slice.len == 0)
|
||||
return null;
|
||||
|
||||
@@ -3325,7 +3332,7 @@ pub fn indexOfChar(slice: []const u8, char: u8) ?u32 {
|
||||
std.debug.assert(i < slice.len);
|
||||
std.debug.assert(slice[i] == char);
|
||||
|
||||
return @truncate(u32, i);
|
||||
return i;
|
||||
}
|
||||
|
||||
test "indexOfChar" {
|
||||
|
||||
Reference in New Issue
Block a user