Skip to content

Commit 1214290

Browse files
authored
fix pcre_study checking with possible zero-length matching (#16)
* fix * add test
1 parent c332978 commit 1214290

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

regex.v

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ pub fn new_regex(source string, options int) !Regex {
5959
}
6060
extra := C.pcre_study(re, 0, voidptr(&pstudyerr))
6161
if extra == 0 {
62-
if pstudyerr == 0 {
63-
return error('no additional information')
62+
if pstudyerr != 0 {
63+
err := unsafe { cstring_to_vstring(pstudyerr) }
64+
return error('Failed to study regex: ${err}')
6465
}
65-
err := unsafe { cstring_to_vstring(pstudyerr) }
66-
return error('Failed to study regex: ${err}')
6766
}
6867
C.pcre_fullinfo(re, 0, C.PCRE_INFO_CAPTURECOUNT, &captures)
6968
return Regex{re, extra, captures, options}

regex_test.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ fn test_match_str_iterator() {
2828
}
2929
assert out == ['a', 'b', 'c', 'd', 'e', 'f']
3030
}
31+
32+
fn test_match_possible_zero_length() {
33+
mut re := pcre.new_regex(r'(.)*', 0) or { panic(err) }
34+
m := re.match_str('Vlang', 0, 0) or { panic(err) }
35+
assert m.get_all() == ['g']
36+
}

0 commit comments

Comments
 (0)