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

string: fix a bug with split()

This commit is contained in:
Alexander Medvednikov 2019-07-31 07:26:22 +02:00
parent a4882f1955
commit e0b45e33ea
2 changed files with 12 additions and 7 deletions

View File

@ -238,8 +238,6 @@ pub fn (s string) split(delim string) []string {
} }
if delim.len == 1 { if delim.len == 1 {
return s.split_single(delim[0]) return s.split_single(delim[0])
// println('split 1 only')
// os.exit()
} }
mut i := 0 mut i := 0
mut start := 0// - 1 mut start := 0// - 1
@ -283,15 +281,15 @@ pub fn (s string) split_single(delim byte) []string {
mut i := 0 mut i := 0
mut start := 0 mut start := 0
for i < s.len { for i < s.len {
a := s[i] == delim is_delim := s[i] == delim
b := i == s.len - 1 last := i == s.len - 1
if a || b { if is_delim || last {
if i == s.len - 1 { if !is_delim && i == s.len - 1 {
i++ i++
} }
val := s.substr(start, i) val := s.substr(start, i)
if val.len > 0 { if val.len > 0 {
res << val.trim_space() res << val
} }
start = i + 1 start = i + 1
} }

View File

@ -106,6 +106,13 @@ fn test_split() {
assert vals[0]== '4627a862c3dec29fb3182a06b8965e0025759e18' assert vals[0]== '4627a862c3dec29fb3182a06b8965e0025759e18'
assert vals[1]=='1530207969' assert vals[1]=='1530207969'
assert vals[2]== 'blue' assert vals[2]== 'blue'
// /////////
s = 'lalala'
vals = s.split('a')
assert vals.len == 3
assert vals[0] == 'l'
assert vals[1] == 'l'
assert vals[2] == 'l'
} }
fn test_trim_space() { fn test_trim_space() {