std: Add reset to TokenIterator

This commit is contained in:
Nuno Leiria 2021-03-20 23:28:02 +00:00 committed by Andrew Kelley
parent 3913332145
commit 0d96a284e8

View File

@ -1373,6 +1373,20 @@ test "mem.tokenize (multibyte)" {
testing.expect(it.next() == null);
}
test "mem.tokenize (reset)" {
var it = tokenize(" abc def ghi ", " ");
testing.expect(eql(u8, it.next().?, "abc"));
testing.expect(eql(u8, it.next().?, "def"));
testing.expect(eql(u8, it.next().?, "ghi"));
it.reset();
testing.expect(eql(u8, it.next().?, "abc"));
testing.expect(eql(u8, it.next().?, "def"));
testing.expect(eql(u8, it.next().?, "ghi"));
testing.expect(it.next() == null);
}
/// Returns an iterator that iterates over the slices of `buffer` that
/// are separated by bytes in `delimiter`.
/// split("abc|def||ghi", "|")
@ -1471,6 +1485,11 @@ pub const TokenIterator = struct {
return self.buffer[index..];
}
/// Resets the iterator to the initial token.
pub fn reset(self: *TokenIterator) void {
self.index = 0;
}
fn isSplitByte(self: TokenIterator, byte: u8) bool {
for (self.delimiter_bytes) |delimiter_byte| {
if (byte == delimiter_byte) {