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)" {...@@ -1779,6 +1779,20 @@ test "split (multibyte)" {
1779 try testing.expect(it16.next() == null);1779 try testing.expect(it16.next() == null);
1780}1780}
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
1782/// Returns an iterator that iterates backwards over the slices of `buffer`1796/// Returns an iterator that iterates backwards over the slices of `buffer`
1783/// that are separated by bytes in `delimiter`.1797/// that are separated by bytes in `delimiter`.
1784///1798///
...@@ -1871,6 +1885,20 @@ test "splitBackwards (multibyte)" {...@@ -1871,6 +1885,20 @@ test "splitBackwards (multibyte)" {
1871 try testing.expect(it16.next() == null);1885 try testing.expect(it16.next() == null);
1872}1886}
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
1874pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool {1902pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
1875 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);1903 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
1876}1904}
...@@ -1980,6 +2008,11 @@ pub fn SplitIterator(comptime T: type) type {...@@ -1980,6 +2008,11 @@ pub fn SplitIterator(comptime T: type) type {
1980 const start = self.index orelse end;2008 const start = self.index orelse end;
1981 return self.buffer[start..end];2009 return self.buffer[start..end];
1982 }2010 }
2011
2012 /// Resets the iterator to the initial slice.
2013 pub fn reset(self: *Self) void {
2014 self.index = 0;
2015 }
1983 };2016 };
1984}2017}
19852018
...@@ -2016,6 +2049,11 @@ pub fn SplitBackwardsIterator(comptime T: type) type {...@@ -2016,6 +2049,11 @@ pub fn SplitBackwardsIterator(comptime T: type) type {
2016 const end = self.index orelse 0;2049 const end = self.index orelse 0;
2017 return self.buffer[0..end];2050 return self.buffer[0..end];
2018 }2051 }
2052
2053 /// Resets the iterator to the initial slice.
2054 pub fn reset(self: *Self) void {
2055 self.index = self.buffer.len;
2056 }
2019 };2057 };
2020}2058}
20212059