Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2229,15 +2229,18 @@ test "splitScalar" {
try testing.expectEqualSlices(u8, it.first(), "abc");

try testing.expectEqualSlices(u8, it.rest(), "def||ghi");
try testing.expectEqualSlices(u8, it.peek().?, "def");
try testing.expectEqualSlices(u8, it.next().?, "def");

try testing.expectEqualSlices(u8, it.rest(), "|ghi");
try testing.expectEqualSlices(u8, it.next().?, "");

try testing.expectEqualSlices(u8, it.rest(), "ghi");
try testing.expectEqualSlices(u8, it.peek().?, "ghi");
try testing.expectEqualSlices(u8, it.next().?, "ghi");

try testing.expectEqualSlices(u8, it.rest(), "");
try testing.expect(it.peek() == null);
try testing.expect(it.next() == null);

it = splitScalar(u8, "", '|');
Expand All @@ -2247,6 +2250,7 @@ test "splitScalar" {
it = splitScalar(u8, "|", '|');
try testing.expectEqualSlices(u8, it.first(), "");
try testing.expectEqualSlices(u8, it.next().?, "");
try testing.expect(it.peek() == null);
try testing.expect(it.next() == null);

it = splitScalar(u8, "hello", ' ');
Expand Down Expand Up @@ -2853,6 +2857,18 @@ pub fn SplitIterator(comptime T: type, comptime delimiter_type: DelimiterType) t
return self.buffer[start..end];
}

/// Returns a slice of the next field, or null if splitting is complete.
/// This method does not alter self.index.
pub fn peek(self: *Self) ?[]const T {
const start = self.index orelse return null;
const end = if (switch (delimiter_type) {
.sequence => indexOfPos(T, self.buffer, start, self.delimiter),
.any => indexOfAnyPos(T, self.buffer, start, self.delimiter),
.scalar => indexOfScalarPos(T, self.buffer, start, self.delimiter),
}) |delim_start| delim_start else self.buffer.len;
return self.buffer[start..end];
}

/// Returns a slice of the remaining bytes. Does not affect iterator state.
pub fn rest(self: Self) []const T {
const end = self.buffer.len;
Expand Down