authorgravatar for nuno@nunoleiria.comNuno Leiria <nuno@nunoleiria.com> 2021-03-20 23:28:02+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-20 22:01:09-07:00
log0d96a284e80fc3d79af9d56bacb05456b9ed3da4
tree59663817a5bc49b75df787e96e9e5d759d96a12e
parent39133321451682d6610b09161114ab96ebdb3d2d

std: Add reset to TokenIterator


1 files changed, 19 insertions(+), 0 deletions(-)

lib/std/mem.zig+19
......@@ -1373,6 +1373,20 @@ test "mem.tokenize (multibyte)" {
13731373 testing.expect(it.next() == null);
13741374}
13751375
1376test "mem.tokenize (reset)" {
1377 var it = tokenize(" abc def ghi ", " ");
1378 testing.expect(eql(u8, it.next().?, "abc"));
1379 testing.expect(eql(u8, it.next().?, "def"));
1380 testing.expect(eql(u8, it.next().?, "ghi"));
1381
1382 it.reset();
1383
1384 testing.expect(eql(u8, it.next().?, "abc"));
1385 testing.expect(eql(u8, it.next().?, "def"));
1386 testing.expect(eql(u8, it.next().?, "ghi"));
1387 testing.expect(it.next() == null);
1388}
1389
13761390/// Returns an iterator that iterates over the slices of `buffer` that
13771391/// are separated by bytes in `delimiter`.
13781392/// split("abc|def||ghi", "|")
......@@ -1471,6 +1485,11 @@ pub const TokenIterator = struct {
14711485 return self.buffer[index..];
14721486 }
14731487
1488 /// Resets the iterator to the initial token.
1489 pub fn reset(self: *TokenIterator) void {
1490 self.index = 0;
1491 }
1492
14741493 fn isSplitByte(self: TokenIterator, byte: u8) bool {
14751494 for (self.delimiter_bytes) |delimiter_byte| {
14761495 if (byte == delimiter_byte) {