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

builtin: rewrite string.split_nth and fix some bugs (#7189)

This commit is contained in:
Andréas Livet 2020-12-08 09:51:47 +01:00 committed by GitHub
parent 8931d3d39c
commit a2ec52b8c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 25 deletions

View File

@ -301,7 +301,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
} }
fn print_output(s os.Result) { fn print_output(s os.Result) {
lines := s.output.trim_right('\n\r').split('\n') lines := s.output.trim_right('\n\r').split_into_lines()
for line in lines { for line in lines {
if line.contains('.vrepl_temp.v:') { if line.contains('.vrepl_temp.v:') {
// Hide the temporary file name // Hide the temporary file name

View File

@ -484,7 +484,7 @@ pub fn (s string) split_nth(delim string, nth int) []string {
i = 1 i = 1
for ch in s { for ch in s {
if nth > 0 && i >= nth { if nth > 0 && i >= nth {
res << s.substr(i, s.len) res << s.right(i)
break break
} }
res << ch.str() res << ch.str()
@ -493,39 +493,30 @@ pub fn (s string) split_nth(delim string, nth int) []string {
return res return res
} }
mut start := 0 mut start := 0
nth_1 := nth - 1 // Take the left part for each delimiter occurence
for i <= s.len { for i <= s.len {
mut is_delim := unsafe {s.str[i] == delim.str[0]} is_delim := i + delim.len <= s.len && s.substr(i, i + delim.len) == delim
mut j := 0 if is_delim {
for is_delim && j < delim.len { val := s.substr(start, i)
is_delim = is_delim && unsafe {s.str[i + j] == delim.str[j]} was_last := nth > 0 && res.len == nth - 1
j++
}
last := i == s.len - 1
if is_delim || last {
if !is_delim && last {
i++
}
mut val := s.substr(start, i)
if val.starts_with(delim) {
val = val.right(delim.len)
}
was_last := nth > 0 && res.len == nth_1
if was_last { if was_last {
res << s.right(start)
break break
} }
res << val res << val
start = i + delim.len start = i + delim.len
} i = start
} else {
i++ i++
} }
if s.ends_with(delim) && (nth < 1 || res.len < nth) { }
res << '' // Then the remaining right part of the string
if nth < 1 || res.len < nth {
res << s.right(start)
} }
return res return res
} }
pub fn (s string) split_into_lines() []string { pub fn (s string) split_into_lines() []string {
mut res := []string{} mut res := []string{}
if s.len == 0 { if s.len == 0 {

View File

@ -138,8 +138,8 @@ fn test_split_nth() {
assert (d.split_nth(',', -1).len == 2) assert (d.split_nth(',', -1).len == 2)
assert (d.split_nth(',', 3).len == 2) assert (d.split_nth(',', 3).len == 2)
e := ",,,0,,,,,a,,b," e := ",,,0,,,,,a,,b,"
// assert (e.split(',,').len == 5) assert (e.split(',,').len == 5)
// assert (e.split_nth(',,', 3).len == 2) assert (e.split_nth(',,', 3).len == 3)
assert (e.split_nth(',', -1).len == 12) assert (e.split_nth(',', -1).len == 12)
assert (e.split_nth(',', 3).len == 3) assert (e.split_nth(',', 3).len == 3)
} }
@ -213,6 +213,12 @@ fn test_split() {
assert a[4] == 'o' assert a[4] == 'o'
assert a[5] == 'm' assert a[5] == 'm'
assert a[6] == 'e' assert a[6] == 'e'
// /////////
s = 'wavy turquoise bags'
vals = s.split(' bags')
assert vals.len == 2
assert vals[0] == 'wavy turquoise'
assert vals[1] == ''
} }
fn test_trim_space() { fn test_trim_space() {