]>
git.saurik.com Git - wxWidgets.git/blob - src/common/regex.cpp
12c790b915bda22886a24710046025799153179e
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/regex.cpp
3 // Purpose: regular expression matching
4 // Author: Karsten Ballüder and Vadim Zeitlin
8 // Copyright: (c) 2000 Karsten Ballüder <ballueder@gmx.net>
9 // 2001 Vadim Zeitlin <vadim@wxwindows.org>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
31 #include "wx/object.h"
32 #include "wx/string.h"
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>
48 // WXREGEX_USING_BUILTIN defined when using the built-in regex lib
49 // WXREGEX_IF_NEED_LEN() wrap the len parameter only used with the built-in
51 // WXREGEX_CONVERT_TO_MB defined when the regex lib is using chars and
52 // wxChar is wide, so conversion must be done
53 // WXREGEX_CHAR(x) Convert wxChar to wxRegChar
56 # define WXREGEX_USING_BUILTIN
57 # define WXREGEX_IF_NEED_LEN(x) ,x
58 # define WXREGEX_CHAR(x) x
60 # ifdef HAVE_RE_SEARCH
61 # define WXREGEX_IF_NEED_LEN(x) ,x
63 # define WXREGEX_IF_NEED_LEN(x)
66 # define WXREGEX_CONVERT_TO_MB
68 # define WXREGEX_CHAR(x) wxConvertWX2MB(x)
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 #ifndef HAVE_RE_SEARCH
77 // the array of offsets for the matches, the usual POSIX regmatch_t array.
81 typedef regmatch_t
*match_type
;
83 wxRegExMatches(size_t n
) { m_matches
= new regmatch_t
[n
]; }
84 ~wxRegExMatches() { delete [] m_matches
; }
86 size_t Start(size_t n
) const { return m_matches
[n
].rm_so
; }
87 size_t End(size_t n
) const { return m_matches
[n
].rm_eo
; }
89 regmatch_t
*get() const { return m_matches
; }
92 regmatch_t
*m_matches
;
95 #else // HAVE_RE_SEARCH
97 // the array of offsets for the matches, the struct used by the GNU lib
101 typedef re_registers
*match_type
;
103 wxRegExMatches(size_t n
)
105 m_matches
.num_regs
= n
;
106 m_matches
.start
= new regoff_t
[n
];
107 m_matches
.end
= new regoff_t
[n
];
112 delete [] m_matches
.start
;
113 delete [] m_matches
.end
;
116 size_t Start(size_t n
) const { return m_matches
.start
[n
]; }
117 size_t End(size_t n
) const { return m_matches
.end
[n
]; }
119 re_registers
*get() { return &m_matches
; }
122 re_registers m_matches
;
125 #endif // HAVE_RE_SEARCH
127 // the character type used by the regular expression engine
128 #ifndef WXREGEX_CONVERT_TO_MB
129 typedef wxChar wxRegChar
;
131 typedef char wxRegChar
;
134 // the real implementation of wxRegEx
142 // return true if Compile() had been called successfully
143 bool IsValid() const { return m_isCompiled
; }
146 bool Compile(const wxString
& expr
, int flags
= 0);
147 bool Matches(const wxRegChar
*str
, int flags
148 WXREGEX_IF_NEED_LEN(size_t len
)) const;
149 bool GetMatch(size_t *start
, size_t *len
, size_t index
= 0) const;
150 size_t GetMatchCount() const;
151 int Replace(wxString
*pattern
, const wxString
& replacement
,
152 size_t maxMatches
= 0) const;
155 // return the string containing the error message for the given err code
156 wxString
GetErrorMsg(int errorcode
, bool badconv
) const;
161 m_isCompiled
= false;
166 // free the RE if compiled
177 // free the RE if any and reinit the members
187 // the subexpressions data
188 wxRegExMatches
*m_Matches
;
191 // true if m_RegEx is valid
196 // ============================================================================
198 // ============================================================================
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 wxRegExImpl::wxRegExImpl()
209 wxRegExImpl::~wxRegExImpl()
214 wxString
wxRegExImpl::GetErrorMsg(int errorcode
, bool badconv
) const
216 #ifdef WXREGEX_CONVERT_TO_MB
217 // currently only needed when using system library in Unicode mode
220 return _("conversion to 8-bit encoding failed");
223 // 'use' badconv to avoid a compiler warning
229 // first get the string length needed
230 int len
= regerror(errorcode
, &m_RegEx
, NULL
, 0);
233 char* szcmbError
= new char[++len
];
235 (void)regerror(errorcode
, &m_RegEx
, szcmbError
, len
);
237 szError
= wxConvertMB2WX(szcmbError
);
238 delete [] szcmbError
;
240 else // regerror() returned 0
242 szError
= _("unknown error");
248 bool wxRegExImpl::Compile(const wxString
& expr
, int flags
)
252 #ifdef WX_NO_REGEX_ADVANCED
253 # define FLAVORS wxRE_BASIC
255 # define FLAVORS (wxRE_ADVANCED | wxRE_BASIC)
256 wxASSERT_MSG( (flags
& FLAVORS
) != FLAVORS
,
257 _T("incompatible flags in wxRegEx::Compile") );
259 wxASSERT_MSG( !(flags
& ~(FLAVORS
| wxRE_ICASE
| wxRE_NOSUB
| wxRE_NEWLINE
)),
260 _T("unrecognized flags in wxRegEx::Compile") );
262 // translate our flags to regcomp() ones
264 if ( !(flags
& wxRE_BASIC
) )
265 #ifndef WX_NO_REGEX_ADVANCED
266 if (flags
& wxRE_ADVANCED
)
267 flagsRE
|= REG_ADVANCED
;
270 flagsRE
|= REG_EXTENDED
;
271 if ( flags
& wxRE_ICASE
)
272 flagsRE
|= REG_ICASE
;
273 if ( flags
& wxRE_NOSUB
)
274 flagsRE
|= REG_NOSUB
;
275 if ( flags
& wxRE_NEWLINE
)
276 flagsRE
|= REG_NEWLINE
;
279 #ifdef WXREGEX_USING_BUILTIN
281 int errorcode
= wx_re_comp(&m_RegEx
, expr
, expr
.length(), flagsRE
);
283 const wxWX2MBbuf conv
= expr
.mbc_str();
284 int errorcode
= conv
? regcomp(&m_RegEx
, conv
, flagsRE
) : REG_BADPAT
;
289 wxLogError(_("Invalid regular expression '%s': %s"),
290 expr
.c_str(), GetErrorMsg(errorcode
, !conv
).c_str());
292 m_isCompiled
= false;
296 // don't allocate the matches array now, but do it later if necessary
297 if ( flags
& wxRE_NOSUB
)
299 // we don't need it at all
304 // we will alloc the array later (only if really needed) but count
305 // the number of sub-expressions in the regex right now
307 // there is always one for the whole expression
310 // and some more for bracketed subexperessions
311 for ( const wxChar
*cptr
= expr
.c_str(); *cptr
; cptr
++ )
313 if ( *cptr
== _T('\\') )
315 // in basic RE syntax groups are inside \(...\)
316 if ( *++cptr
== _T('(') && (flags
& wxRE_BASIC
) )
321 else if ( *cptr
== _T('(') && !(flags
& wxRE_BASIC
) )
323 // we know that the previous character is not an unquoted
324 // backslash because it would have been eaten above, so we
325 // have a bare '(' and this indicates a group start for the
326 // extended syntax. '(?' is used for extensions by perl-
327 // like REs (e.g. advanced), and is not valid for POSIX
328 // extended, so ignore them always.
329 if ( cptr
[1] != _T('?') )
341 #ifdef HAVE_RE_SEARCH
343 // On GNU, regexec is implemented as a wrapper around re_search. re_search
344 // requires a length parameter which the POSIX regexec does not have,
345 // therefore regexec must do a strlen on the search text each time it is
346 // called. This can drastically affect performance when matching is done in
347 // a loop along a string, such as during a search and replace. Therefore if
348 // re_search is detected by configure, it is used directly.
350 static int ReSearch(const regex_t
*preg
,
353 re_registers
*matches
,
356 regex_t
*pattern
= wx_const_cast(regex_t
*, preg
);
358 pattern
->not_bol
= (eflags
& REG_NOTBOL
) != 0;
359 pattern
->not_eol
= (eflags
& REG_NOTEOL
) != 0;
360 pattern
->regs_allocated
= REGS_FIXED
;
362 int ret
= re_search(pattern
, text
, len
, 0, len
, matches
);
363 return ret
>= 0 ? 0 : REG_NOMATCH
;
366 #endif // HAVE_RE_SEARCH
368 bool wxRegExImpl::Matches(const wxRegChar
*str
,
370 WXREGEX_IF_NEED_LEN(size_t len
)) const
372 wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
374 // translate our flags to regexec() ones
375 wxASSERT_MSG( !(flags
& ~(wxRE_NOTBOL
| wxRE_NOTEOL
)),
376 _T("unrecognized flags in wxRegEx::Matches") );
379 if ( flags
& wxRE_NOTBOL
)
380 flagsRE
|= REG_NOTBOL
;
381 if ( flags
& wxRE_NOTEOL
)
382 flagsRE
|= REG_NOTEOL
;
384 // allocate matches array if needed
385 wxRegExImpl
*self
= wxConstCast(this, wxRegExImpl
);
386 if ( !m_Matches
&& m_nMatches
)
388 self
->m_Matches
= new wxRegExMatches(m_nMatches
);
391 wxRegExMatches::match_type matches
= m_Matches
? m_Matches
->get() : NULL
;
394 #if defined WXREGEX_USING_BUILTIN
395 int rc
= wx_re_exec(&self
->m_RegEx
, str
, len
, NULL
, m_nMatches
, matches
, flagsRE
);
396 #elif defined HAVE_RE_SEARCH
397 int rc
= str
? ReSearch(&self
->m_RegEx
, str
, len
, matches
, flagsRE
) : REG_BADPAT
;
399 int rc
= str
? regexec(&self
->m_RegEx
, str
, m_nMatches
, matches
, flagsRE
) : REG_BADPAT
;
405 // matched successfully
410 wxLogError(_("Failed to find match for regular expression: %s"),
411 GetErrorMsg(rc
, !str
).c_str());
420 bool wxRegExImpl::GetMatch(size_t *start
, size_t *len
, size_t index
) const
422 wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
423 wxCHECK_MSG( m_nMatches
, false, _T("can't use with wxRE_NOSUB") );
424 wxCHECK_MSG( m_Matches
, false, _T("must call Matches() first") );
425 wxCHECK_MSG( index
< m_nMatches
, false, _T("invalid match index") );
428 *start
= m_Matches
->Start(index
);
430 *len
= m_Matches
->End(index
) - m_Matches
->Start(index
);
435 size_t wxRegExImpl::GetMatchCount() const
437 wxCHECK_MSG( IsValid(), 0, _T("must successfully Compile() first") );
438 wxCHECK_MSG( m_nMatches
, 0, _T("can't use with wxRE_NOSUB") );
443 int wxRegExImpl::Replace(wxString
*text
,
444 const wxString
& replacement
,
445 size_t maxMatches
) const
447 wxCHECK_MSG( text
, wxNOT_FOUND
, _T("NULL text in wxRegEx::Replace") );
448 wxCHECK_MSG( IsValid(), wxNOT_FOUND
, _T("must successfully Compile() first") );
451 #ifndef WXREGEX_CONVERT_TO_MB
452 const wxChar
*textstr
= text
->c_str();
453 size_t textlen
= text
->length();
455 const wxWX2MBbuf textstr
= WXREGEX_CHAR(*text
);
458 wxLogError(_("Failed to find match for regular expression: %s"),
459 GetErrorMsg(0, true).c_str());
462 size_t textlen
= strlen(textstr
);
466 // the replacement text
469 // the result, allow 25% extra
471 result
.reserve(5 * textlen
/ 4);
473 // attempt at optimization: don't iterate over the string if it doesn't
474 // contain back references at all
475 bool mayHaveBackrefs
=
476 replacement
.find_first_of(_T("\\&")) != wxString::npos
;
478 if ( !mayHaveBackrefs
)
480 textNew
= replacement
;
483 // the position where we start looking for the match
484 size_t matchStart
= 0;
486 // number of replacement made: we won't make more than maxMatches of them
487 // (unless maxMatches is 0 which doesn't limit the number of replacements)
488 size_t countRepl
= 0;
490 // note that "^" shouldn't match after the first call to Matches() so we
491 // use wxRE_NOTBOL to prevent it from happening
492 while ( (!maxMatches
|| countRepl
< maxMatches
) &&
493 Matches(textstr
+ matchStart
,
494 countRepl
? wxRE_NOTBOL
: 0
495 WXREGEX_IF_NEED_LEN(textlen
- matchStart
)) )
497 // the string possibly contains back references: we need to calculate
498 // the replacement text anew after each match
499 if ( mayHaveBackrefs
)
501 mayHaveBackrefs
= false;
503 textNew
.reserve(replacement
.length());
505 for ( const wxChar
*p
= replacement
.c_str(); *p
; p
++ )
507 size_t index
= (size_t)-1;
509 if ( *p
== _T('\\') )
511 if ( wxIsdigit(*++p
) )
515 index
= (size_t)wxStrtoul(p
, &end
, 10);
516 p
= end
- 1; // -1 to compensate for p++ in the loop
518 //else: backslash used as escape character
520 else if ( *p
== _T('&') )
522 // treat this as "\0" for compatbility with ed and such
526 // do we have a back reference?
527 if ( index
!= (size_t)-1 )
531 if ( !GetMatch(&start
, &len
, index
) )
533 wxFAIL_MSG( _T("invalid back reference") );
539 textNew
+= wxString(textstr
+ matchStart
+ start
,
540 *wxConvCurrent
, len
);
542 mayHaveBackrefs
= true;
545 else // ordinary character
553 if ( !GetMatch(&start
, &len
) )
555 // we did have match as Matches() returned true above!
556 wxFAIL_MSG( _T("internal logic error in wxRegEx::Replace") );
561 // an insurance against implementations that don't grow exponentially
562 // to ensure building the result takes linear time
563 if (result
.capacity() < result
.length() + start
+ textNew
.length())
564 result
.reserve(2 * result
.length());
566 #ifndef WXREGEX_CONVERT_TO_MB
567 result
.append(*text
, matchStart
, start
);
569 result
.append(wxString(textstr
+ matchStart
, *wxConvCurrent
, start
));
572 result
.append(textNew
);
579 #ifndef WXREGEX_CONVERT_TO_MB
580 result
.append(*text
, matchStart
, wxString::npos
);
582 result
.append(wxString(textstr
+ matchStart
, *wxConvCurrent
));
589 // ----------------------------------------------------------------------------
590 // wxRegEx: all methods are mostly forwarded to wxRegExImpl
591 // ----------------------------------------------------------------------------
603 bool wxRegEx::Compile(const wxString
& expr
, int flags
)
607 m_impl
= new wxRegExImpl
;
610 if ( !m_impl
->Compile(expr
, flags
) )
612 // error message already given in wxRegExImpl::Compile
622 bool wxRegEx::Matches(const wxChar
*str
, int flags
, size_t len
) const
624 wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
627 return m_impl
->Matches(WXREGEX_CHAR(str
), flags
WXREGEX_IF_NEED_LEN(len
));
630 bool wxRegEx::Matches(const wxChar
*str
, int flags
) const
632 wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
634 return m_impl
->Matches(WXREGEX_CHAR(str
),
636 WXREGEX_IF_NEED_LEN(wxStrlen(str
)));
639 bool wxRegEx::GetMatch(size_t *start
, size_t *len
, size_t index
) const
641 wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
643 return m_impl
->GetMatch(start
, len
, index
);
646 wxString
wxRegEx::GetMatch(const wxString
& text
, size_t index
) const
649 if ( !GetMatch(&start
, &len
, index
) )
650 return wxEmptyString
;
652 return text
.Mid(start
, len
);
655 size_t wxRegEx::GetMatchCount() const
657 wxCHECK_MSG( IsValid(), 0, _T("must successfully Compile() first") );
659 return m_impl
->GetMatchCount();
662 int wxRegEx::Replace(wxString
*pattern
,
663 const wxString
& replacement
,
664 size_t maxMatches
) const
666 wxCHECK_MSG( IsValid(), wxNOT_FOUND
, _T("must successfully Compile() first") );
668 return m_impl
->Replace(pattern
, replacement
, maxMatches
);
671 #endif // wxUSE_REGEX