style: add Offset.Slice.slice helper fn

Makes code that interacts with these so much cleaner
pull/9609/head
Qwerasd 2025-11-16 10:32:57 -07:00
parent 985e1a3cea
commit 00c2216fe1
6 changed files with 26 additions and 21 deletions

View File

@ -4103,7 +4103,7 @@ fn osc8URI(self: *Surface, pin: terminal.Pin) ?[]const u8 {
const cell = pin.rowAndCell().cell;
const link_id = page.lookupHyperlink(cell) orelse return null;
const entry = page.hyperlink_set.get(page.memory, link_id);
return entry.uri.offset.ptr(page.memory)[0..entry.uri.len];
return entry.uri.slice(page.memory);
}
pub fn mousePressureCallback(

View File

@ -131,7 +131,7 @@ pub const Set = struct {
// then we use an alternate matching technique that iterates forward
// and backward until it finds boundaries.
if (link.id == .implicit) {
const uri = link.uri.offset.ptr(page.memory)[0..link.uri.len];
const uri = link.uri.slice(page.memory);
return try self.matchSetFromOSC8Implicit(
alloc,
matches,
@ -232,7 +232,7 @@ pub const Set = struct {
if (link.id != .implicit) break;
// If this link has a different URI then we found a boundary
const cell_uri = link.uri.offset.ptr(page.memory)[0..link.uri.len];
const cell_uri = link.uri.slice(page.memory);
if (!std.mem.eql(u8, uri, cell_uri)) break;
sel.startPtr().* = cell_pin;
@ -258,7 +258,7 @@ pub const Set = struct {
if (link.id != .implicit) break;
// If this link has a different URI then we found a boundary
const cell_uri = link.uri.offset.ptr(page.memory)[0..link.uri.len];
const cell_uri = link.uri.slice(page.memory);
if (!std.mem.eql(u8, uri, cell_uri)) break;
sel.endPtr().* = cell_pin;

View File

@ -1050,9 +1050,9 @@ pub fn cursorCopy(self: *Screen, other: Cursor, opts: struct {
const other_page = &other.page_pin.node.data;
const other_link = other_page.hyperlink_set.get(other_page.memory, other.hyperlink_id);
const uri = other_link.uri.offset.ptr(other_page.memory)[0..other_link.uri.len];
const uri = other_link.uri.slice(other_page.memory);
const id_ = switch (other_link.id) {
.explicit => |id| id.offset.ptr(other_page.memory)[0..id.len],
.explicit => |id| id.slice(other_page.memory),
.implicit => null,
};

View File

@ -103,7 +103,7 @@ pub const PageEntry = struct {
// Copy the URI
{
const uri = self.uri.offset.ptr(self_page.memory)[0..self.uri.len];
const uri = self.uri.slice(self_page.memory);
const buf = try dst_page.string_alloc.alloc(u8, dst_page.memory, uri.len);
@memcpy(buf, uri);
copy.uri = .{
@ -113,14 +113,14 @@ pub const PageEntry = struct {
}
errdefer dst_page.string_alloc.free(
dst_page.memory,
copy.uri.offset.ptr(dst_page.memory)[0..copy.uri.len],
copy.uri.slice(dst_page.memory),
);
// Copy the ID
switch (copy.id) {
.implicit => {}, // Shallow is fine
.explicit => |slice| {
const id = slice.offset.ptr(self_page.memory)[0..slice.len];
const id = slice.slice(self_page.memory);
const buf = try dst_page.string_alloc.alloc(u8, dst_page.memory, id.len);
@memcpy(buf, id);
copy.id = .{ .explicit = .{
@ -133,7 +133,7 @@ pub const PageEntry = struct {
.implicit => {},
.explicit => |v| dst_page.string_alloc.free(
dst_page.memory,
v.offset.ptr(dst_page.memory)[0..v.len],
v.slice(dst_page.memory),
),
};
@ -147,13 +147,13 @@ pub const PageEntry = struct {
.implicit => |v| autoHash(&hasher, v),
.explicit => |slice| autoHashStrat(
&hasher,
slice.offset.ptr(base)[0..slice.len],
slice.slice(base),
.Deep,
),
}
autoHashStrat(
&hasher,
self.uri.offset.ptr(base)[0..self.uri.len],
self.uri.slice(base),
.Deep,
);
return hasher.final();
@ -181,8 +181,8 @@ pub const PageEntry = struct {
return std.mem.eql(
u8,
self.uri.offset.ptr(self_base)[0..self.uri.len],
other.uri.offset.ptr(other_base)[0..other.uri.len],
self.uri.slice(self_base),
other.uri.slice(other_base),
);
}
@ -196,12 +196,12 @@ pub const PageEntry = struct {
.implicit => {},
.explicit => |v| alloc.free(
page.memory,
v.offset.ptr(page.memory)[0..v.len],
v.slice(page.memory),
),
}
alloc.free(
page.memory,
self.uri.offset.ptr(page.memory)[0..self.uri.len],
self.uri.slice(page.memory),
);
}
};

View File

@ -1198,7 +1198,7 @@ pub const Page = struct {
};
errdefer self.string_alloc.free(
self.memory,
page_uri.offset.ptr(self.memory)[0..page_uri.len],
page_uri.slice(self.memory),
);
// Allocate an ID for our page memory if we have to.
@ -1228,7 +1228,7 @@ pub const Page = struct {
.implicit => {},
.explicit => |slice| self.string_alloc.free(
self.memory,
slice.offset.ptr(self.memory)[0..slice.len],
slice.slice(self.memory),
),
};
@ -1421,7 +1421,7 @@ pub const Page = struct {
// most graphemes to fit within our chunk size.
const cps = try self.grapheme_alloc.alloc(u21, self.memory, slice.len + 1);
errdefer self.grapheme_alloc.free(self.memory, cps);
const old_cps = slice.offset.ptr(self.memory)[0..slice.len];
const old_cps = slice.slice(self.memory);
fastmem.copy(u21, cps[0..old_cps.len], old_cps);
cps[slice.len] = cp;
slice.* = .{
@ -1440,7 +1440,7 @@ pub const Page = struct {
const cell_offset = getOffset(Cell, self.memory, cell);
const map = self.grapheme_map.map(self.memory);
const slice = map.get(cell_offset) orelse return null;
return slice.offset.ptr(self.memory)[0..slice.len];
return slice.slice(self.memory);
}
/// Move the graphemes from one cell to another. This can't fail
@ -1475,7 +1475,7 @@ pub const Page = struct {
const entry = map.getEntry(cell_offset).?;
// Free our grapheme data
const cps = entry.value_ptr.offset.ptr(self.memory)[0..entry.value_ptr.len];
const cps = entry.value_ptr.slice(self.memory);
self.grapheme_alloc.free(self.memory, cps);
// Remove the entry

View File

@ -28,6 +28,11 @@ pub fn Offset(comptime T: type) type {
pub const Slice = struct {
offset: Self = .{},
len: usize = 0,
/// Returns a slice for the data, properly typed.
pub inline fn slice(self: Slice, base: anytype) []T {
return self.offset.ptr(base)[0..self.len];
}
};
/// Returns a pointer to the start of the data, properly typed.