Skip to content

Commit f7ab822

Browse files
committed
Fixes to changes to regular expression handling
1 parent b73a97e commit f7ab822

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

regexp.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ pcre_extra * extra;
4242
re->m_extra = extra;
4343
re->m_iExecutionError = 0; // no error now
4444

45-
// inspired by a suggestion by Twisol (to remove a hard-coded limit on the number of wildcards)
46-
int capturecount = 0;
47-
// how many captures did we get?
48-
pcre_fullinfo(program, NULL, PCRE_INFO_CAPTURECOUNT, &capturecount);
49-
// allocate memory for them
50-
re->m_vOffsets.resize ((capturecount + 1) * 3); // add 1 for the whole expression
5145

5246
return re;
5347
}
@@ -59,20 +53,27 @@ int regexec(register t_regexp *prog,
5953
int options = App.m_bRegexpMatchEmpty ? 0 : PCRE_NOTEMPTY; // don't match on an empty string
6054
int count;
6155

62-
63-
LARGE_INTEGER start,
64-
finish;
65-
6656
// exit if no regexp program to process (possibly because of previous error)
6757
if (prog->m_program == NULL)
6858
return false;
6959

60+
// inspired by a suggestion by Twisol (to remove a hard-coded limit on the number of wildcards)
61+
int capturecount = 0;
62+
// how many captures did we get?
63+
pcre_fullinfo(prog->m_program, NULL, PCRE_INFO_CAPTURECOUNT, &capturecount);
64+
// allocate enough memory
65+
vector<int> offsets ((capturecount + 1) * 3); // we always get offset 0 - the whole match
66+
67+
LARGE_INTEGER start,
68+
finish;
69+
70+
7071
if (App.m_iCounterFrequency)
7172
QueryPerformanceCounter (&start);
7273

7374
pcre_callout = NULL;
7475
count = pcre_exec(prog->m_program, prog->m_extra, string, strlen (string),
75-
start_offset, options, &prog->m_vOffsets [0], prog->m_vOffsets.size ());
76+
start_offset, options, &offsets [0], offsets.size ());
7677

7778
if (App.m_iCounterFrequency)
7879
{
@@ -81,32 +82,28 @@ LARGE_INTEGER start,
8182
}
8283

8384
if (count == PCRE_ERROR_NOMATCH)
84-
return false; // no match
85+
return false; // no match - don't save matching string etc.
8586

86-
// free program as an indicator that we can't keep trying to do this one
87+
// otherwise free program as an indicator that we can't keep trying to do this one
8788
if (count <= 0)
8889
{
8990
pcre_free (prog->m_program);
9091
prog->m_program = NULL;
9192
pcre_free (prog->m_extra);
9293
prog->m_extra = NULL;
9394
prog->m_iExecutionError = count; // remember reason
94-
}
95-
96-
if (count == 0)
97-
ThrowErrorException (Translate ("Too many substrings in regular expression"));
98-
99-
if (count < 0)
10095
ThrowErrorException (TFormat ("Error executing regular expression: %s",
101-
Convert_PCRE_Runtime_Error (count)));
96+
Convert_PCRE_Runtime_Error (count)));
97+
}
10298

10399

104-
// if, and only if, we match we will save the matching string, the count
100+
// if, and only if, we match, we will save the matching string, the count
105101
// and offsets, so we can extract the wildcards later on
106102

107103
prog->m_sTarget = string; // for extracting wildcards
108104
prog->m_iCount = count; // ditto
109-
105+
prog->m_vOffsets.resize (0); // clear for copy, but leave allocated memory
106+
copy (offsets.begin (), offsets.end (), back_inserter (prog->m_vOffsets));
110107
return true; // match
111108
}
112109

scripting/lua_scripting.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,9 @@ bool CScriptEngine::ExecuteLua (DISPID & dispid, // dispatch ID, will be set to
470470

471471
for (i = 0; i <= ncapt; i++)
472472
{
473-
int j = i * 2;
474-
lua_pushlstring(L, regexp->m_sTarget.c_str () + regexp->m_vOffsets[j], regexp->m_vOffsets[j + 1] - regexp->m_vOffsets[j]);
473+
lua_pushstring(L, regexp->GetWildcard (i).c_str ());
475474
lua_rawseti (L, -2, i);
476475
}
477-
// now add item 0 - the whole matching line
478-
// lua_pushstring (trigger_item->wildcards [0].c_str ())
479-
// lua_rawseti(L, -2, 0);
480476

481477
/* now do named subpatterns */
482478
pcre_fullinfo(regexp->m_program, regexp->m_extra, PCRE_INFO_NAMECOUNT, &namecount);
@@ -512,7 +508,7 @@ bool CScriptEngine::ExecuteLua (DISPID & dispid, // dispatch ID, will be set to
512508

513509
lua_pushstring (L, (LPCTSTR) name);
514510
if (n >= 0 && n <= ncapt)
515-
lua_pushlstring(L, regexp->m_sTarget.c_str () + regexp->m_vOffsets[j], regexp->m_vOffsets[j + 1] - regexp->m_vOffsets[j]);
511+
lua_pushstring(L, regexp->GetWildcard (n).c_str ());
516512
else
517513
lua_pushnil (L); /* n out of range */
518514
lua_settable (L, -3);

0 commit comments

Comments
 (0)