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;
33const extras = @import("./lib.zig");
44
55pub fn countScalar(comptime T: type, haystack: []const T, needle: T) usize {
6 var i: usize = 0;
76 var found: usize = 0;
8
9 while (std.mem.indexOfScalarPos(T, haystack, i, needle)) |idx| {
10 i = idx + 1;
11 found += 1;
7 for (haystack) |item| {
8 if (item == needle) {
9 found += 1;
10 }
1211 }
13
1412 return found;
1513}
1614