Function sliceTo [src]

Takes a pointer to an array, a sentinel-terminated pointer, or a slice and iterates searching for the first occurrence of end, returning the scanned slice. If end is not found, the full length of the array/slice/sentinel terminated pointer is returned. If the pointer type is sentinel terminated and end matches that terminator, the resulting slice is also sentinel terminated. Pointer properties such as mutability and alignment are preserved. C pointers are assumed to be non-null.

Prototype

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

Parameters

end: std.meta.Elem(@TypeOf(ptr))

Example

test sliceTo { try testing.expectEqualSlices(u8, "aoeu", sliceTo("aoeu", 0)); { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; try testing.expectEqualSlices(u16, &array, sliceTo(&array, 0)); try testing.expectEqualSlices(u16, array[0..3], sliceTo(array[0..3], 0)); try testing.expectEqualSlices(u16, array[0..2], sliceTo(&array, 3)); try testing.expectEqualSlices(u16, array[0..2], sliceTo(array[0..3], 3)); const sentinel_ptr = @as([*:5]u16, @ptrCast(&array)); try testing.expectEqualSlices(u16, array[0..2], sliceTo(sentinel_ptr, 3)); try testing.expectEqualSlices(u16, array[0..4], sliceTo(sentinel_ptr, 99)); const optional_sentinel_ptr = @as(?[*:5]u16, @ptrCast(&array)); try testing.expectEqualSlices(u16, array[0..2], sliceTo(optional_sentinel_ptr, 3).?); try testing.expectEqualSlices(u16, array[0..4], sliceTo(optional_sentinel_ptr, 99).?); const c_ptr = @as([*c]u16, &array); try testing.expectEqualSlices(u16, array[0..2], sliceTo(c_ptr, 3)); const slice: []u16 = &array; try testing.expectEqualSlices(u16, array[0..2], sliceTo(slice, 3)); try testing.expectEqualSlices(u16, &array, sliceTo(slice, 99)); const sentinel_slice: [:5]u16 = array[0..4 :5]; try testing.expectEqualSlices(u16, array[0..2], sliceTo(sentinel_slice, 3)); try testing.expectEqualSlices(u16, array[0..4], sliceTo(sentinel_slice, 99)); } { var sentinel_array: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 }; try testing.expectEqualSlices(u16, sentinel_array[0..2], sliceTo(&sentinel_array, 3)); try testing.expectEqualSlices(u16, &sentinel_array, sliceTo(&sentinel_array, 0)); try testing.expectEqualSlices(u16, &sentinel_array, sliceTo(&sentinel_array, 99)); } try testing.expectEqual(@as(?[]u8, null), sliceTo(@as(?[]u8, null), 0)); }

Source

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