Skip to content

Commit

Permalink
builtin: fix 'aaaa'.split('aa') (fix #21936) (#21951)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Jul 28, 2024
1 parent b5452e0 commit bf04adc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -977,14 +977,16 @@ pub fn (s string) split_nth(delim string, nth int) []string {
else {
mut start := 0
// Add up to `nth` segments left of every occurrence of the delimiter.
for i := 0; i + delim.len <= s.len; i++ {
for i := 0; i + delim.len <= s.len; {
if unsafe { s.substr_unsafe(i, i + delim.len) } == delim {
if nth > 0 && res.len == nth - 1 {
break
}
res << s.substr(start, i)
i += delim.len
start = i
} else {
i++
}
}
// Then add the remaining part of the string as the last segment.
Expand Down
7 changes: 7 additions & 0 deletions vlib/builtin/string_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ fn test_split() {
assert vals.len == 2
assert vals[0] == 'wavy turquoise'
assert vals[1] == ''
// /////////
s = 'aaaa'
vals = s.split('aa')
assert vals.len == 3
assert vals[0] == ''
assert vals[1] == ''
assert vals[2] == ''
}

fn test_rsplit() {
Expand Down

0 comments on commit bf04adc

Please sign in to comment.