1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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

View File

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