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