authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-03 20:58:00 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-03 20:58:00 -08:00
loga6e8db462de2eab7636ee26ed4c1f8b238c55f96
tree9878afdf823d6f890d740b4f838e889af8fc5018
parentf2c237f7944dfcd72492a1123f5860088c64726c

countScalar: for loop is faster

see zig/pull/24104

1 files changed, 4 insertions(+), 6 deletions(-)

src/countScalar.zig+4-6
...@@ -3,14 +3,12 @@ const string = []const u8;...@@ -3,14 +3,12 @@ const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
44
5pub fn countScalar(comptime T: type, haystack: []const T, needle: T) usize {5pub fn countScalar(comptime T: type, haystack: []const T, needle: T) usize {
6 var i: usize = 0;
7 var found: usize = 0;6 var found: usize = 0;
87 for (haystack) |item| {
9 while (std.mem.indexOfScalarPos(T, haystack, i, needle)) |idx| {8 if (item == needle) {
10 i = idx + 1;9 found += 1;
11 found += 1;10 }
12 }11 }
13
14 return found;12 return found;
15}13}
1614