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:
parent
4b6244c9c1
commit
b4f7a975e8
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user