builtin.string: optimize string.split_nth() for len == 1 delim (#9325)

pull/9333/head
shadowninja55 2021-03-16 14:30:27 -04:00 committed by GitHub
parent 39a9beb9e0
commit d92f9e77b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 29 deletions

View File

@ -530,40 +530,70 @@ pub fn (s string) split(delim string) []string {
pub fn (s string) split_nth(delim string, nth int) []string {
mut res := []string{}
mut i := 0
if delim.len == 0 {
i = 1
for ch in s {
if nth > 0 && i >= nth {
res << s[i..]
break
match delim.len {
0 {
i = 1
for ch in s {
if nth > 0 && i >= nth {
res << s[i..]
break
}
res << ch.ascii_str()
i++
}
res << ch.ascii_str()
i++
return res
}
return res
}
mut start := 0
// Take the left part for each delimiter occurence
for i <= s.len {
is_delim := i + delim.len <= s.len && s.substr(i, i + delim.len) == delim
if is_delim {
val := s.substr(start, i)
was_last := nth > 0 && res.len == nth - 1
if was_last {
break
1 {
mut start := 0
delim_byte := delim[0]
for i < s.len {
if s[i] == delim_byte {
was_last := nth > 0 && res.len == nth - 1
if was_last {
break
}
val := s.substr(start, i)
res << val
start = i + delim.len
i = start
} else {
i++
}
}
res << val
start = i + delim.len
i = start
} else {
i++
// Then the remaining right part of the string
if nth < 1 || res.len < nth {
res << s[start..]
}
return res
}
else {
mut start := 0
// Take the left part for each delimiter occurence
for i <= s.len {
is_delim := i + delim.len <= s.len && s.substr(i, i + delim.len) == delim
if is_delim {
was_last := nth > 0 && res.len == nth - 1
if was_last {
break
}
val := s.substr(start, i)
res << val
start = i + delim.len
i = start
} else {
i++
}
}
// Then the remaining right part of the string
if nth < 1 || res.len < nth {
res << s[start..]
}
return res
}
}
// Then the remaining right part of the string
if nth < 1 || res.len < nth {
res << s[start..]
}
return res
}
// split_into_lines splits the string by newline characters.