]> git.saurik.com Git - wxWidgets.git/blob - src/common/regex.cpp
otherwise all runloop dependent callbacks cannot work outside of the main thread
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma implementation "regex.h"
23 #endif
24
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if wxUSE_REGEX
33
34 #ifndef WX_PRECOMP
35 #include "wx/object.h"
36 #include "wx/string.h"
37 #include "wx/log.h"
38 #include "wx/intl.h"
39 #endif //WX_PRECOMP
40
41 // FreeBSD, Watcom and DMars require this, CW doesn't have nor need it.
42 // Others also don't seem to need it. If you have an error related to
43 // (not) including <sys/types.h> please report details to
44 // wx-dev@lists.wxwindows.org
45 #if defined(__UNIX__) || defined(__WATCOMC__) || defined(__DIGITALMARS__)
46 # include <sys/types.h>
47 #endif
48
49 #include <regex.h>
50 #include "wx/regex.h"
51
52 // ----------------------------------------------------------------------------
53 // private classes
54 // ----------------------------------------------------------------------------
55
56 // the real implementation of wxRegEx
57 class wxRegExImpl
58 {
59 public:
60 // ctor and dtor
61 wxRegExImpl();
62 ~wxRegExImpl();
63
64 // return TRUE if Compile() had been called successfully
65 bool IsValid() const { return m_isCompiled; }
66
67 // RE operations
68 bool Compile(const wxString& expr, int flags = 0);
69 bool Matches(const wxChar *str, int flags = 0) const;
70 bool GetMatch(size_t *start, size_t *len, size_t index = 0) const;
71 int Replace(wxString *pattern, const wxString& replacement,
72 size_t maxMatches = 0) const;
73
74 private:
75 // return the string containing the error message for the given err code
76 wxString GetErrorMsg(int errorcode, bool badconv) const;
77
78 // init the members
79 void Init()
80 {
81 m_isCompiled = FALSE;
82 m_Matches = NULL;
83 m_nMatches = 0;
84 }
85
86 // free the RE if compiled
87 void Free()
88 {
89 if ( IsValid() )
90 {
91 regfree(&m_RegEx);
92 }
93
94 delete [] m_Matches;
95 }
96
97 // free the RE if any and reinit the members
98 void Reinit()
99 {
100 Free();
101 Init();
102 }
103
104
105 // compiled RE
106 regex_t m_RegEx;
107
108 // the subexpressions data
109 regmatch_t *m_Matches;
110 size_t m_nMatches;
111
112 // TRUE if m_RegEx is valid
113 bool m_isCompiled;
114 };
115
116 // ============================================================================
117 // implementation
118 // ============================================================================
119
120 // ----------------------------------------------------------------------------
121 // wxRegExImpl
122 // ----------------------------------------------------------------------------
123
124 wxRegExImpl::wxRegExImpl()
125 {
126 Init();
127 }
128
129 wxRegExImpl::~wxRegExImpl()
130 {
131 Free();
132 }
133
134 wxString wxRegExImpl::GetErrorMsg(int errorcode, bool badconv) const
135 {
136 #if wxUSE_UNICODE && !defined(__REG_NOFRONT)
137 // currently only needed when using system library in Unicode mode
138 if ( badconv )
139 {
140 return _("conversion to 8-bit encoding failed");
141 }
142 #else
143 // 'use' badconv to avoid a compiler warning
144 (void)badconv;
145 #endif
146
147 wxString szError;
148
149 // first get the string length needed
150 int len = regerror(errorcode, &m_RegEx, NULL, 0);
151 if ( len > 0 )
152 {
153 char* szcmbError = new char[++len];
154
155 (void)regerror(errorcode, &m_RegEx, szcmbError, len);
156
157 szError = wxConvertMB2WX(szcmbError);
158 delete [] szcmbError;
159 }
160 else // regerror() returned 0
161 {
162 szError = _("unknown error");
163 }
164
165 return szError;
166 }
167
168 bool wxRegExImpl::Compile(const wxString& expr, int flags)
169 {
170 Reinit();
171
172 #ifdef WX_NO_REGEX_ADVANCED
173 # define FLAVORS wxRE_BASIC
174 #else
175 # define FLAVORS (wxRE_ADVANCED | wxRE_BASIC)
176 wxASSERT_MSG( (flags & FLAVORS) != FLAVORS,
177 _T("incompatible flags in wxRegEx::Compile") );
178 #endif
179 wxASSERT_MSG( !(flags & ~(FLAVORS | wxRE_ICASE | wxRE_NOSUB | wxRE_NEWLINE)),
180 _T("unrecognized flags in wxRegEx::Compile") );
181
182 // translate our flags to regcomp() ones
183 int flagsRE = 0;
184 if ( !(flags & wxRE_BASIC) )
185 #ifndef WX_NO_REGEX_ADVANCED
186 if (flags & wxRE_ADVANCED)
187 flagsRE |= REG_ADVANCED;
188 else
189 #endif
190 flagsRE |= REG_EXTENDED;
191 if ( flags & wxRE_ICASE )
192 flagsRE |= REG_ICASE;
193 if ( flags & wxRE_NOSUB )
194 flagsRE |= REG_NOSUB;
195 if ( flags & wxRE_NEWLINE )
196 flagsRE |= REG_NEWLINE;
197
198 // compile it
199 #ifdef __REG_NOFRONT
200 bool conv = true;
201 int errorcode = re_comp(&m_RegEx, expr, expr.length(), flagsRE);
202 #else
203 const wxWX2MBbuf conv = expr.mbc_str();
204 int errorcode = conv ? regcomp(&m_RegEx, conv, flagsRE) : REG_BADPAT;
205 #endif
206
207 if ( errorcode )
208 {
209 wxLogError(_("Invalid regular expression '%s': %s"),
210 expr.c_str(), GetErrorMsg(errorcode, !conv).c_str());
211
212 m_isCompiled = FALSE;
213 }
214 else // ok
215 {
216 // don't allocate the matches array now, but do it later if necessary
217 if ( flags & wxRE_NOSUB )
218 {
219 // we don't need it at all
220 m_nMatches = 0;
221 }
222 else
223 {
224 // we will alloc the array later (only if really needed) but count
225 // the number of sub-expressions in the regex right now
226
227 // there is always one for the whole expression
228 m_nMatches = 1;
229
230 // and some more for bracketed subexperessions
231 for ( const wxChar *cptr = expr.c_str(); *cptr; cptr++ )
232 {
233 if ( *cptr == _T('\\') )
234 {
235 // in basic RE syntax groups are inside \(...\)
236 if ( *++cptr == _T('(') && (flags & wxRE_BASIC) )
237 {
238 m_nMatches++;
239 }
240 }
241 else if ( *cptr == _T('(') && !(flags & wxRE_BASIC) )
242 {
243 // we know that the previous character is not an unquoted
244 // backslash because it would have been eaten above, so we
245 // have a bar '(' and this indicates a group start for the
246 // extended syntax
247 m_nMatches++;
248 }
249 }
250 }
251
252 m_isCompiled = TRUE;
253 }
254
255 return IsValid();
256 }
257
258 bool wxRegExImpl::Matches(const wxChar *str, int flags) const
259 {
260 wxCHECK_MSG( IsValid(), FALSE, _T("must successfully Compile() first") );
261
262 // translate our flags to regexec() ones
263 wxASSERT_MSG( !(flags & ~(wxRE_NOTBOL | wxRE_NOTEOL)),
264 _T("unrecognized flags in wxRegEx::Matches") );
265
266 int flagsRE = 0;
267 if ( flags & wxRE_NOTBOL )
268 flagsRE |= REG_NOTBOL;
269 if ( flags & wxRE_NOTEOL )
270 flagsRE |= REG_NOTEOL;
271
272 // allocate matches array if needed
273 wxRegExImpl *self = wxConstCast(this, wxRegExImpl);
274 if ( !m_Matches && m_nMatches )
275 {
276 self->m_Matches = new regmatch_t[m_nMatches];
277 }
278
279 // do match it
280 #ifdef __REG_NOFRONT
281 bool conv = true;
282 int rc = re_exec(&self->m_RegEx, str, wxStrlen(str), NULL, m_nMatches, m_Matches, flagsRE);
283 #else
284 const wxWX2MBbuf conv = wxConvertWX2MB(str);
285 int rc = conv ? regexec(&self->m_RegEx, conv, m_nMatches, m_Matches, flagsRE) : REG_BADPAT;
286 #endif
287
288 switch ( rc )
289 {
290 case 0:
291 // matched successfully
292 return TRUE;
293
294 default:
295 // an error occured
296 wxLogError(_("Failed to match '%s' in regular expression: %s"),
297 str, GetErrorMsg(rc, !conv).c_str());
298 // fall through
299
300 case REG_NOMATCH:
301 // no match
302 return FALSE;
303 }
304 }
305
306 bool wxRegExImpl::GetMatch(size_t *start, size_t *len, size_t index) const
307 {
308 wxCHECK_MSG( IsValid(), FALSE, _T("must successfully Compile() first") );
309 wxCHECK_MSG( m_Matches, FALSE, _T("can't use with wxRE_NOSUB") );
310 wxCHECK_MSG( index < m_nMatches, FALSE, _T("invalid match index") );
311
312 const regmatch_t& match = m_Matches[index];
313
314 if ( start )
315 *start = match.rm_so;
316 if ( len )
317 *len = match.rm_eo - match.rm_so;
318
319 return TRUE;
320 }
321
322 int wxRegExImpl::Replace(wxString *text,
323 const wxString& replacement,
324 size_t maxMatches) const
325 {
326 wxCHECK_MSG( text, -1, _T("NULL text in wxRegEx::Replace") );
327 wxCHECK_MSG( IsValid(), -1, _T("must successfully Compile() first") );
328
329 // the replacement text
330 wxString textNew;
331
332 // attempt at optimization: don't iterate over the string if it doesn't
333 // contain back references at all
334 bool mayHaveBackrefs =
335 replacement.find_first_of(_T("\\&")) != wxString::npos;
336
337 if ( !mayHaveBackrefs )
338 {
339 textNew = replacement;
340 }
341
342 // the position where we start looking for the match
343 //
344 // NB: initial version had a nasty bug because it used a wxChar* instead of
345 // an index but the problem is that replace() in the loop invalidates
346 // all pointers into the string so we have to use indices instead
347 size_t matchStart = 0;
348
349 // number of replacement made: we won't make more than maxMatches of them
350 // (unless maxMatches is 0 which doesn't limit the number of replacements)
351 size_t countRepl = 0;
352
353 // note that "^" shouldn't match after the first call to Matches() so we
354 // use wxRE_NOTBOL to prevent it from happening
355 while ( (!maxMatches || countRepl < maxMatches) &&
356 Matches(text->c_str() + matchStart, countRepl ? wxRE_NOTBOL : 0) )
357 {
358 // the string possibly contains back references: we need to calculate
359 // the replacement text anew after each match
360 if ( mayHaveBackrefs )
361 {
362 mayHaveBackrefs = FALSE;
363 textNew.clear();
364 textNew.reserve(replacement.length());
365
366 for ( const wxChar *p = replacement.c_str(); *p; p++ )
367 {
368 size_t index = (size_t)-1;
369
370 if ( *p == _T('\\') )
371 {
372 if ( wxIsdigit(*++p) )
373 {
374 // back reference
375 wxChar *end;
376 index = (size_t)wxStrtoul(p, &end, 10);
377 p = end - 1; // -1 to compensate for p++ in the loop
378 }
379 //else: backslash used as escape character
380 }
381 else if ( *p == _T('&') )
382 {
383 // treat this as "\0" for compatbility with ed and such
384 index = 0;
385 }
386
387 // do we have a back reference?
388 if ( index != (size_t)-1 )
389 {
390 // yes, get its text
391 size_t start, len;
392 if ( !GetMatch(&start, &len, index) )
393 {
394 wxFAIL_MSG( _T("invalid back reference") );
395
396 // just eat it...
397 }
398 else
399 {
400 textNew += wxString(text->c_str() + matchStart + start,
401 len);
402
403 mayHaveBackrefs = TRUE;
404 }
405 }
406 else // ordinary character
407 {
408 textNew += *p;
409 }
410 }
411 }
412
413 size_t start, len;
414 if ( !GetMatch(&start, &len) )
415 {
416 // we did have match as Matches() returned true above!
417 wxFAIL_MSG( _T("internal logic error in wxRegEx::Replace") );
418
419 return -1;
420 }
421
422 matchStart += start;
423 text->replace(matchStart, len, textNew);
424
425 countRepl++;
426
427 matchStart += textNew.length();
428 }
429
430 return countRepl;
431 }
432
433 // ----------------------------------------------------------------------------
434 // wxRegEx: all methods are mostly forwarded to wxRegExImpl
435 // ----------------------------------------------------------------------------
436
437 void wxRegEx::Init()
438 {
439 m_impl = NULL;
440 }
441
442
443 wxRegEx::~wxRegEx()
444 {
445 delete m_impl;
446 }
447
448 bool wxRegEx::Compile(const wxString& expr, int flags)
449 {
450 if ( !m_impl )
451 {
452 m_impl = new wxRegExImpl;
453 }
454
455 if ( !m_impl->Compile(expr, flags) )
456 {
457 // error message already given in wxRegExImpl::Compile
458 delete m_impl;
459 m_impl = NULL;
460
461 return FALSE;
462 }
463
464 return TRUE;
465 }
466
467 bool wxRegEx::Matches(const wxChar *str, int flags) const
468 {
469 wxCHECK_MSG( IsValid(), FALSE, _T("must successfully Compile() first") );
470
471 return m_impl->Matches(str, flags);
472 }
473
474 bool wxRegEx::GetMatch(size_t *start, size_t *len, size_t index) const
475 {
476 wxCHECK_MSG( IsValid(), FALSE, _T("must successfully Compile() first") );
477
478 return m_impl->GetMatch(start, len, index);
479 }
480
481 wxString wxRegEx::GetMatch(const wxString& text, size_t index) const
482 {
483 size_t start, len;
484 if ( !GetMatch(&start, &len, index) )
485 return wxEmptyString;
486
487 return text.Mid(start, len);
488 }
489
490 int wxRegEx::Replace(wxString *pattern,
491 const wxString& replacement,
492 size_t maxMatches) const
493 {
494 wxCHECK_MSG( IsValid(), -1, _T("must successfully Compile() first") );
495
496 return m_impl->Replace(pattern, replacement, maxMatches);
497 }
498
499 #endif // wxUSE_REGEX