Commit 0d96a284e8

Nuno Leiria <nuno@nunoleiria.com>
2021-03-21 00:28:02
std: Add reset to TokenIterator
1 parent 3913332
Changed files (1)
lib
lib/std/mem.zig
@@ -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) {