From 14e5e18228a1013cee42ac3a51e5800f93e91ba5 Mon Sep 17 00:00:00 2001 From: kayomn Date: Tue, 1 Nov 2022 22:25:19 +0000 Subject: [PATCH] Fix implementation of findFirstOf and Spliterator --- src/core/io.zig | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/core/io.zig b/src/core/io.zig index 495fd7d..ad814dc 100644 --- a/src/core/io.zig +++ b/src/core/io.zig @@ -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;