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