Function count [src]
Returns the number of needles inside the haystack
needle.len must be > 0
does not count overlapping needles
Prototype
pub fn count(comptime T: type, haystack: []const T, needle: []const T) usize Parameters
T: typehaystack: []const Tneedle: []const T Example
test count {
try testing.expect(count(u8, "", "h") == 0);
try testing.expect(count(u8, "h", "h") == 1);
try testing.expect(count(u8, "hh", "h") == 2);
try testing.expect(count(u8, "world!", "hello") == 0);
try testing.expect(count(u8, "hello world!", "hello") == 1);
try testing.expect(count(u8, " abcabc abc", "abc") == 3);
try testing.expect(count(u8, "udexdcbvbruhasdrw", "bruh") == 1);
try testing.expect(count(u8, "foo bar", "o bar") == 1);
try testing.expect(count(u8, "foofoofoo", "foo") == 3);
try testing.expect(count(u8, "fffffff", "ff") == 3);
try testing.expect(count(u8, "owowowu", "owowu") == 1);
} Source
pub fn count(comptime T: type, haystack: []const T, needle: []const T) usize {
assert(needle.len > 0);
var i: usize = 0;
var found: usize = 0;
while (indexOfPos(T, haystack, i, needle)) |idx| {
i = idx + needle.len;
found += 1;
}
return found;
}