mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 19:38:58 +00:00
### What does this PR do? Fixes a small bug I found in https://github.com/oven-sh/bun/pull/23107 which caused `SlicedString` not to correctly provide us with subslices. This would have been a **killer** use-case for the interval utility we decided to reject in https://github.com/oven-sh/bun/pull/23882. Consider how nice the code could've been: ```zig pub inline fn sub(this: SlicedString, input: string) SlicedString { const buf_r = bun.math.interval.fromSlice(this.buf); const inp_r = bun.math.interval.fromSlice(this.input); if (Environment.allow_assert) { if (!buf_r.superset(inp_r)) { bun.Output.panic("SlicedString.sub input [{}, {}) is not a substring of the " ++ "slice [{}, {})", .{ start_i, end_i, start_buf, end_buf }); } } return SlicedString{ .buf = this.buf, .slice = input }; } ``` That's a lot more readable than the middle-school algebra we have here, but here we are. ### How did you verify your code works? CI Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
54 lines
1.9 KiB
Zig
54 lines
1.9 KiB
Zig
const SlicedString = @This();
|
|
|
|
buf: string,
|
|
slice: string,
|
|
|
|
pub inline fn init(buf: string, slice: string) SlicedString {
|
|
if (Environment.allow_assert and !@inComptime()) {
|
|
if (@intFromPtr(buf.ptr) > @intFromPtr(slice.ptr)) {
|
|
@panic("SlicedString.init buf is not in front of slice");
|
|
}
|
|
}
|
|
return SlicedString{ .buf = buf, .slice = slice };
|
|
}
|
|
|
|
pub inline fn external(this: SlicedString) ExternalString {
|
|
if (comptime Environment.allow_assert) {
|
|
assert(@intFromPtr(this.buf.ptr) <= @intFromPtr(this.slice.ptr) and ((@intFromPtr(this.slice.ptr) + this.slice.len) <= (@intFromPtr(this.buf.ptr) + this.buf.len)));
|
|
}
|
|
|
|
return ExternalString.init(this.buf, this.slice, bun.Wyhash11.hash(0, this.slice));
|
|
}
|
|
|
|
pub inline fn value(this: SlicedString) String {
|
|
if (comptime Environment.allow_assert) {
|
|
assert(@intFromPtr(this.buf.ptr) <= @intFromPtr(this.slice.ptr) and ((@intFromPtr(this.slice.ptr) + this.slice.len) <= (@intFromPtr(this.buf.ptr) + this.buf.len)));
|
|
}
|
|
|
|
return String.init(this.buf, this.slice);
|
|
}
|
|
|
|
pub inline fn sub(this: SlicedString, input: string) SlicedString {
|
|
if (Environment.allow_assert) {
|
|
if (!bun.isSliceInBuffer(input, this.buf)) {
|
|
const start_buf = @intFromPtr(this.buf.ptr);
|
|
const end_buf = @intFromPtr(this.buf.ptr) + this.buf.len;
|
|
const start_i = @intFromPtr(input.ptr);
|
|
const end_i = @intFromPtr(input.ptr) + input.len;
|
|
|
|
bun.Output.panic("SlicedString.sub input [{}, {}) is not a substring of the " ++
|
|
"slice [{}, {})", .{ start_i, end_i, start_buf, end_buf });
|
|
}
|
|
}
|
|
return SlicedString{ .buf = this.buf, .slice = input };
|
|
}
|
|
|
|
const string = []const u8;
|
|
|
|
const bun = @import("bun");
|
|
const Environment = bun.Environment;
|
|
const assert = bun.assert;
|
|
|
|
const ExternalString = bun.Semver.ExternalString;
|
|
const String = bun.Semver.String;
|