@@ -73,12 +73,6 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
7373 PCRE2_SIZE erroroffset = 0 ;
7474 m_pc = pcre2_compile (pcre2_pattern, PCRE2_ZERO_TERMINATED,
7575 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- }
8276#else
8377 const char *errptr = NULL ;
8478 int erroffset;
@@ -97,7 +91,6 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
9791
9892Regex::~Regex () {
9993#if WITH_PCRE2
100- pcre2_match_data_free (m_match_data);
10194 pcre2_code_free (m_pc);
10295#else
10396 if (m_pc != NULL ) {
@@ -123,10 +116,11 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
123116 PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
124117 PCRE2_SIZE offset = 0 ;
125118
119+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
126120 do {
127121 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 );
130124#else
131125 const char *subject = s.c_str ();
132126 int ovector[OVECCOUNT];
@@ -155,14 +149,18 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
155149 }
156150 } while (rc > 0 );
157151
152+ #ifdef WITH_PCRE2
153+ pcre2_match_data_free (match_data);
154+ #endif
158155 return retList;
159156}
160157
161158bool Regex::searchOneMatch (const std::string& s, std::vector<SMatchCapture>& captures) const {
162159#ifdef WITH_PCRE2
163160 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);
166164#else
167165 const char *subject = s.c_str ();
168166 int ovector[OVECCOUNT];
@@ -181,6 +179,9 @@ bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& cap
181179 captures.push_back (capture);
182180 }
183181
182+ #ifdef WITH_PCRE2
183+ pcre2_match_data_free (match_data);
184+ #endif
184185 return (rc > 0 );
185186}
186187
@@ -190,14 +191,15 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
190191 PCRE2_SPTR pcre2_s = reinterpret_cast <PCRE2_SPTR>(s.c_str ());
191192 PCRE2_SIZE startOffset = 0 ;
192193
194+ pcre2_match_data *match_data = pcre2_match_data_create_from_pattern (m_pc, NULL );
193195 while (startOffset <= s.length ()) {
194196 uint32_t pcre2_options = 0 ;
195197 if (prev_match_zero_length) {
196198 pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
197199 }
198200 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 );
201203
202204#else
203205 const char *subject = s.c_str ();
@@ -258,17 +260,21 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
258260 }
259261 }
260262
263+ #ifdef WITH_PCRE2
264+ pcre2_match_data_free (match_data);
265+ #endif
261266 return (captures.size () > 0 );
262267}
263268
264269int Regex::search (const std::string& s, SMatch *match) const {
265270#ifdef WITH_PCRE2
266271 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 );
267273 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 ;
269275
270276 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 );
272278#else
273279 int ovector[OVECCOUNT];
274280 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 {
281287 0 );
282288 }
283289
290+ #ifdef WITH_PCRE2
291+ pcre2_match_data_free (match_data);
292+ #endif
284293 return ret;
285294}
286295
287296int Regex::search (const std::string& s) const {
288297#ifdef WITH_PCRE2
289298 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);
291302 if (rc > 0 ) {
292303 return 1 ; // match
293304 } else {
0 commit comments