mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
string: fix bug and add tests for string.count
This commit is contained in:
parent
93d27b0b9f
commit
331d6f98ee
@ -462,7 +462,7 @@ pub fn (s string) count(substr string) int {
|
|||||||
mut n := 0
|
mut n := 0
|
||||||
mut i := 0
|
mut i := 0
|
||||||
for {
|
for {
|
||||||
i := s.index_after(substr, i)
|
i = s.index_after(substr, i)
|
||||||
if i == -1 {
|
if i == -1 {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
@ -338,3 +338,13 @@ fn test_bytes_to_string() {
|
|||||||
bytes := [`h`, `e`, `l`, `l`, `o`]
|
bytes := [`h`, `e`, `l`, `l`, `o`]
|
||||||
assert string(bytes, 5) == 'hello'
|
assert string(bytes, 5) == 'hello'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_count() {
|
||||||
|
assert ''.count('') == 0
|
||||||
|
assert ''.count('a') == 0
|
||||||
|
assert 'a'.count('') == 0
|
||||||
|
assert 'aa'.count('a') == 2
|
||||||
|
assert 'aa'.count('aa') == 1
|
||||||
|
assert 'aabbaa'.count('aa') == 2
|
||||||
|
assert 'bbaabb'.count('aa') == 1
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user