| ... | @@ -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 | } |
| 1781 | | 1781 | |
| | 1782 | test "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 | } |
| 1873 | | 1887 | |
| | 1888 | test "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 | |
| 1874 | pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool { | 1902 | pub 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 | } |
| 1985 | | 2018 | |
| ... | @@ -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 | } |
| 2021 | | 2059 | |