commit 1a16b7214d88261f0e38b7ca4d15bcd76caaec4c (tree)
parent 20ea44ef107828d76692e610e1ca62ee231fb4a9
Author: r00ster <r00ster91@proton.me>
Date: Tue, 26 Jul 2022 13:26:46 +0200
std.mem: add `reset` to `SplitBackwardsIterator` and `SplitIterator`
Diffstat:
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/lib/std/mem.zig b/lib/std/mem.zig
@@ -1779,6 +1779,20 @@ test "split (multibyte)" {
try testing.expect(it16.next() == null);
}
+test "split (reset)" {
+ var it = split(u8, "abc def ghi", " ");
+ try testing.expect(eql(u8, it.first(), "abc"));
+ try testing.expect(eql(u8, it.next().?, "def"));
+ try testing.expect(eql(u8, it.next().?, "ghi"));
+
+ it.reset();
+
+ try testing.expect(eql(u8, it.first(), "abc"));
+ try testing.expect(eql(u8, it.next().?, "def"));
+ try testing.expect(eql(u8, it.next().?, "ghi"));
+ try testing.expect(it.next() == null);
+}
+
/// Returns an iterator that iterates backwards over the slices of `buffer`
/// that are separated by bytes in `delimiter`.
///
@@ -1871,6 +1885,20 @@ test "splitBackwards (multibyte)" {
try testing.expect(it16.next() == null);
}
+test "splitBackwards (reset)" {
+ var it = splitBackwards(u8, "abc def ghi", " ");
+ try testing.expect(eql(u8, it.first(), "ghi"));
+ try testing.expect(eql(u8, it.next().?, "def"));
+ try testing.expect(eql(u8, it.next().?, "abc"));
+
+ it.reset();
+
+ try testing.expect(eql(u8, it.first(), "ghi"));
+ try testing.expect(eql(u8, it.next().?, "def"));
+ try testing.expect(eql(u8, it.next().?, "abc"));
+ try testing.expect(it.next() == null);
+}
+
pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
}
@@ -1980,6 +2008,11 @@ pub fn SplitIterator(comptime T: type) type {
const start = self.index orelse end;
return self.buffer[start..end];
}
+
+ /// Resets the iterator to the initial slice.
+ pub fn reset(self: *Self) void {
+ self.index = 0;
+ }
};
}
@@ -2016,6 +2049,11 @@ pub fn SplitBackwardsIterator(comptime T: type) type {
const end = self.index orelse 0;
return self.buffer[0..end];
}
+
+ /// Resets the iterator to the initial slice.
+ pub fn reset(self: *Self) void {
+ self.index = self.buffer.len;
+ }
};
}