Skip to content

Commit

Permalink
fix pcre_study checking with possible zero-length matching (#16)
Browse files Browse the repository at this point in the history
* fix

* add test
  • Loading branch information
felipensp authored Jan 31, 2025
1 parent c332978 commit 1214290
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 3 additions & 4 deletions regex.v
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ pub fn new_regex(source string, options int) !Regex {
}
extra := C.pcre_study(re, 0, voidptr(&pstudyerr))
if extra == 0 {
if pstudyerr == 0 {
return error('no additional information')
if pstudyerr != 0 {
err := unsafe { cstring_to_vstring(pstudyerr) }
return error('Failed to study regex: ${err}')
}
err := unsafe { cstring_to_vstring(pstudyerr) }
return error('Failed to study regex: ${err}')
}
C.pcre_fullinfo(re, 0, C.PCRE_INFO_CAPTURECOUNT, &captures)
return Regex{re, extra, captures, options}
Expand Down
6 changes: 6 additions & 0 deletions regex_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ fn test_match_str_iterator() {
}
assert out == ['a', 'b', 'c', 'd', 'e', 'f']
}

fn test_match_possible_zero_length() {
mut re := pcre.new_regex(r'(.)*', 0) or { panic(err) }
m := re.match_str('Vlang', 0, 0) or { panic(err) }
assert m.get_all() == ['g']
}

0 comments on commit 1214290

Please sign in to comment.