@@ -73,12 +73,6 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
73
73
PCRE2_SIZE erroroffset = 0 ;
74
74
m_pc = pcre2_compile (pcre2_pattern, PCRE2_ZERO_TERMINATED,
75
75
pcre2_options, &errornumber, &erroroffset, NULL );
76
- if (m_pc != NULL ) {
77
- m_match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
78
- if (m_match_data == NULL ) {
79
- m_pc = NULL ;
80
- }
81
- }
82
76
#else
83
77
const char *errptr = NULL ;
84
78
int erroffset;
@@ -97,7 +91,6 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
97
91
98
92
Regex::~Regex () {
99
93
#if WITH_PCRE2
100
- pcre2_match_data_free (m_match_data);
101
94
pcre2_code_free (m_pc);
102
95
#else
103
96
if (m_pc != NULL ) {
@@ -123,10 +116,11 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
123
116
PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
124
117
PCRE2_SIZE offset = 0 ;
125
118
119
+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
126
120
do {
127
121
rc = pcre2_match (m_pc, pcre2_s, s.length (),
128
- offset, 0 , m_match_data , NULL );
129
- PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (m_match_data );
122
+ offset, 0 , match_data , NULL );
123
+ PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (match_data );
130
124
#else
131
125
const char *subject = s.c_str ();
132
126
int ovector[OVECCOUNT];
@@ -155,14 +149,18 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
155
149
}
156
150
} while (rc > 0 );
157
151
152
+ #ifdef WITH_PCRE2
153
+ pcre2_match_data_free (match_data);
154
+ #endif
158
155
return retList;
159
156
}
160
157
161
158
bool Regex::searchOneMatch (const std::string& s, std::vector<SMatchCapture>& captures) const {
162
159
#ifdef WITH_PCRE2
163
160
PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
164
- int rc = pcre2_match (m_pc, pcre2_s, s.length (), 0 , 0 , m_match_data, NULL );
165
- PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (m_match_data);
161
+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
162
+ int rc = pcre2_match (m_pc, pcre2_s, s.length (), 0 , 0 , match_data, NULL );
163
+ PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (match_data);
166
164
#else
167
165
const char *subject = s.c_str ();
168
166
int ovector[OVECCOUNT];
@@ -181,6 +179,9 @@ bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& cap
181
179
captures.push_back (capture);
182
180
}
183
181
182
+ #ifdef WITH_PCRE2
183
+ pcre2_match_data_free (match_data);
184
+ #endif
184
185
return (rc > 0 );
185
186
}
186
187
@@ -190,14 +191,15 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
190
191
PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
191
192
PCRE2_SIZE startOffset = 0 ;
192
193
194
+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
193
195
while (startOffset <= s.length ()) {
194
196
uint32_t pcre2_options = 0 ;
195
197
if (prev_match_zero_length) {
196
198
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
197
199
}
198
200
int rc = pcre2_match (m_pc, pcre2_s, s.length (),
199
- startOffset, pcre2_options, m_match_data , NULL );
200
- PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (m_match_data );
201
+ startOffset, pcre2_options, match_data , NULL );
202
+ PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (match_data );
201
203
202
204
#else
203
205
const char *subject = s.c_str ();
@@ -258,17 +260,21 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
258
260
}
259
261
}
260
262
263
+ #ifdef WITH_PCRE2
264
+ pcre2_match_data_free (match_data);
265
+ #endif
261
266
return (captures.size () > 0 );
262
267
}
263
268
264
269
int Regex::search (const std::string& s, SMatch *match) const {
265
270
#ifdef WITH_PCRE2
266
271
PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
272
+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
267
273
int ret = pcre2_match (m_pc, pcre2_s, s.length (),
268
- 0 , 0 , m_match_data , NULL ) > 0 ;
274
+ 0 , 0 , match_data , NULL ) > 0 ;
269
275
270
276
if (ret > 0 ) { // match
271
- PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (m_match_data );
277
+ PCRE2_SIZE *ovector = pcre2_get_ovector_pointer (match_data );
272
278
#else
273
279
int ovector[OVECCOUNT];
274
280
int ret = pcre_exec (m_pc, m_pce, s.c_str (),
@@ -281,13 +287,18 @@ int Regex::search(const std::string& s, SMatch *match) const {
281
287
0 );
282
288
}
283
289
290
+ #ifdef WITH_PCRE2
291
+ pcre2_match_data_free (match_data);
292
+ #endif
284
293
return ret;
285
294
}
286
295
287
296
int Regex::search (const std::string& s) const {
288
297
#ifdef WITH_PCRE2
289
298
PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
290
- int rc = pcre2_match (m_pc, pcre2_s, s.length (), 0 , 0 , m_match_data, NULL );
299
+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
300
+ int rc = pcre2_match (m_pc, pcre2_s, s.length (), 0 , 0 , match_data, NULL );
301
+ pcre2_match_data_free (match_data);
291
302
if (rc > 0 ) {
292
303
return 1 ; // match
293
304
} else {
0 commit comments