builtin.string: optimize string.count where substr.len == 1 (#9337)

pull/9343/head
shadowninja55 2021-03-16 18:19:48 -04:00 committed by GitHub
parent 4b6244c9c1
commit b4f7a975e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -834,7 +834,21 @@ pub fn (s string) count(substr string) int {
if substr.len > s.len {
return 0
}
mut n := 0
if substr.len == 1 {
target := substr[0]
for letter in s {
if letter == target {
n++
}
}
return n
}
mut i := 0
for {
i = s.index_after(substr, i)