Takes a pointer to an array, a many-item pointer, or a slice, and returns a
slice of the items up to the first occurrence of end.
If end is not found, the resulting slice will include all items up to the
input's length or sentinel.
If the pointer type is unbounded (no length or sentinel), end will be the
sentinel for the resulting slice.
If the pointer type is sentinel-terminated by end, the resulting slice
will also be sentinel-terminated by end.
Pointer properties such as mutability and alignment are preserved.
C pointers are assumed to be non-null.
pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo(@TypeOf(ptr), end)
pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo(@TypeOf(ptr), end) {
if (@typeInfo(@TypeOf(ptr)) == .optional) {
const non_null = ptr orelse return null;
return sliceTo(non_null, end);
}
const Result = SliceTo(@TypeOf(ptr), end);
const length = lenSliceTo(ptr, end);
const ptr_info = @typeInfo(Result).pointer;
if (ptr_info.sentinel()) |s| {
return ptr[0..length :s];
} else {
return ptr[0..length];
}
}