]>
Commit | Line | Data |
---|---|---|
e7208277 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/regex.cpp | |
3 | // Purpose: regular expression matching | |
9d55bfef | 4 | // Author: Karsten Ballueder and Vadim Zeitlin |
e7208277 VZ |
5 | // Modified by: |
6 | // Created: 13.07.01 | |
7 | // RCS-ID: $Id$ | |
9d55bfef | 8 | // Copyright: (c) 2000 Karsten Ballueder <ballueder@gmx.net> |
e7208277 | 9 | // 2001 Vadim Zeitlin <vadim@wxwindows.org> |
65571936 | 10 | // Licence: wxWindows licence |
e7208277 VZ |
11 | /////////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
e7208277 VZ |
21 | // For compilers that support precompilation, includes "wx.h". |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
28 | #if wxUSE_REGEX | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/object.h" | |
32 | #include "wx/string.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/intl.h" | |
0bf751e7 | 35 | #include "wx/crt.h" |
e7208277 VZ |
36 | #endif //WX_PRECOMP |
37 | ||
04fdd873 DS |
38 | // FreeBSD, Watcom and DMars require this, CW doesn't have nor need it. |
39 | // Others also don't seem to need it. If you have an error related to | |
40 | // (not) including <sys/types.h> please report details to | |
41 | // wx-dev@lists.wxwindows.org | |
4ded51f2 | 42 | #if defined(__UNIX__) || defined(__WATCOMC__) || defined(__DIGITALMARS__) |
04fdd873 | 43 | # include <sys/types.h> |
0f2ecc91 VZ |
44 | #endif |
45 | ||
e7208277 | 46 | #include <regex.h> |
e7208277 VZ |
47 | #include "wx/regex.h" |
48 | ||
7553e636 MW |
49 | // WXREGEX_USING_BUILTIN defined when using the built-in regex lib |
50 | // WXREGEX_USING_RE_SEARCH defined when using re_search in the GNU regex lib | |
51 | // WXREGEX_IF_NEED_LEN() wrap the len parameter only used with the built-in | |
52 | // or GNU regex | |
53 | // WXREGEX_CONVERT_TO_MB defined when the regex lib is using chars and | |
54 | // wxChar is wide, so conversion must be done | |
55 | // WXREGEX_CHAR(x) Convert wxChar to wxRegChar | |
ab0f0edd | 56 | // |
c9eee7f0 MW |
57 | #ifdef __REG_NOFRONT |
58 | # define WXREGEX_USING_BUILTIN | |
ab0f0edd | 59 | # define WXREGEX_IF_NEED_LEN(x) ,x |
52de37c7 | 60 | # if wxUSE_UNICODE |
bcbb0248 | 61 | # define WXREGEX_CHAR(x) (x).wc_str() |
52de37c7 | 62 | # else |
bcbb0248 | 63 | # define WXREGEX_CHAR(x) (x).mb_str() |
52de37c7 | 64 | # endif |
c9eee7f0 | 65 | #else |
ab0f0edd MW |
66 | # ifdef HAVE_RE_SEARCH |
67 | # define WXREGEX_IF_NEED_LEN(x) ,x | |
7553e636 | 68 | # define WXREGEX_USING_RE_SEARCH |
2cdf2445 | 69 | # else |
ab0f0edd MW |
70 | # define WXREGEX_IF_NEED_LEN(x) |
71 | # endif | |
c9eee7f0 MW |
72 | # if wxUSE_UNICODE |
73 | # define WXREGEX_CONVERT_TO_MB | |
74 | # endif | |
bcbb0248 | 75 | # define WXREGEX_CHAR(x) (x).mb_str() |
7553e636 MW |
76 | # define wx_regfree regfree |
77 | # define wx_regerror regerror | |
f302b6a4 MW |
78 | #endif |
79 | ||
e7208277 VZ |
80 | // ---------------------------------------------------------------------------- |
81 | // private classes | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
7553e636 | 84 | #ifndef WXREGEX_USING_RE_SEARCH |
ab0f0edd MW |
85 | |
86 | // the array of offsets for the matches, the usual POSIX regmatch_t array. | |
87 | class wxRegExMatches | |
88 | { | |
89 | public: | |
90 | typedef regmatch_t *match_type; | |
91 | ||
2cdf2445 | 92 | wxRegExMatches(size_t n) { m_matches = new regmatch_t[n]; } |
ab0f0edd MW |
93 | ~wxRegExMatches() { delete [] m_matches; } |
94 | ||
2cdf2445 VZ |
95 | // we just use casts here because the fields of regmatch_t struct may be 64 |
96 | // bit but we're limited to size_t in our public API and are not going to | |
97 | // change it because operating on strings longer than 4GB using it is | |
9630cac5 | 98 | // absolutely impractical anyhow |
2cdf2445 VZ |
99 | size_t Start(size_t n) const |
100 | { | |
2cdf2445 VZ |
101 | return wx_truncate_cast(size_t, m_matches[n].rm_so); |
102 | } | |
103 | ||
104 | size_t End(size_t n) const | |
105 | { | |
2cdf2445 VZ |
106 | return wx_truncate_cast(size_t, m_matches[n].rm_eo); |
107 | } | |
ab0f0edd MW |
108 | |
109 | regmatch_t *get() const { return m_matches; } | |
110 | ||
111 | private: | |
112 | regmatch_t *m_matches; | |
113 | }; | |
114 | ||
7553e636 | 115 | #else // WXREGEX_USING_RE_SEARCH |
ab0f0edd MW |
116 | |
117 | // the array of offsets for the matches, the struct used by the GNU lib | |
118 | class wxRegExMatches | |
119 | { | |
120 | public: | |
121 | typedef re_registers *match_type; | |
122 | ||
123 | wxRegExMatches(size_t n) | |
124 | { | |
125 | m_matches.num_regs = n; | |
126 | m_matches.start = new regoff_t[n]; | |
127 | m_matches.end = new regoff_t[n]; | |
128 | } | |
129 | ||
130 | ~wxRegExMatches() | |
131 | { | |
132 | delete [] m_matches.start; | |
133 | delete [] m_matches.end; | |
134 | } | |
135 | ||
136 | size_t Start(size_t n) const { return m_matches.start[n]; } | |
137 | size_t End(size_t n) const { return m_matches.end[n]; } | |
138 | ||
139 | re_registers *get() { return &m_matches; } | |
140 | ||
141 | private: | |
142 | re_registers m_matches; | |
143 | }; | |
144 | ||
7553e636 | 145 | #endif // WXREGEX_USING_RE_SEARCH |
ab0f0edd | 146 | |
f302b6a4 | 147 | // the character type used by the regular expression engine |
db9db9cd | 148 | #ifndef WXREGEX_CONVERT_TO_MB |
f302b6a4 MW |
149 | typedef wxChar wxRegChar; |
150 | #else | |
151 | typedef char wxRegChar; | |
152 | #endif | |
153 | ||
e7208277 VZ |
154 | // the real implementation of wxRegEx |
155 | class wxRegExImpl | |
156 | { | |
157 | public: | |
158 | // ctor and dtor | |
159 | wxRegExImpl(); | |
160 | ~wxRegExImpl(); | |
161 | ||
701a0b47 | 162 | // return true if Compile() had been called successfully |
e7208277 VZ |
163 | bool IsValid() const { return m_isCompiled; } |
164 | ||
165 | // RE operations | |
765624f7 | 166 | bool Compile(const wxString& expr, int flags = 0); |
c9eee7f0 | 167 | bool Matches(const wxRegChar *str, int flags |
ab0f0edd | 168 | WXREGEX_IF_NEED_LEN(size_t len)) const; |
765624f7 | 169 | bool GetMatch(size_t *start, size_t *len, size_t index = 0) const; |
86b79b93 | 170 | size_t GetMatchCount() const; |
765624f7 VZ |
171 | int Replace(wxString *pattern, const wxString& replacement, |
172 | size_t maxMatches = 0) const; | |
e7208277 VZ |
173 | |
174 | private: | |
175 | // return the string containing the error message for the given err code | |
e3f9e20c | 176 | wxString GetErrorMsg(int errorcode, bool badconv) const; |
e7208277 | 177 | |
a9103a81 VZ |
178 | // init the members |
179 | void Init() | |
180 | { | |
701a0b47 | 181 | m_isCompiled = false; |
a9103a81 VZ |
182 | m_Matches = NULL; |
183 | m_nMatches = 0; | |
184 | } | |
185 | ||
e7208277 VZ |
186 | // free the RE if compiled |
187 | void Free() | |
188 | { | |
189 | if ( IsValid() ) | |
190 | { | |
7553e636 | 191 | wx_regfree(&m_RegEx); |
e7208277 | 192 | } |
a9103a81 | 193 | |
ab0f0edd | 194 | delete m_Matches; |
e7208277 VZ |
195 | } |
196 | ||
a9103a81 VZ |
197 | // free the RE if any and reinit the members |
198 | void Reinit() | |
199 | { | |
200 | Free(); | |
201 | Init(); | |
202 | } | |
203 | ||
e7208277 | 204 | // compiled RE |
ab0f0edd | 205 | regex_t m_RegEx; |
e7208277 VZ |
206 | |
207 | // the subexpressions data | |
ab0f0edd MW |
208 | wxRegExMatches *m_Matches; |
209 | size_t m_nMatches; | |
e7208277 | 210 | |
701a0b47 | 211 | // true if m_RegEx is valid |
ab0f0edd | 212 | bool m_isCompiled; |
e7208277 VZ |
213 | }; |
214 | ||
ab0f0edd | 215 | |
e7208277 VZ |
216 | // ============================================================================ |
217 | // implementation | |
218 | // ============================================================================ | |
219 | ||
220 | // ---------------------------------------------------------------------------- | |
221 | // wxRegExImpl | |
222 | // ---------------------------------------------------------------------------- | |
223 | ||
224 | wxRegExImpl::wxRegExImpl() | |
225 | { | |
a9103a81 | 226 | Init(); |
e7208277 VZ |
227 | } |
228 | ||
229 | wxRegExImpl::~wxRegExImpl() | |
230 | { | |
231 | Free(); | |
e7208277 VZ |
232 | } |
233 | ||
e3f9e20c | 234 | wxString wxRegExImpl::GetErrorMsg(int errorcode, bool badconv) const |
e7208277 | 235 | { |
db9db9cd | 236 | #ifdef WXREGEX_CONVERT_TO_MB |
e3f9e20c VS |
237 | // currently only needed when using system library in Unicode mode |
238 | if ( badconv ) | |
239 | { | |
240 | return _("conversion to 8-bit encoding failed"); | |
241 | } | |
242 | #else | |
243 | // 'use' badconv to avoid a compiler warning | |
244 | (void)badconv; | |
245 | #endif | |
246 | ||
c5feba0e | 247 | wxString szError; |
e7208277 VZ |
248 | |
249 | // first get the string length needed | |
7553e636 | 250 | int len = wx_regerror(errorcode, &m_RegEx, NULL, 0); |
e7208277 VZ |
251 | if ( len > 0 ) |
252 | { | |
c5feba0e | 253 | char* szcmbError = new char[++len]; |
e7208277 | 254 | |
7553e636 | 255 | (void)wx_regerror(errorcode, &m_RegEx, szcmbError, len); |
9aee09a3 | 256 | |
6d50049a | 257 | szError = wxConvLibc.cMB2WX(szcmbError); |
c5feba0e | 258 | delete [] szcmbError; |
e7208277 VZ |
259 | } |
260 | else // regerror() returned 0 | |
261 | { | |
c5feba0e | 262 | szError = _("unknown error"); |
e7208277 VZ |
263 | } |
264 | ||
c5feba0e | 265 | return szError; |
e7208277 VZ |
266 | } |
267 | ||
268 | bool wxRegExImpl::Compile(const wxString& expr, int flags) | |
269 | { | |
a9103a81 | 270 | Reinit(); |
e7208277 | 271 | |
e3f9e20c VS |
272 | #ifdef WX_NO_REGEX_ADVANCED |
273 | # define FLAVORS wxRE_BASIC | |
274 | #else | |
275 | # define FLAVORS (wxRE_ADVANCED | wxRE_BASIC) | |
276 | wxASSERT_MSG( (flags & FLAVORS) != FLAVORS, | |
277 | _T("incompatible flags in wxRegEx::Compile") ); | |
278 | #endif | |
279 | wxASSERT_MSG( !(flags & ~(FLAVORS | wxRE_ICASE | wxRE_NOSUB | wxRE_NEWLINE)), | |
e7208277 VZ |
280 | _T("unrecognized flags in wxRegEx::Compile") ); |
281 | ||
e3f9e20c | 282 | // translate our flags to regcomp() ones |
e7208277 VZ |
283 | int flagsRE = 0; |
284 | if ( !(flags & wxRE_BASIC) ) | |
2a230426 | 285 | { |
e3f9e20c VS |
286 | #ifndef WX_NO_REGEX_ADVANCED |
287 | if (flags & wxRE_ADVANCED) | |
288 | flagsRE |= REG_ADVANCED; | |
289 | else | |
290 | #endif | |
291 | flagsRE |= REG_EXTENDED; | |
2a230426 | 292 | } |
e7208277 VZ |
293 | if ( flags & wxRE_ICASE ) |
294 | flagsRE |= REG_ICASE; | |
295 | if ( flags & wxRE_NOSUB ) | |
296 | flagsRE |= REG_NOSUB; | |
297 | if ( flags & wxRE_NEWLINE ) | |
298 | flagsRE |= REG_NEWLINE; | |
299 | ||
e3f9e20c | 300 | // compile it |
c9eee7f0 | 301 | #ifdef WXREGEX_USING_BUILTIN |
e3f9e20c | 302 | bool conv = true; |
52de37c7 VS |
303 | // FIXME-UTF8: use wc_str() after removing ANSI build |
304 | int errorcode = wx_re_comp(&m_RegEx, expr.c_str(), expr.length(), flagsRE); | |
e3f9e20c | 305 | #else |
52de37c7 VS |
306 | // FIXME-UTF8: this is potentially broken, we shouldn't even try it |
307 | // and should always use builtin regex library (or PCRE?) | |
e3f9e20c VS |
308 | const wxWX2MBbuf conv = expr.mbc_str(); |
309 | int errorcode = conv ? regcomp(&m_RegEx, conv, flagsRE) : REG_BADPAT; | |
310 | #endif | |
49bf0b01 | 311 | |
e3f9e20c | 312 | if ( errorcode ) |
e7208277 VZ |
313 | { |
314 | wxLogError(_("Invalid regular expression '%s': %s"), | |
e3f9e20c | 315 | expr.c_str(), GetErrorMsg(errorcode, !conv).c_str()); |
e7208277 | 316 | |
701a0b47 | 317 | m_isCompiled = false; |
e7208277 VZ |
318 | } |
319 | else // ok | |
320 | { | |
321 | // don't allocate the matches array now, but do it later if necessary | |
322 | if ( flags & wxRE_NOSUB ) | |
323 | { | |
324 | // we don't need it at all | |
325 | m_nMatches = 0; | |
326 | } | |
327 | else | |
328 | { | |
59078e62 VZ |
329 | // we will alloc the array later (only if really needed) but count |
330 | // the number of sub-expressions in the regex right now | |
331 | ||
332 | // there is always one for the whole expression | |
333 | m_nMatches = 1; | |
334 | ||
335 | // and some more for bracketed subexperessions | |
71b5725f | 336 | for ( const wxChar *cptr = expr.c_str(); *cptr; cptr++ ) |
59078e62 | 337 | { |
71b5725f | 338 | if ( *cptr == _T('\\') ) |
59078e62 | 339 | { |
71b5725f VZ |
340 | // in basic RE syntax groups are inside \(...\) |
341 | if ( *++cptr == _T('(') && (flags & wxRE_BASIC) ) | |
342 | { | |
343 | m_nMatches++; | |
344 | } | |
345 | } | |
346 | else if ( *cptr == _T('(') && !(flags & wxRE_BASIC) ) | |
347 | { | |
348 | // we know that the previous character is not an unquoted | |
349 | // backslash because it would have been eaten above, so we | |
86b79b93 VS |
350 | // have a bare '(' and this indicates a group start for the |
351 | // extended syntax. '(?' is used for extensions by perl- | |
352 | // like REs (e.g. advanced), and is not valid for POSIX | |
353 | // extended, so ignore them always. | |
354 | if ( cptr[1] != _T('?') ) | |
355 | m_nMatches++; | |
59078e62 | 356 | } |
59078e62 | 357 | } |
e7208277 VZ |
358 | } |
359 | ||
701a0b47 | 360 | m_isCompiled = true; |
e7208277 VZ |
361 | } |
362 | ||
363 | return IsValid(); | |
364 | } | |
365 | ||
7553e636 | 366 | #ifdef WXREGEX_USING_RE_SEARCH |
ab0f0edd MW |
367 | |
368 | // On GNU, regexec is implemented as a wrapper around re_search. re_search | |
369 | // requires a length parameter which the POSIX regexec does not have, | |
370 | // therefore regexec must do a strlen on the search text each time it is | |
371 | // called. This can drastically affect performance when matching is done in | |
372 | // a loop along a string, such as during a search and replace. Therefore if | |
373 | // re_search is detected by configure, it is used directly. | |
374 | // | |
375 | static int ReSearch(const regex_t *preg, | |
376 | const char *text, | |
377 | size_t len, | |
378 | re_registers *matches, | |
379 | int eflags) | |
380 | { | |
381 | regex_t *pattern = wx_const_cast(regex_t*, preg); | |
382 | ||
383 | pattern->not_bol = (eflags & REG_NOTBOL) != 0; | |
384 | pattern->not_eol = (eflags & REG_NOTEOL) != 0; | |
385 | pattern->regs_allocated = REGS_FIXED; | |
386 | ||
387 | int ret = re_search(pattern, text, len, 0, len, matches); | |
388 | return ret >= 0 ? 0 : REG_NOMATCH; | |
389 | } | |
390 | ||
7553e636 | 391 | #endif // WXREGEX_USING_RE_SEARCH |
ab0f0edd | 392 | |
c9eee7f0 MW |
393 | bool wxRegExImpl::Matches(const wxRegChar *str, |
394 | int flags | |
ab0f0edd | 395 | WXREGEX_IF_NEED_LEN(size_t len)) const |
e7208277 | 396 | { |
701a0b47 | 397 | wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") ); |
e7208277 VZ |
398 | |
399 | // translate our flags to regexec() ones | |
400 | wxASSERT_MSG( !(flags & ~(wxRE_NOTBOL | wxRE_NOTEOL)), | |
401 | _T("unrecognized flags in wxRegEx::Matches") ); | |
402 | ||
403 | int flagsRE = 0; | |
404 | if ( flags & wxRE_NOTBOL ) | |
405 | flagsRE |= REG_NOTBOL; | |
406 | if ( flags & wxRE_NOTEOL ) | |
407 | flagsRE |= REG_NOTEOL; | |
408 | ||
409 | // allocate matches array if needed | |
410 | wxRegExImpl *self = wxConstCast(this, wxRegExImpl); | |
411 | if ( !m_Matches && m_nMatches ) | |
412 | { | |
ab0f0edd | 413 | self->m_Matches = new wxRegExMatches(m_nMatches); |
e7208277 VZ |
414 | } |
415 | ||
ab0f0edd MW |
416 | wxRegExMatches::match_type matches = m_Matches ? m_Matches->get() : NULL; |
417 | ||
e7208277 | 418 | // do match it |
ab0f0edd MW |
419 | #if defined WXREGEX_USING_BUILTIN |
420 | int rc = wx_re_exec(&self->m_RegEx, str, len, NULL, m_nMatches, matches, flagsRE); | |
7553e636 | 421 | #elif defined WXREGEX_USING_RE_SEARCH |
ab0f0edd | 422 | int rc = str ? ReSearch(&self->m_RegEx, str, len, matches, flagsRE) : REG_BADPAT; |
e3f9e20c | 423 | #else |
ab0f0edd | 424 | int rc = str ? regexec(&self->m_RegEx, str, m_nMatches, matches, flagsRE) : REG_BADPAT; |
e3f9e20c | 425 | #endif |
e7208277 VZ |
426 | |
427 | switch ( rc ) | |
428 | { | |
429 | case 0: | |
430 | // matched successfully | |
701a0b47 | 431 | return true; |
e7208277 VZ |
432 | |
433 | default: | |
3103e8a9 | 434 | // an error occurred |
f302b6a4 MW |
435 | wxLogError(_("Failed to find match for regular expression: %s"), |
436 | GetErrorMsg(rc, !str).c_str()); | |
e7208277 VZ |
437 | // fall through |
438 | ||
439 | case REG_NOMATCH: | |
440 | // no match | |
701a0b47 | 441 | return false; |
e7208277 VZ |
442 | } |
443 | } | |
444 | ||
445 | bool wxRegExImpl::GetMatch(size_t *start, size_t *len, size_t index) const | |
446 | { | |
701a0b47 WS |
447 | wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") ); |
448 | wxCHECK_MSG( m_nMatches, false, _T("can't use with wxRE_NOSUB") ); | |
449 | wxCHECK_MSG( m_Matches, false, _T("must call Matches() first") ); | |
450 | wxCHECK_MSG( index < m_nMatches, false, _T("invalid match index") ); | |
e7208277 | 451 | |
e7208277 | 452 | if ( start ) |
ab0f0edd | 453 | *start = m_Matches->Start(index); |
e7208277 | 454 | if ( len ) |
ab0f0edd | 455 | *len = m_Matches->End(index) - m_Matches->Start(index); |
e7208277 | 456 | |
701a0b47 | 457 | return true; |
e7208277 VZ |
458 | } |
459 | ||
86b79b93 VS |
460 | size_t wxRegExImpl::GetMatchCount() const |
461 | { | |
462 | wxCHECK_MSG( IsValid(), 0, _T("must successfully Compile() first") ); | |
463 | wxCHECK_MSG( m_nMatches, 0, _T("can't use with wxRE_NOSUB") ); | |
464 | ||
465 | return m_nMatches; | |
466 | } | |
467 | ||
765624f7 VZ |
468 | int wxRegExImpl::Replace(wxString *text, |
469 | const wxString& replacement, | |
470 | size_t maxMatches) const | |
e7208277 | 471 | { |
701a0b47 WS |
472 | wxCHECK_MSG( text, wxNOT_FOUND, _T("NULL text in wxRegEx::Replace") ); |
473 | wxCHECK_MSG( IsValid(), wxNOT_FOUND, _T("must successfully Compile() first") ); | |
e7208277 | 474 | |
f302b6a4 | 475 | // the input string |
db9db9cd | 476 | #ifndef WXREGEX_CONVERT_TO_MB |
f302b6a4 MW |
477 | const wxChar *textstr = text->c_str(); |
478 | size_t textlen = text->length(); | |
479 | #else | |
ab0f0edd | 480 | const wxWX2MBbuf textstr = WXREGEX_CHAR(*text); |
f302b6a4 MW |
481 | if (!textstr) |
482 | { | |
483 | wxLogError(_("Failed to find match for regular expression: %s"), | |
484 | GetErrorMsg(0, true).c_str()); | |
485 | return 0; | |
486 | } | |
487 | size_t textlen = strlen(textstr); | |
488 | text->clear(); | |
489 | #endif | |
490 | ||
765624f7 VZ |
491 | // the replacement text |
492 | wxString textNew; | |
e7208277 | 493 | |
f302b6a4 MW |
494 | // the result, allow 25% extra |
495 | wxString result; | |
496 | result.reserve(5 * textlen / 4); | |
497 | ||
765624f7 VZ |
498 | // attempt at optimization: don't iterate over the string if it doesn't |
499 | // contain back references at all | |
500 | bool mayHaveBackrefs = | |
501 | replacement.find_first_of(_T("\\&")) != wxString::npos; | |
e7208277 | 502 | |
765624f7 | 503 | if ( !mayHaveBackrefs ) |
e7208277 | 504 | { |
765624f7 | 505 | textNew = replacement; |
e7208277 | 506 | } |
765624f7 VZ |
507 | |
508 | // the position where we start looking for the match | |
765624f7 VZ |
509 | size_t matchStart = 0; |
510 | ||
511 | // number of replacement made: we won't make more than maxMatches of them | |
512 | // (unless maxMatches is 0 which doesn't limit the number of replacements) | |
513 | size_t countRepl = 0; | |
514 | ||
515 | // note that "^" shouldn't match after the first call to Matches() so we | |
516 | // use wxRE_NOTBOL to prevent it from happening | |
517 | while ( (!maxMatches || countRepl < maxMatches) && | |
7e1df0e6 | 518 | #ifndef WXREGEX_CONVERT_TO_MB |
f302b6a4 | 519 | Matches(textstr + matchStart, |
7e1df0e6 VS |
520 | #else |
521 | Matches(textstr.data() + matchStart, | |
522 | #endif | |
c9eee7f0 | 523 | countRepl ? wxRE_NOTBOL : 0 |
ab0f0edd | 524 | WXREGEX_IF_NEED_LEN(textlen - matchStart)) ) |
765624f7 VZ |
525 | { |
526 | // the string possibly contains back references: we need to calculate | |
527 | // the replacement text anew after each match | |
528 | if ( mayHaveBackrefs ) | |
529 | { | |
701a0b47 | 530 | mayHaveBackrefs = false; |
765624f7 VZ |
531 | textNew.clear(); |
532 | textNew.reserve(replacement.length()); | |
533 | ||
534 | for ( const wxChar *p = replacement.c_str(); *p; p++ ) | |
535 | { | |
536 | size_t index = (size_t)-1; | |
537 | ||
538 | if ( *p == _T('\\') ) | |
539 | { | |
540 | if ( wxIsdigit(*++p) ) | |
541 | { | |
542 | // back reference | |
543 | wxChar *end; | |
544 | index = (size_t)wxStrtoul(p, &end, 10); | |
545 | p = end - 1; // -1 to compensate for p++ in the loop | |
546 | } | |
547 | //else: backslash used as escape character | |
548 | } | |
549 | else if ( *p == _T('&') ) | |
550 | { | |
551 | // treat this as "\0" for compatbility with ed and such | |
552 | index = 0; | |
553 | } | |
554 | ||
555 | // do we have a back reference? | |
556 | if ( index != (size_t)-1 ) | |
557 | { | |
558 | // yes, get its text | |
559 | size_t start, len; | |
560 | if ( !GetMatch(&start, &len, index) ) | |
561 | { | |
59078e62 | 562 | wxFAIL_MSG( _T("invalid back reference") ); |
765624f7 VZ |
563 | |
564 | // just eat it... | |
565 | } | |
566 | else | |
567 | { | |
7e1df0e6 | 568 | #ifndef WXREGEX_CONVERT_TO_MB |
f302b6a4 | 569 | textNew += wxString(textstr + matchStart + start, |
7e1df0e6 VS |
570 | #else |
571 | textNew += wxString(textstr.data() + matchStart + | |
572 | start, | |
573 | #endif | |
f302b6a4 | 574 | *wxConvCurrent, len); |
765624f7 | 575 | |
701a0b47 | 576 | mayHaveBackrefs = true; |
765624f7 VZ |
577 | } |
578 | } | |
579 | else // ordinary character | |
580 | { | |
581 | textNew += *p; | |
582 | } | |
583 | } | |
584 | } | |
585 | ||
586 | size_t start, len; | |
587 | if ( !GetMatch(&start, &len) ) | |
588 | { | |
589 | // we did have match as Matches() returned true above! | |
590 | wxFAIL_MSG( _T("internal logic error in wxRegEx::Replace") ); | |
591 | ||
701a0b47 | 592 | return wxNOT_FOUND; |
765624f7 VZ |
593 | } |
594 | ||
f302b6a4 MW |
595 | // an insurance against implementations that don't grow exponentially |
596 | // to ensure building the result takes linear time | |
597 | if (result.capacity() < result.length() + start + textNew.length()) | |
598 | result.reserve(2 * result.length()); | |
599 | ||
db9db9cd | 600 | #ifndef WXREGEX_CONVERT_TO_MB |
f302b6a4 MW |
601 | result.append(*text, matchStart, start); |
602 | #else | |
7e1df0e6 VS |
603 | result.append(wxString(textstr.data() + matchStart, *wxConvCurrent, |
604 | start)); | |
f302b6a4 | 605 | #endif |
765624f7 | 606 | matchStart += start; |
f302b6a4 | 607 | result.append(textNew); |
765624f7 VZ |
608 | |
609 | countRepl++; | |
610 | ||
f302b6a4 | 611 | matchStart += len; |
765624f7 VZ |
612 | } |
613 | ||
db9db9cd | 614 | #ifndef WXREGEX_CONVERT_TO_MB |
f302b6a4 MW |
615 | result.append(*text, matchStart, wxString::npos); |
616 | #else | |
7e1df0e6 | 617 | result.append(wxString(textstr.data() + matchStart, *wxConvCurrent)); |
f302b6a4 MW |
618 | #endif |
619 | *text = result; | |
620 | ||
765624f7 | 621 | return countRepl; |
e7208277 VZ |
622 | } |
623 | ||
624 | // ---------------------------------------------------------------------------- | |
625 | // wxRegEx: all methods are mostly forwarded to wxRegExImpl | |
626 | // ---------------------------------------------------------------------------- | |
627 | ||
628 | void wxRegEx::Init() | |
629 | { | |
630 | m_impl = NULL; | |
631 | } | |
632 | ||
e7208277 VZ |
633 | wxRegEx::~wxRegEx() |
634 | { | |
635 | delete m_impl; | |
636 | } | |
637 | ||
638 | bool wxRegEx::Compile(const wxString& expr, int flags) | |
639 | { | |
640 | if ( !m_impl ) | |
641 | { | |
642 | m_impl = new wxRegExImpl; | |
643 | } | |
644 | ||
645 | if ( !m_impl->Compile(expr, flags) ) | |
646 | { | |
647 | // error message already given in wxRegExImpl::Compile | |
648 | delete m_impl; | |
649 | m_impl = NULL; | |
650 | ||
701a0b47 | 651 | return false; |
e7208277 VZ |
652 | } |
653 | ||
701a0b47 | 654 | return true; |
e7208277 VZ |
655 | } |
656 | ||
63465885 | 657 | bool wxRegEx::Matches(const wxString& str, int flags) const |
c9eee7f0 MW |
658 | { |
659 | wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") ); | |
c9eee7f0 | 660 | |
63465885 VZ |
661 | return m_impl->Matches(WXREGEX_CHAR(str), flags |
662 | WXREGEX_IF_NEED_LEN(str.length())); | |
e7208277 VZ |
663 | } |
664 | ||
665 | bool wxRegEx::GetMatch(size_t *start, size_t *len, size_t index) const | |
666 | { | |
701a0b47 | 667 | wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") ); |
e7208277 VZ |
668 | |
669 | return m_impl->GetMatch(start, len, index); | |
670 | } | |
671 | ||
00e6c2bd VZ |
672 | wxString wxRegEx::GetMatch(const wxString& text, size_t index) const |
673 | { | |
674 | size_t start, len; | |
675 | if ( !GetMatch(&start, &len, index) ) | |
676 | return wxEmptyString; | |
677 | ||
678 | return text.Mid(start, len); | |
679 | } | |
680 | ||
86b79b93 VS |
681 | size_t wxRegEx::GetMatchCount() const |
682 | { | |
683 | wxCHECK_MSG( IsValid(), 0, _T("must successfully Compile() first") ); | |
684 | ||
685 | return m_impl->GetMatchCount(); | |
686 | } | |
687 | ||
765624f7 VZ |
688 | int wxRegEx::Replace(wxString *pattern, |
689 | const wxString& replacement, | |
690 | size_t maxMatches) const | |
e7208277 | 691 | { |
701a0b47 | 692 | wxCHECK_MSG( IsValid(), wxNOT_FOUND, _T("must successfully Compile() first") ); |
e7208277 | 693 | |
765624f7 | 694 | return m_impl->Replace(pattern, replacement, maxMatches); |
e7208277 VZ |
695 | } |
696 | ||
697 | #endif // wxUSE_REGEX |