mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
std: add mem.SplitIterator.peek() (#15670)
This commit is contained in:
parent
cc56ab8c68
commit
cd0594e4a6
@ -2241,15 +2241,18 @@ test "splitScalar" {
|
|||||||
try testing.expectEqualSlices(u8, it.first(), "abc");
|
try testing.expectEqualSlices(u8, it.first(), "abc");
|
||||||
|
|
||||||
try testing.expectEqualSlices(u8, it.rest(), "def||ghi");
|
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.next().?, "def");
|
||||||
|
|
||||||
try testing.expectEqualSlices(u8, it.rest(), "|ghi");
|
try testing.expectEqualSlices(u8, it.rest(), "|ghi");
|
||||||
try testing.expectEqualSlices(u8, it.next().?, "");
|
try testing.expectEqualSlices(u8, it.next().?, "");
|
||||||
|
|
||||||
try testing.expectEqualSlices(u8, it.rest(), "ghi");
|
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.next().?, "ghi");
|
||||||
|
|
||||||
try testing.expectEqualSlices(u8, it.rest(), "");
|
try testing.expectEqualSlices(u8, it.rest(), "");
|
||||||
|
try testing.expect(it.peek() == null);
|
||||||
try testing.expect(it.next() == null);
|
try testing.expect(it.next() == null);
|
||||||
|
|
||||||
it = splitScalar(u8, "", '|');
|
it = splitScalar(u8, "", '|');
|
||||||
@ -2259,6 +2262,7 @@ test "splitScalar" {
|
|||||||
it = splitScalar(u8, "|", '|');
|
it = splitScalar(u8, "|", '|');
|
||||||
try testing.expectEqualSlices(u8, it.first(), "");
|
try testing.expectEqualSlices(u8, it.first(), "");
|
||||||
try testing.expectEqualSlices(u8, it.next().?, "");
|
try testing.expectEqualSlices(u8, it.next().?, "");
|
||||||
|
try testing.expect(it.peek() == null);
|
||||||
try testing.expect(it.next() == null);
|
try testing.expect(it.next() == null);
|
||||||
|
|
||||||
it = splitScalar(u8, "hello", ' ');
|
it = splitScalar(u8, "hello", ' ');
|
||||||
@ -2865,6 +2869,18 @@ pub fn SplitIterator(comptime T: type, comptime delimiter_type: DelimiterType) t
|
|||||||
return self.buffer[start..end];
|
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.
|
/// Returns a slice of the remaining bytes. Does not affect iterator state.
|
||||||
pub fn rest(self: Self) []const T {
|
pub fn rest(self: Self) []const T {
|
||||||
const end = self.buffer.len;
|
const end = self.buffer.len;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user