]> git.saurik.com Git - wxWidgets.git/commitdiff
made wxString::Replace, Matches and Find work with any form of string argument
authorVáclav Slavík <vslavik@fastmail.fm>
Thu, 5 Apr 2007 08:35:39 +0000 (08:35 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Thu, 5 Apr 2007 08:35:39 +0000 (08:35 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45249 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/latex/wx/wxstring.tex
include/wx/string.h
src/common/string.cpp

index 60bbd04123aa89bd8979037960e0e0b750e637e6..445d0058f76da121cf7ee5235799b5c4d1b611f2 100644 (file)
@@ -673,11 +673,11 @@ See also: \helpref{Clear()}{wxstringclear}.
 
 \membersection{wxString::Find}\label{wxstringfind}
 
-\constfunc{int}{Find}{\param{wxChar}{ ch}, \param{bool}{ fromEnd = false}}
+\constfunc{int}{Find}{\param{wxUniChar}{ ch}, \param{bool}{ fromEnd = false}}
 
 Searches for the given character. Returns the starting index, or {\tt wxNOT\_FOUND} if not found.
 
-\constfunc{int}{Find}{\param{const wxChar*}{ sz}}
+\constfunc{int}{Find}{\param{const wxString\&}{ sub}}
 
 Searches for the given string. Returns the starting index, or {\tt wxNOT\_FOUND} if not found.
 
@@ -942,7 +942,7 @@ Converts all characters to upper case and returns the result.
 
 \membersection{wxString::Matches}\label{wxstringmatches}
 
-\constfunc{bool}{Matches}{\param{const wxChar*}{ szMask}}
+\constfunc{bool}{Matches}{\param{const wxString\&}{ mask}}
 
 Returns \true if the string contents matches a mask containing '*' and '?'.
 
@@ -1046,7 +1046,7 @@ Removes the last character.
 
 \membersection{wxString::Replace}\label{wxstringreplace}
 
-\func{size\_t}{Replace}{\param{const wxChar*}{ szOld}, \param{const wxChar*}{ szNew}, \param{bool}{ replaceAll = true}}
+\func{size\_t}{Replace}{\param{const wxString\&}{ strOld}, \param{const wxString\&}{ strNew}, \param{bool}{ replaceAll = true}}
 
 Replace first (or all) occurrences of substring with another one.
 
index 5ec271239ac2471c6d97d9dfb361be4722f631c8..0b4762344fdc8d22657510cd859ed455e10e152e 100644 (file)
@@ -1241,16 +1241,40 @@ public:
   // searching and replacing
       // searching (return starting index, or -1 if not found)
   int Find(wxUniChar ch, bool bFromEnd = false) const;   // like strchr/strrchr
+  int Find(wxUniCharRef ch, bool bFromEnd = false) const
+    { return Find(wxUniChar(ch), bFromEnd); }
+  int Find(char ch, bool bFromEnd = false) const
+    { return Find(wxUniChar(ch), bFromEnd); }
+  int Find(unsigned char ch, bool bFromEnd = false) const
+    { return Find(wxUniChar(ch), bFromEnd); }
+  int Find(wchar_t ch, bool bFromEnd = false) const
+    { return Find(wxUniChar(ch), bFromEnd); }
       // searching (return starting index, or -1 if not found)
-  int Find(const wxChar *pszSub) const;               // like strstr
+  // FIXME-UTF8: keep wxString overload only
+  int Find(const wxString& sub) const               // like strstr
+  {
+    size_type idx = find(sub);
+    return (idx == npos) ? wxNOT_FOUND : (int)idx;
+  }
+  int Find(const char *sub) const               // like strstr
+  {
+    size_type idx = find(sub);
+    return (idx == npos) ? wxNOT_FOUND : (int)idx;
+  }
+  int Find(const wchar_t *sub) const               // like strstr
+  {
+    size_type idx = find(sub);
+    return (idx == npos) ? wxNOT_FOUND : (int)idx;
+  }
+
       // replace first (or all of bReplaceAll) occurences of substring with
       // another string, returns the number of replacements made
-  size_t Replace(const wxChar *szOld,
-                 const wxChar *szNew,
+  size_t Replace(const wxString& strOld,
+                 const wxString& strNew,
                  bool bReplaceAll = true);
 
     // check if the string contents matches a mask containing '*' and '?'
-  bool Matches(const wxChar *szMask) const;
+  bool Matches(const wxString& mask) const;
 
     // conversion to numbers: all functions return true only if the whole
     // string is a number and put the value of this number into the pointer
@@ -1351,11 +1375,12 @@ public:
 
     // use Find()
   int First( wxUniChar ch ) const { return Find(ch); }
+  int First( wxUniCharRef ch ) const { return Find(ch); }
   int First( char ch ) const { return Find(ch); }
   int First( unsigned char ch ) const { return Find(ch); }
   int First( wchar_t ch ) const { return Find(ch); }
   int First( const wxChar* psz ) const { return Find(psz); }
-  int First( const wxString &str ) const { return Find(str); }
+  int First( const wxStringstr ) const { return Find(str); }
   int Last( wxUniChar ch ) const { return Find(ch, true); }
   bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
 
index ae7d1af85cbcf371256f63083134664e4d3f1d20..14e90941a342b83ac743331295a0bb953155d947 100644 (file)
@@ -941,32 +941,32 @@ wxString wxString::AfterFirst(wxUniChar ch) const
 }
 
 // replace first (or all) occurences of some substring with another one
-size_t wxString::Replace(const wxChar *szOld,
-                  const wxChar *szNew, bool bReplaceAll)
+size_t wxString::Replace(const wxString& strOld,
+                         const wxString& strNew, bool bReplaceAll)
 {
     // if we tried to replace an empty string we'd enter an infinite loop below
-    wxCHECK_MSG( szOld && *szOld && szNew, 0,
+    wxCHECK_MSG( !strOld.empty(), 0,
                  _T("wxString::Replace(): invalid parameter") );
 
     size_t uiCount = 0;   // count of replacements made
 
-    size_t uiOldLen = wxStrlen(szOld);
-    size_t uiNewLen = wxStrlen(szNew);
+    size_t uiOldLen = strOld.length();
+    size_t uiNewLen = strNew.length();
 
     size_t dwPos = 0;
 
-    while ( this->c_str()[dwPos] != wxT('\0') )
+    while ( (*this)[dwPos] != wxT('\0') )
     {
         //DO NOT USE STRSTR HERE
         //this string can contain embedded null characters,
         //so strstr will function incorrectly
-        dwPos = find(szOld, dwPos);
+        dwPos = find(strOld, dwPos);
         if ( dwPos == npos )
             break;                  // exit the loop
         else
         {
             //replace this occurance of the old string with the new one
-            replace(dwPos, uiOldLen, szNew, uiNewLen);
+            replace(dwPos, uiOldLen, strNew, uiNewLen);
 
             //move up pos past the string that was replaced
             dwPos += uiNewLen;
@@ -1131,14 +1131,6 @@ int wxString::Find(wxUniChar ch, bool bFromEnd) const
     return (idx == npos) ? wxNOT_FOUND : (int)idx;
 }
 
-// find a sub-string (like strstr)
-int wxString::Find(const wxChar *pszSub) const
-{
-    size_type idx = find(pszSub);
-
-    return (idx == npos) ? wxNOT_FOUND : (int)idx;
-}
-
 // ----------------------------------------------------------------------------
 // conversion to numbers
 // ----------------------------------------------------------------------------
@@ -1356,7 +1348,7 @@ int wxString::PrintfV(const wxString& format, va_list argptr)
 // returns true if the string matches the pattern which may contain '*' and
 // '?' metacharacters (as usual, '?' matches any character and '*' any number
 // of them)
-bool wxString::Matches(const wxChar *pszMask) const
+bool wxString::Matches(const wxString& mask) const
 {
     // I disable this code as it doesn't seem to be faster (in fact, it seems
     // to be much slower) than the old, hand-written code below and using it
@@ -1407,8 +1399,17 @@ bool wxString::Matches(const wxChar *pszMask) const
 #else // !wxUSE_REGEX
   // TODO: this is, of course, awfully inefficient...
 
+  // FIXME-UTF8: implement using iterators, remove #if
+#if wxUSE_UNICODE_UTF8
+  wxWCharBuffer maskBuf = mask.wc_str();
+  wxWCharBuffer txtBuf = wc_str();
+  const wxChar *pszMask = maskBuf.data();
+  const wxChar *pszTxt = txtBuf.data();
+#else
+  const wxChar *pszMask = mask.wx_str();
   // the char currently being checked
-  const wxChar *pszTxt = c_str();
+  const wxChar *pszTxt = wx_str();
+#endif
 
   // the last location where '*' matched
   const wxChar *pszLastStarInText = NULL;