Add String.count to count the number of instances of a string.

This commit is contained in:
Christoffer Lerno
2025-05-02 21:48:04 +02:00
parent a411f20762
commit d313afa487
3 changed files with 49 additions and 0 deletions

View File

@@ -375,6 +375,38 @@ fn bool String.contains(s, String substr)
return @ok(s.index_of(substr));
}
<*
Check how many non-overlapping instances of a substring there is.
If the substring has zero length, the number of matches is zero.
@param [in] self : "The string to search"
@param [in] substr : "The string to look for."
@pure
@return "The number of times matched"
*>
fn usz String.count(self, String substr)
{
usz count = 0;
usz needed = substr.len;
if (needed == 0) return 0;
char first = substr[0];
while OUTER: (self.len >= needed)
{
foreach (i, c: self[..^needed])
{
if (c == first && self[i : needed] == substr)
{
count++;
self = self[i + needed..];
continue OUTER;
}
}
break;
}
return count;
}
<*
Find the index of the first incidence of a string.