From f4ebb330408b3845aff8c66f01182663424f787e Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Fri, 9 Feb 2024 02:54:45 -0800 Subject: [PATCH] This method has no generic usages, it does not need to be generic cc @paperdave --- src/allocators.zig | 12 +++++++++--- src/bun.js/webcore/blob.zig | 2 +- src/bun.zig | 2 +- src/install/semver.zig | 2 +- src/js_lexer.zig | 2 +- src/resolver/resolve_path.zig | 2 +- src/string_immutable.zig | 2 +- src/string_mutable.zig | 2 +- 8 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/allocators.zig b/src/allocators.zig index 291677d0de..f129b2cd77 100644 --- a/src/allocators.zig +++ b/src/allocators.zig @@ -5,12 +5,18 @@ const Environment = @import("./env.zig"); const FixedBufferAllocator = std.heap.FixedBufferAllocator; const bun = @import("root").bun; -/// Checks if a slice's pointer is contained within another slice. -pub inline fn isSliceInBuffer(comptime T: type, slice: []const T, buffer: []const T) bool { +inline fn isSliceInBufferT(comptime T: type, slice: []const T, buffer: []const T) bool { return (@intFromPtr(buffer.ptr) <= @intFromPtr(slice.ptr) and (@intFromPtr(slice.ptr) + slice.len) <= (@intFromPtr(buffer.ptr) + buffer.len)); } +/// Checks if a slice's pointer is contained within another slice. +/// +/// If you need to make this generic, use isSliceInBufferT. +pub inline fn isSliceInBuffer(slice: []const u8, buffer: []const u8) bool { + return isSliceInBufferT(u8, slice, buffer); +} + pub fn sliceRange(slice: []const u8, buffer: []const u8) ?[2]u32 { return if (@intFromPtr(buffer.ptr) <= @intFromPtr(slice.ptr) and (@intFromPtr(slice.ptr) + slice.len) <= (@intFromPtr(buffer.ptr) + buffer.len)) @@ -309,7 +315,7 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type } pub fn exists(self: *const Self, value: ValueType) bool { - return isSliceInBuffer(u8, value, &self.backing_buf); + return isSliceInBuffer(value, &self.backing_buf); } pub fn editableSlice(slice: []const u8) []u8 { diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index 98ac742ee0..835bfdfa44 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -3765,7 +3765,7 @@ pub const Blob = struct { if (this.store) |store| { if (store.data == .bytes) { const allocated_slice = store.data.bytes.allocatedSlice(); - if (bun.isSliceInBuffer(u8, buf, allocated_slice)) { + if (bun.isSliceInBuffer(buf, allocated_slice)) { if (bun.linux.memfd_allocator.from(store.data.bytes.allocator)) |allocator| { allocator.ref(); defer allocator.deref(); diff --git a/src/bun.zig b/src/bun.zig index 3cd1f21690..d75aae6949 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -563,7 +563,7 @@ pub inline fn sliceInBuffer(stable: string, value: string) string { } pub fn rangeOfSliceInBuffer(slice: []const u8, buffer: []const u8) ?[2]u32 { - if (!isSliceInBuffer(u8, slice, buffer)) return null; + if (!isSliceInBuffer(slice, buffer)) return null; const r = [_]u32{ @as(u32, @truncate(@intFromPtr(slice.ptr) -| @intFromPtr(buffer.ptr))), @as(u32, @truncate(slice.len)), diff --git a/src/install/semver.zig b/src/install/semver.zig index ad2778c71f..20e7066f24 100644 --- a/src/install/semver.zig +++ b/src/install/semver.zig @@ -250,7 +250,7 @@ pub const String = extern struct { in: string, ) Pointer { if (Environment.allow_assert) { - std.debug.assert(bun.isSliceInBuffer(u8, in, buf)); + std.debug.assert(bun.isSliceInBuffer(in, buf)); } return Pointer{ diff --git a/src/js_lexer.zig b/src/js_lexer.zig index c7adf811f1..a7ac8f8bdc 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -2001,7 +2001,7 @@ fn NewLexer_( } if (comptime Environment.allow_assert) - std.debug.assert(rest.len == 0 or bun.isSliceInBuffer(u8, rest, text)); + std.debug.assert(rest.len == 0 or bun.isSliceInBuffer(rest, text)); while (rest.len > 0) { const c = rest[0]; diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index cd5e3be976..d13122c254 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -1196,7 +1196,7 @@ pub fn joinZ(_parts: anytype, comptime _platform: Platform) [:0]const u8 { pub fn joinZBuf(buf: []u8, _parts: anytype, comptime _platform: Platform) [:0]const u8 { const joined = joinStringBuf(buf[0 .. buf.len - 1], _parts, _platform); - std.debug.assert(bun.isSliceInBuffer(u8, joined, buf)); + std.debug.assert(bun.isSliceInBuffer(joined, buf)); const start_offset = @intFromPtr(joined.ptr) - @intFromPtr(buf.ptr); buf[joined.len + start_offset] = 0; return buf[start_offset..][0..joined.len :0]; diff --git a/src/string_immutable.zig b/src/string_immutable.zig index f59c365f63..740f6e5cbd 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -735,7 +735,7 @@ pub fn withoutTrailingSlashWindowsPath(this: string) []const u8 { } pub fn withTrailingSlash(dir: string, in: string) []const u8 { - if (comptime Environment.allow_assert) std.debug.assert(bun.isSliceInBuffer(u8, dir, in)); + if (comptime Environment.allow_assert) std.debug.assert(bun.isSliceInBuffer(dir, in)); return in[0..@min(strings.withoutTrailingSlash(in[0..@min(dir.len + 1, in.len)]).len + 1, in.len)]; } diff --git a/src/string_mutable.zig b/src/string_mutable.zig index cdf89173ca..42bfdd4fa1 100644 --- a/src/string_mutable.zig +++ b/src/string_mutable.zig @@ -37,7 +37,7 @@ pub const MutableString = struct { } pub fn owns(this: *const MutableString, slice: []const u8) bool { - return bun.isSliceInBuffer(u8, slice, this.list.items.ptr[0..this.list.capacity]); + return bun.isSliceInBuffer(slice, this.list.items.ptr[0..this.list.capacity]); } pub fn growIfNeeded(self: *MutableString, amount: usize) !void {