Fix bug in applying regex replacement to non-matching strings.

We didn't handle pcre_exec() return code properly and so the replacement could
be still done even if there was no match if the replacement part contained
anything else than back-references (in this, the only tested so far, case the
replacement was still done but the result turned out to be empty and the
calling code assumed the regex didn't match).

Do check for PCRE_ERROR_NOMATCH now and also give an error message if another
error unexpectedly occurred.

Add a test case for the bug that was fixed.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12187 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Vadim Zeitlin 2010-08-14 14:12:23 +00:00
commit 024ed6ce2a
2 changed files with 17 additions and 3 deletions

View file

@ -3,9 +3,14 @@
// strip the wx prefix from all identifiers except those starting with wxEVT
%rename("%(regex:/wx(?!EVT)(.*)/\\1/)s") "";
// Replace "Set" prefix with "put" in all functions
%rename("%(regex:/^Set(.*)/put\\1/)s", %$isfunction) "";
%inline %{
class wxSomeWidget {
struct wxSomeWidget {
void SetBorderWidth(int);
void SetSize(int, int);
};
struct wxAnotherWidget {

View file

@ -1188,6 +1188,8 @@ String *Swig_string_regex(String *s) {
int captures[30];
if (split_regex_pattern_subst(s, &pattern, &subst, &input)) {
int rc;
compiled_pat = pcre_compile(
Char(pattern), pcre_options, &pcre_error, &pcre_errorpos, NULL);
if (!compiled_pat) {
@ -1195,8 +1197,15 @@ String *Swig_string_regex(String *s) {
pcre_error, Char(pattern), pcre_errorpos);
exit(1);
}
pcre_exec(compiled_pat, NULL, input, strlen(input), 0, 0, captures, 30);
res = replace_captures(input, subst, captures);
rc = pcre_exec(compiled_pat, NULL, input, strlen(input), 0, 0, captures, 30);
if (rc >= 0) {
res = replace_captures(input, subst, captures);
}
else if (rc != PCRE_ERROR_NOMATCH) {
Swig_error("SWIG", Getline(s), "PCRE execution failed: error %d while matching \"%s\" in \"%s\".\n",
rc, Char(pattern), input);
exit(1);
}
}
DohDelete(pattern);