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 { fn testEquality(this: Element, that: Element) bool {
return this == that; return this == that;
} }
}.testEquality)) |index| { }.testEquality)) |head| {
defer self.source = self.source[(index + self.delimiter.len) .. self.source.len]; 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]; 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, pub fn findFirstOf(comptime Element: type, haystack: []const Element,
needle: []const Element, comptime testEquality: fn (Element, Element) bool) ?usize { needle: []const Element, comptime testEquality: fn (Element, Element) bool) ?usize {
var cursor: usize = 0; var head: usize = 0;
const end = (haystack.len - needle.len); const tail = (haystack.len - needle.len);
walk_haystack: while (cursor <= end) : (cursor += 1) { walk_haystack: while (head <= tail) : (head += 1) {
const range = (cursor + needle.len); for (needle) |element, index| {
var index = cursor; if (!testEquality(haystack[head + index], element)) continue: walk_haystack;
}
while (index < range) : (index += 1) return head;
if (testEquality(haystack[index], needle[index])) continue: walk_haystack;
return cursor;
} }
return null; return null;