authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-07-26 13:26:46+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-26 14:26:46+03:00
log1a16b7214d88261f0e38b7ca4d15bcd76caaec4c
treecdcc31edb203667a1e0383b7dfbe454a48b07461
parent20ea44ef107828d76692e610e1ca62ee231fb4a9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.mem: add `reset` to `SplitBackwardsIterator` and `SplitIterator`


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

lib/std/mem.zig+38
......@@ -1779,6 +1779,20 @@ test "split (multibyte)" {
17791779 try testing.expect(it16.next() == null);
17801780}
17811781
1782test "split (reset)" {
1783 var it = split(u8, "abc def ghi", " ");
1784 try testing.expect(eql(u8, it.first(), "abc"));
1785 try testing.expect(eql(u8, it.next().?, "def"));
1786 try testing.expect(eql(u8, it.next().?, "ghi"));
1787
1788 it.reset();
1789
1790 try testing.expect(eql(u8, it.first(), "abc"));
1791 try testing.expect(eql(u8, it.next().?, "def"));
1792 try testing.expect(eql(u8, it.next().?, "ghi"));
1793 try testing.expect(it.next() == null);
1794}
1795
17821796/// Returns an iterator that iterates backwards over the slices of `buffer`
17831797/// that are separated by bytes in `delimiter`.
17841798///
......@@ -1871,6 +1885,20 @@ test "splitBackwards (multibyte)" {
18711885 try testing.expect(it16.next() == null);
18721886}
18731887
1888test "splitBackwards (reset)" {
1889 var it = splitBackwards(u8, "abc def ghi", " ");
1890 try testing.expect(eql(u8, it.first(), "ghi"));
1891 try testing.expect(eql(u8, it.next().?, "def"));
1892 try testing.expect(eql(u8, it.next().?, "abc"));
1893
1894 it.reset();
1895
1896 try testing.expect(eql(u8, it.first(), "ghi"));
1897 try testing.expect(eql(u8, it.next().?, "def"));
1898 try testing.expect(eql(u8, it.next().?, "abc"));
1899 try testing.expect(it.next() == null);
1900}
1901
18741902pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
18751903 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
18761904}
......@@ -1980,6 +2008,11 @@ pub fn SplitIterator(comptime T: type) type {
19802008 const start = self.index orelse end;
19812009 return self.buffer[start..end];
19822010 }
2011
2012 /// Resets the iterator to the initial slice.
2013 pub fn reset(self: *Self) void {
2014 self.index = 0;
2015 }
19832016 };
19842017}
19852018
......@@ -2016,6 +2049,11 @@ pub fn SplitBackwardsIterator(comptime T: type) type {
20162049 const end = self.index orelse 0;
20172050 return self.buffer[0..end];
20182051 }
2052
2053 /// Resets the iterator to the initial slice.
2054 pub fn reset(self: *Self) void {
2055 self.index = self.buffer.len;
2056 }
20192057 };
20202058}
20212059