authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-06-21 18:54:17 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-06-21 18:54:17 -07:00
log4fe97f8f42ae9cf88ef90706d39bcc2bb5a94a10
tree9f703ff4d8879db0992923299d50dd457ee48d85
parent94af3029c54900384512739110423f2ca9a0e317

faster countScalar


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

src/countScalar.zig+5-4
......@@ -3,13 +3,14 @@ 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;
67 var found: usize = 0;
78
8 for (haystack) |item| {
9 if (item == needle) {
10 found += 1;
11 }
9 while (std.mem.indexOfScalarPos(T, haystack, i, needle)) |idx| {
10 i = idx + 1;
11 found += 1;
1212 }
13
1314 return found;
1415}
1516