Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

sliceTo

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.

mem.sliceTo
pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo(@TypeOf(ptr), end)

File

lib/std/mem.zig:963

Code

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];
    }
}