Fix implementation of findFirstOf and Spliterator

This commit is contained in:
kayomn 2022-11-01 22:25:19 +00:00
parent dd81ae76ec
commit 14e5e18228
1 changed files with 12 additions and 12 deletions

View File

@ -63,10 +63,12 @@ pub fn Spliterator(comptime Element: type) type {
fn testEquality(this: Element, that: Element) bool {
return this == that;
}
}.testEquality)) |index| {
defer self.source = self.source[(index + self.delimiter.len) .. self.source.len];
}.testEquality)) |head| {
const tail = (head + self.delimiter.len);
return self.source[0 .. index];
defer self.source = self.source[tail .. self.source.len];
return self.source[0 .. (tail - 1)];
}
defer self.source = self.source[self.source.len .. self.source.len];
@ -254,17 +256,15 @@ test "Find first of element" {
pub fn findFirstOf(comptime Element: type, haystack: []const Element,
needle: []const Element, comptime testEquality: fn (Element, Element) bool) ?usize {
var cursor: usize = 0;
const end = (haystack.len - needle.len);
var head: usize = 0;
const tail = (haystack.len - needle.len);
walk_haystack: while (cursor <= end) : (cursor += 1) {
const range = (cursor + needle.len);
var index = cursor;
walk_haystack: while (head <= tail) : (head += 1) {
for (needle) |element, index| {
if (!testEquality(haystack[head + index], element)) continue: walk_haystack;
}
while (index < range) : (index += 1)
if (testEquality(haystack[index], needle[index])) continue: walk_haystack;
return cursor;
return head;
}
return null;