@@ -42,12 +42,6 @@ pcre_extra * extra;
42
42
re->m_extra = extra;
43
43
re->m_iExecutionError = 0 ; // no error now
44
44
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
51
45
52
46
return re;
53
47
}
@@ -59,20 +53,27 @@ int regexec(register t_regexp *prog,
59
53
int options = App.m_bRegexpMatchEmpty ? 0 : PCRE_NOTEMPTY; // don't match on an empty string
60
54
int count;
61
55
62
-
63
- LARGE_INTEGER start,
64
- finish;
65
-
66
56
// exit if no regexp program to process (possibly because of previous error)
67
57
if (prog->m_program == NULL )
68
58
return false ;
69
59
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
+
70
71
if (App.m_iCounterFrequency )
71
72
QueryPerformanceCounter (&start);
72
73
73
74
pcre_callout = NULL ;
74
75
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 ());
76
77
77
78
if (App.m_iCounterFrequency )
78
79
{
@@ -81,32 +82,28 @@ LARGE_INTEGER start,
81
82
}
82
83
83
84
if (count == PCRE_ERROR_NOMATCH)
84
- return false ; // no match
85
+ return false ; // no match - don't save matching string etc.
85
86
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
87
88
if (count <= 0 )
88
89
{
89
90
pcre_free (prog->m_program );
90
91
prog->m_program = NULL ;
91
92
pcre_free (prog->m_extra );
92
93
prog->m_extra = NULL ;
93
94
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 )
100
95
ThrowErrorException (TFormat (" Error executing regular expression: %s" ,
101
- Convert_PCRE_Runtime_Error (count)));
96
+ Convert_PCRE_Runtime_Error (count)));
97
+ }
102
98
103
99
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
105
101
// and offsets, so we can extract the wildcards later on
106
102
107
103
prog->m_sTarget = string; // for extracting wildcards
108
104
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 ));
110
107
return true ; // match
111
108
}
112
109
0 commit comments