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