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