X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0f2ecc913ba8f6998a3c77001e20210aca0b9d0a..88e3652fd0c13feb4fcd843bd0d1167ab84041c1:/src/common/regex.cpp?ds=sidebyside diff --git a/src/common/regex.cpp b/src/common/regex.cpp index db11e97cec..9c7e55c040 100644 --- a/src/common/regex.cpp +++ b/src/common/regex.cpp @@ -18,7 +18,7 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "regex.h" #endif @@ -38,12 +38,14 @@ #include "wx/intl.h" #endif //WX_PRECOMP -// FreeBSD requires this, it probably doesn't hurt for others -#ifdef __UNIX__ +// FreeBSD & Watcom require this, it probably doesn't hurt for others +#if defined(__UNIX__) || defined(__WATCOMC__) || defined(__DIGITALMARS__) #include #endif +#ifndef __WXWINCE__ #include +#endif #include "wx/regex.h" @@ -73,17 +75,33 @@ private: // return the string containing the error message for the given err code wxString GetErrorMsg(int errorcode) const; + // init the members + void Init() + { + m_isCompiled = FALSE; + m_Matches = NULL; + m_nMatches = 0; + } + // free the RE if compiled void Free() { if ( IsValid() ) { regfree(&m_RegEx); - - m_isCompiled = FALSE; } + + delete [] m_Matches; } + // free the RE if any and reinit the members + void Reinit() + { + Free(); + Init(); + } + + // compiled RE regex_t m_RegEx; @@ -105,15 +123,12 @@ private: wxRegExImpl::wxRegExImpl() { - m_isCompiled = FALSE; - m_Matches = NULL; + Init(); } wxRegExImpl::~wxRegExImpl() { Free(); - - delete [] m_Matches; } wxString wxRegExImpl::GetErrorMsg(int errorcode) const @@ -126,9 +141,15 @@ wxString wxRegExImpl::GetErrorMsg(int errorcode) const { len++; - (void)regerror(errorcode, &m_RegEx, msg.GetWriteBuf(len), len); +#if wxUSE_UNICODE + wxCharBuffer buf(len); + + (void)regerror(errorcode, &m_RegEx, (char *)buf.data(), len); - msg.UngetWriteBuf(); + msg = wxString(buf.data(), wxConvLibc); +#else // !Unicode + (void)regerror(errorcode, &m_RegEx, wxStringBuffer(msg, len), len); +#endif // Unicode/!Unicode } else // regerror() returned 0 { @@ -140,7 +161,7 @@ wxString wxRegExImpl::GetErrorMsg(int errorcode) const bool wxRegExImpl::Compile(const wxString& expr, int flags) { - Free(); + Reinit(); // translate our flags to regcomp() ones wxASSERT_MSG( !(flags & @@ -158,7 +179,7 @@ bool wxRegExImpl::Compile(const wxString& expr, int flags) flagsRE |= REG_NEWLINE; // compile it - int errorcode = regcomp(&m_RegEx, expr, flagsRE); + int errorcode = regcomp(&m_RegEx, expr.mb_str(), flagsRE); if ( errorcode ) { wxLogError(_("Invalid regular expression '%s': %s"), @@ -176,8 +197,32 @@ bool wxRegExImpl::Compile(const wxString& expr, int flags) } else { - // will alloc later - m_nMatches = WX_REGEX_MAXMATCHES; + // we will alloc the array later (only if really needed) but count + // the number of sub-expressions in the regex right now + + // there is always one for the whole expression + m_nMatches = 1; + + // and some more for bracketed subexperessions + for ( const wxChar *cptr = expr.c_str(); *cptr; cptr++ ) + { + if ( *cptr == _T('\\') ) + { + // in basic RE syntax groups are inside \(...\) + if ( *++cptr == _T('(') && (flags & wxRE_BASIC) ) + { + m_nMatches++; + } + } + else if ( *cptr == _T('(') && !(flags & wxRE_BASIC) ) + { + // we know that the previous character is not an unquoted + // backslash because it would have been eaten above, so we + // have a bar '(' and this indicates a group start for the + // extended syntax + m_nMatches++; + } + } } m_isCompiled = TRUE; @@ -208,7 +253,7 @@ bool wxRegExImpl::Matches(const wxChar *str, int flags) const } // do match it - int rc = regexec(&self->m_RegEx, str, m_nMatches, m_Matches, flagsRE); + int rc = regexec(&self->m_RegEx, wxConvertWX2MB(str), m_nMatches, m_Matches, flagsRE); switch ( rc ) { @@ -235,8 +280,6 @@ bool wxRegExImpl::GetMatch(size_t *start, size_t *len, size_t index) const wxCHECK_MSG( index < m_nMatches, FALSE, _T("invalid match index") ); const regmatch_t& match = m_Matches[index]; - if ( match.rm_so == -1 ) - return FALSE; if ( start ) *start = match.rm_so; @@ -318,17 +361,7 @@ int wxRegExImpl::Replace(wxString *text, size_t start, len; if ( !GetMatch(&start, &len, index) ) { - // we can't do it because GetMatch() returns FALSE - // even for a valid back reference index if it didn't - // match for this expression (e.g. it when alternative - // branches were used and the one contained the back - // ref didn't match) - // - // it would be better to distinguish between this case - // and really invalid index, but I don't know how to - // do it - - //wxFAIL_MSG( _T("invalid back reference") ); + wxFAIL_MSG( _T("invalid back reference") ); // just eat it... }