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

regex: fix regex.split() (fix #16876) #21953

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 12 additions & 10 deletions vlib/regex/regex_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -431,29 +431,31 @@ const (
split_test_suite = [
Test_split{'abcd 1234 efgh 1234 ghkl1234 ab34546df', r'\d+', ['abcd ', ' efgh ', ' ghkl',
' ab', 'df']},
Test_split{'abcd 1234 efgh 1234 ghkl1234 ab34546df', r'\a+', [' 1234 ', ' 1234 ', '1234 ',
'34546']},
Test_split{'abcd 1234 efgh 1234 ghkl1234 ab34546df', r'\a+', ['', ' 1234 ', ' 1234 ', '1234 ',
'34546', '']},
Test_split{'oggi pippo è andato a casa di pluto ed ha trovato pippo', r'p[iplut]+o', [
'oggi ', ' è andato a casa di ', ' ed ha trovato ']},
'oggi ', ' è andato a casa di ', ' ed ha trovato ', '']},
Test_split{'oggi pibao è andato a casa di pbababao ed ha trovato pibabababao', r'(pi?(ba)+o)', [
'oggi ', ' è andato a casa di ', ' ed ha trovato ']},
'oggi ', ' è andato a casa di ', ' ed ha trovato ', '']},
Test_split{'Today is a good day and tomorrow will be for sure.', r'[Tt]o\w+', [
' is a good day and ', ' will be for sure.']},
'', ' is a good day and ', ' will be for sure.']},
Test_split{'pera\nurl = https://github.com/dario/pig.html\npippo', r'url *= *https?://[\w./]+', [
'pera\n', '\npippo']},
Test_split{'pera\nurl = https://github.com/dario/pig.html\npippo', r'url *= *https?://.*' +
'\n', ['pera\n', 'pippo']},
Test_split{'#.#......##.#..#..##........##....###...##...######.......#.....#..#......#...#........###.#..#.', r'#[.#]{4}##[.#]{4}##[.#]{4}###', [
'#.#......##.#..#..##........#', '##.......#.....#..#......#...#........###.#..#.']},
Test_split{'#.#......##.#..#..##........##....###...##...######.......#.....#..#......#...#........###.#..#.', r'.*#[.#]{4}##[.#]{4}##[.#]{4}###', [
'##.......#.....#..#......#...#........###.#..#.']},
Test_split{'1234 Aa dddd Aaf 12334 Aa opopo Aaf', r'Aa.+Aaf', ['1234 ', ' 12334 ']},
'', '##.......#.....#..#......#...#........###.#..#.']},
Test_split{'1234 Aa dddd Aaf 12334 Aa opopo Aaf', r'Aa.+Aaf', ['1234 ', ' 12334 ', '']},
Test_split{'@for something @endfor @for something else @endfor altro testo @for body @endfor uno due @for senza dire più @endfor pippo', r'@for.+@endfor', [
' ', ' altro testo ', ' uno due ', ' pippo']},
'', ' ', ' altro testo ', ' uno due ', ' pippo']},
Test_split{'+++pippo+++\n elvo +++ pippo2 +++ +++ oggi+++', r'\+{3}.*\+{3}', [
'\n elvo ', ' ']},
'', '\n elvo ', ' ', '']},
Test_split{'foobar', r'\d', ['foobar']},
Test_split{'1234', r'\d+', []},
Test_split{'1234', r'\d+', ['', '']},
Test_split{'a-', r'-', ['a', '']},
Test_split{'-a', r'-', ['', 'a']},
]
)
// vfmt on
Expand Down
7 changes: 1 addition & 6 deletions vlib/regex/regex_util.v
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,13 @@ pub fn (mut re RE) split(in_txt string) []string {
return [in_txt]
}
for i := 0; i < pos.len; i += 2 {
if pos[i] == 0 {
continue
}
if i == 0 {
sections << in_txt[..pos[i]]
} else {
sections << in_txt[pos[i - 1]..pos[i]]
}
}
if pos[pos.len - 1] != in_txt.len {
sections << in_txt[pos[pos.len - 1]..]
}
sections << in_txt[pos[pos.len - 1]..]
return sections
}

Expand Down
Loading