From 331d6f98eeb04448b948ada3eba38d98a4a04620 Mon Sep 17 00:00:00 2001 From: Alvydas Vitkauskas Date: Sat, 3 Aug 2019 23:24:03 +0300 Subject: [PATCH] string: fix bug and add tests for string.count --- vlib/builtin/string.v | 2 +- vlib/builtin/string_test.v | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 20b9b9dec7..78d3f23d3e 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -462,7 +462,7 @@ pub fn (s string) count(substr string) int { mut n := 0 mut i := 0 for { - i := s.index_after(substr, i) + i = s.index_after(substr, i) if i == -1 { return n } diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 38e63c3983..9fdd924136 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -338,3 +338,13 @@ fn test_bytes_to_string() { bytes := [`h`, `e`, `l`, `l`, `o`] 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 +}