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)" {...@@ -1373,6 +1373,20 @@ test "mem.tokenize (multibyte)" {
1373 testing.expect(it.next() == null);1373 testing.expect(it.next() == null);
1374}1374}
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
1376/// Returns an iterator that iterates over the slices of `buffer` that1390/// Returns an iterator that iterates over the slices of `buffer` that
1377/// are separated by bytes in `delimiter`.1391/// are separated by bytes in `delimiter`.
1378/// split("abc|def||ghi", "|")1392/// split("abc|def||ghi", "|")
...@@ -1471,6 +1485,11 @@ pub const TokenIterator = struct {...@@ -1471,6 +1485,11 @@ pub const TokenIterator = struct {
1471 return self.buffer[index..];1485 return self.buffer[index..];
1472 }1486 }
14731487
1488 /// Resets the iterator to the initial token.
1489 pub fn reset(self: *TokenIterator) void {
1490 self.index = 0;
1491 }
1492
1474 fn isSplitByte(self: TokenIterator, byte: u8) bool {1493 fn isSplitByte(self: TokenIterator, byte: u8) bool {
1475 for (self.delimiter_bytes) |delimiter_byte| {1494 for (self.delimiter_bytes) |delimiter_byte| {
1476 if (byte == delimiter_byte) {1495 if (byte == delimiter_byte) {