From c9eee7f035ebc6b5c044475d508b018ae9fb5bc2 Mon Sep 17 00:00:00 2001
From: Michael Wetherell <mike.wetherell@ntlworld.com>
Date: Tue, 15 Nov 2005 15:51:10 +0000
Subject: [PATCH] Add a length parameter for wxRegEx::Matches

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36182 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/regex.tex | 13 ++++++++++-
 include/wx/regex.h      |  4 ++++
 src/common/regex.cpp    | 48 ++++++++++++++++++++++++++++++-----------
 3 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/docs/latex/wx/regex.tex b/docs/latex/wx/regex.tex
index 52ea21d508..58687c9934 100644
--- a/docs/latex/wx/regex.tex
+++ b/docs/latex/wx/regex.tex
@@ -188,10 +188,21 @@ and only if {\tt wxRE\_NOSUB} was {\bf not} used.
 
 \constfunc{bool}{Matches}{\param{const wxChar* }{text}, \param{int }{flags = 0}}
 
+\constfunc{bool}{Matches}{\param{const wxChar* }{text}, \param{int }{flags}, \param{size\_t }{len}}
+
+\constfunc{bool}{Matches}{\param{const wxString\& }{text}, \param{int }{flags = 0}}
+
 Matches the precompiled regular expression against the string {\it text},
 returns {\tt true} if matches and {\tt false} otherwise.
 
-Flags may be combination of {\tt wxRE\_NOTBOL} and {\tt wxRE\_NOTEOL}.
+{\it Flags} may be combination of {\tt wxRE\_NOTBOL} and {\tt wxRE\_NOTEOL}.
+
+System regex libraries always assume the text being searched is null
+terminated and any length given is ignored.
+
+When using the built-in regex library, the first overload obtains the length
+of the string using wxStrlen, the second from the {\it len} parameter and the
+third from the length of the {\it wxString}.
 
 May only be called after successful call to \helpref{Compile()}{wxregexcompile}.
 
diff --git a/include/wx/regex.h b/include/wx/regex.h
index c30f0bf140..f32e2b83d9 100644
--- a/include/wx/regex.h
+++ b/include/wx/regex.h
@@ -94,9 +94,13 @@ public:
     // true if matches and false otherwise
     //
     // flags may be combination of wxRE_NOTBOL and wxRE_NOTEOL
+    // len may be the length of text (ignored except by built-in regex lib)
     //
     // may only be called after successful call to Compile()
     bool Matches(const wxChar *text, int flags = 0) const;
+    bool Matches(const wxChar *text, int flags, size_t len) const;
+    bool Matches(const wxString& text, int flags = 0) const
+        { return Matches(text.c_str(), flags, text.length()); }
 
     // get the start index and the length of the match of the expression
     // (index 0) or a bracketed subexpression (index != 0)
diff --git a/src/common/regex.cpp b/src/common/regex.cpp
index b5b1fb49d4..b3c78820df 100644
--- a/src/common/regex.cpp
+++ b/src/common/regex.cpp
@@ -45,9 +45,18 @@
 #include <regex.h>
 #include "wx/regex.h"
 
-// defined when the regex lib uses 'char' but 'wxChar' is wide
-#if wxUSE_UNICODE && !defined(__REG_NOFRONT)
-#   define WXREGEX_CONVERT_TO_MB
+// WXREGEX_USING_BUILTIN  defined when using the built-in regex lib
+// WXREGEX_BUILTIN_ONLY() wrap a parameter only used with the built-in regex
+// WXREGEX_CONVERT_TO_MB  indicates when the regex lib is using chars and
+//                        wxChar is wide, so conversion must be done
+#ifdef __REG_NOFRONT
+#   define WXREGEX_USING_BUILTIN
+#   define WXREGEX_BUILTIN_ONLY(x) ,x
+#else
+#   define WXREGEX_BUILTIN_ONLY(x)
+#   if wxUSE_UNICODE
+#       define WXREGEX_CONVERT_TO_MB
+#   endif
 #endif
 
 // ----------------------------------------------------------------------------
@@ -74,7 +83,8 @@ public:
 
     // RE operations
     bool Compile(const wxString& expr, int flags = 0);
-    bool Matches(const wxRegChar *str, int flags, size_t len) const;
+    bool Matches(const wxRegChar *str, int flags
+                 WXREGEX_BUILTIN_ONLY(size_t len)) const;
     bool GetMatch(size_t *start, size_t *len, size_t index = 0) const;
     size_t GetMatchCount() const;
     int Replace(wxString *pattern, const wxString& replacement,
@@ -205,7 +215,7 @@ bool wxRegExImpl::Compile(const wxString& expr, int flags)
         flagsRE |= REG_NEWLINE;
 
     // compile it
-#ifdef __REG_NOFRONT
+#ifdef WXREGEX_USING_BUILTIN
     bool conv = true;
     int errorcode = wx_re_comp(&m_RegEx, expr, expr.length(), flagsRE);
 #else
@@ -267,7 +277,9 @@ bool wxRegExImpl::Compile(const wxString& expr, int flags)
     return IsValid();
 }
 
-bool wxRegExImpl::Matches(const wxRegChar *str, int flags, size_t len) const
+bool wxRegExImpl::Matches(const wxRegChar *str,
+                          int flags
+                          WXREGEX_BUILTIN_ONLY(size_t len)) const
 {
     wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
 
@@ -289,7 +301,7 @@ bool wxRegExImpl::Matches(const wxRegChar *str, int flags, size_t len) const
     }
 
     // do match it
-#ifdef __REG_NOFRONT
+#ifdef WXREGEX_USING_BUILTIN
     int rc = wx_re_exec(&self->m_RegEx, str, len, NULL, m_nMatches, m_Matches, flagsRE);
 #else
     int rc = str ? regexec(&self->m_RegEx, str, m_nMatches, m_Matches, flagsRE) : REG_BADPAT;
@@ -390,8 +402,8 @@ int wxRegExImpl::Replace(wxString *text,
     // use wxRE_NOTBOL to prevent it from happening
     while ( (!maxMatches || countRepl < maxMatches) &&
             Matches(textstr + matchStart,
-                    countRepl ? wxRE_NOTBOL : 0,
-                    textlen - matchStart) )
+                    countRepl ? wxRE_NOTBOL : 0
+                    WXREGEX_BUILTIN_ONLY(textlen - matchStart)) )
     {
         // the string possibly contains back references: we need to calculate
         // the replacement text anew after each match
@@ -518,14 +530,26 @@ bool wxRegEx::Compile(const wxString& expr, int flags)
     return true;
 }
 
+bool wxRegEx::Matches(const wxChar *str, int flags, size_t len) const
+{
+    wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
+    (void)len;
+
+#ifdef WXREGEX_CONVERT_TO_MB
+    return m_impl->Matches(wxConvertWX2MB(str), flags);
+#else
+    return m_impl->Matches(str, flags WXREGEX_BUILTIN_ONLY(len));
+#endif
+}
+
 bool wxRegEx::Matches(const wxChar *str, int flags) const
 {
     wxCHECK_MSG( IsValid(), false, _T("must successfully Compile() first") );
 
-#ifndef WXREGEX_CONVERT_TO_MB
-    return m_impl->Matches(str, flags, wxStrlen(str));
+#ifdef WXREGEX_CONVERT_TO_MB
+    return m_impl->Matches(wxConvertWX2MB(str), flags);
 #else
-    return m_impl->Matches(wxConvertWX2MB(str), flags, wxStrlen(str));
+    return m_impl->Matches(str, flags WXREGEX_BUILTIN_ONLY(wxStrlen(str)));
 #endif
 }
 
-- 
2.50.0

