| ... | @@ -2241,15 +2241,18 @@ test "splitScalar" { | ... | @@ -2241,15 +2241,18 @@ test "splitScalar" { |
| 2241 | try testing.expectEqualSlices(u8, it.first(), "abc"); | 2241 | try testing.expectEqualSlices(u8, it.first(), "abc"); |
| 2242 | | 2242 | |
| 2243 | try testing.expectEqualSlices(u8, it.rest(), "def||ghi"); | 2243 | try testing.expectEqualSlices(u8, it.rest(), "def||ghi"); |
| | 2244 | try testing.expectEqualSlices(u8, it.peek().?, "def"); |
| 2244 | try testing.expectEqualSlices(u8, it.next().?, "def"); | 2245 | try testing.expectEqualSlices(u8, it.next().?, "def"); |
| 2245 | | 2246 | |
| 2246 | try testing.expectEqualSlices(u8, it.rest(), "|ghi"); | 2247 | try testing.expectEqualSlices(u8, it.rest(), "|ghi"); |
| 2247 | try testing.expectEqualSlices(u8, it.next().?, ""); | 2248 | try testing.expectEqualSlices(u8, it.next().?, ""); |
| 2248 | | 2249 | |
| 2249 | try testing.expectEqualSlices(u8, it.rest(), "ghi"); | 2250 | try testing.expectEqualSlices(u8, it.rest(), "ghi"); |
| | 2251 | try testing.expectEqualSlices(u8, it.peek().?, "ghi"); |
| 2250 | try testing.expectEqualSlices(u8, it.next().?, "ghi"); | 2252 | try testing.expectEqualSlices(u8, it.next().?, "ghi"); |
| 2251 | | 2253 | |
| 2252 | try testing.expectEqualSlices(u8, it.rest(), ""); | 2254 | try testing.expectEqualSlices(u8, it.rest(), ""); |
| | 2255 | try testing.expect(it.peek() == null); |
| 2253 | try testing.expect(it.next() == null); | 2256 | try testing.expect(it.next() == null); |
| 2254 | | 2257 | |
| 2255 | it = splitScalar(u8, "", '|'); | 2258 | it = splitScalar(u8, "", '|'); |
| ... | @@ -2259,6 +2262,7 @@ test "splitScalar" { | ... | @@ -2259,6 +2262,7 @@ test "splitScalar" { |
| 2259 | it = splitScalar(u8, "|", '|'); | 2262 | it = splitScalar(u8, "|", '|'); |
| 2260 | try testing.expectEqualSlices(u8, it.first(), ""); | 2263 | try testing.expectEqualSlices(u8, it.first(), ""); |
| 2261 | try testing.expectEqualSlices(u8, it.next().?, ""); | 2264 | try testing.expectEqualSlices(u8, it.next().?, ""); |
| | 2265 | try testing.expect(it.peek() == null); |
| 2262 | try testing.expect(it.next() == null); | 2266 | try testing.expect(it.next() == null); |
| 2263 | | 2267 | |
| 2264 | it = splitScalar(u8, "hello", ' '); | 2268 | it = splitScalar(u8, "hello", ' '); |
| ... | @@ -2865,6 +2869,18 @@ pub fn SplitIterator(comptime T: type, comptime delimiter_type: DelimiterType) t | ... | @@ -2865,6 +2869,18 @@ pub fn SplitIterator(comptime T: type, comptime delimiter_type: DelimiterType) t |
| 2865 | return self.buffer[start..end]; | 2869 | return self.buffer[start..end]; |
| 2866 | } | 2870 | } |
| 2867 | | 2871 | |
| | 2872 | /// Returns a slice of the next field, or null if splitting is complete. |
| | 2873 | /// This method does not alter self.index. |
| | 2874 | pub fn peek(self: *Self) ?[]const T { |
| | 2875 | const start = self.index orelse return null; |
| | 2876 | const end = if (switch (delimiter_type) { |
| | 2877 | .sequence => indexOfPos(T, self.buffer, start, self.delimiter), |
| | 2878 | .any => indexOfAnyPos(T, self.buffer, start, self.delimiter), |
| | 2879 | .scalar => indexOfScalarPos(T, self.buffer, start, self.delimiter), |
| | 2880 | }) |delim_start| delim_start else self.buffer.len; |
| | 2881 | return self.buffer[start..end]; |
| | 2882 | } |
| | 2883 | |
| 2868 | /// Returns a slice of the remaining bytes. Does not affect iterator state. | 2884 | /// Returns a slice of the remaining bytes. Does not affect iterator state. |
| 2869 | pub fn rest(self: Self) []const T { | 2885 | pub fn rest(self: Self) []const T { |
| 2870 | const end = self.buffer.len; | 2886 | const end = self.buffer.len; |