Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin: simplify splint_nth methods #21563

Merged
merged 4 commits into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 26 additions & 55 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -922,71 +922,54 @@ pub fn (s string) rsplit_once(delim string) ?(string, string) {
@[direct_array_access]
pub fn (s string) split_nth(delim string, nth int) []string {
mut res := []string{}
mut i := 0

match delim.len {
0 {
i = 1
for ch in s {
if nth > 0 && i >= nth {
res << s[i - 1..]
for i, ch in s {
if nth > 0 && res.len == nth - 1 {
res << s[i..]
break
}
res << ch.ascii_str()
i++
}
return res
}
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 {
mut start := 0
for i, ch in s {
if ch == delim_byte {
if nth > 0 && res.len == nth - 1 {
break
}
val := s.substr(start, i)
res << val
start = i + delim.len
i = start
} else {
i++
res << s.substr(start, i)
start = i + 1
}
}

// 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 occurrence
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 {
// Add up to `nth` segments left of every occurrence of the delimiter.
for i := 0; i + delim.len <= s.len; i++ {
if unsafe { s.substr_unsafe(i, i + delim.len) } == delim {
if nth > 0 && res.len == nth - 1 {
break
}
val := s.substr(start, i)
res << val
start = i + delim.len
i = start
} else {
i++
res << s.substr(start, i)
i += delim.len
start = i
}
}
// Then the remaining right part of the string
// Then add the remaining part of the string as the last segment.
if nth < 1 || res.len < nth {
res << s[start..]
}
return res
}
}

return res
}

// rsplit_nth splits the string based on the passed `delim` substring in revese order.
Expand All @@ -996,65 +979,53 @@ pub fn (s string) split_nth(delim string, nth int) []string {
@[direct_array_access]
pub fn (s string) rsplit_nth(delim string, nth int) []string {
mut res := []string{}
mut i := s.len - 1

match delim.len {
0 {
for i >= 0 {
for i := s.len - 1; i >= 0; i-- {
if nth > 0 && res.len == nth - 1 {
res << s[..i + 1]
break
}
res << s[i].ascii_str()
i--
}
return res
}
1 {
mut rbound := s.len
delim_byte := delim[0]

for i >= 0 {
mut rbound := s.len
for i := s.len - 1; i >= 0; i-- {
if s[i] == delim_byte {
if nth > 0 && res.len == nth - 1 {
break
}
res << s[i + 1..rbound]
rbound = i
i--
} else {
i--
}
}

if nth < 1 || res.len < nth {
res << s[..rbound]
}
return res
}
else {
mut rbound := s.len

for i >= 0 {
for i := s.len - 1; i >= 0; i-- {
is_delim := i - delim.len >= 0 && s[i - delim.len..i] == delim
if is_delim {
if nth > 0 && res.len == nth - 1 {
break
}
res << s[i..rbound]
rbound = i - delim.len
i -= delim.len
} else {
i--
rbound = i
}
}

if nth < 1 || res.len < nth {
res << s[..rbound]
}
return res
}
}

return res
}

// split_into_lines splits the string by newline characters.
Expand Down
Loading