]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
floating pane now respects IsFixed pane status; indirect fix for #4044
[wxWidgets.git] / src / common / string.cpp
index f413b031fd79c9c1b92e58e28794f5d40a26fe38..362d682fb2eef1bba8264c871cf7a1b5b57c1d8c 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
-#ifdef __SALFORDC__
-    #include <clib.h>
-#endif
-
 #include "wx/hashmap.h"
 
 // string handling functions used by wxString:
@@ -373,6 +369,9 @@ wxString::SubstrBufFromMB wxString::ConvertStr(const char *psz, size_t nLength,
         // UTF-8 sequence and psz may be invalid:
         if ( wxStringOperations::IsValidUtf8String(psz, nLength) )
         {
+            // we must pass the real string length to SubstrBufFromMB ctor
+            if ( nLength == npos )
+                nLength = psz ? strlen(psz) : 0;
             return SubstrBufFromMB(wxCharBuffer::CreateNonOwned(psz), nLength);
         }
         // else: do the roundtrip through wchar_t*
@@ -1240,33 +1239,55 @@ size_t wxString::Replace(const wxString& strOld,
 
     size_t uiCount = 0;   // count of replacements made
 
-    size_t uiOldLen = strOld.length();
-    size_t uiNewLen = strNew.length();
+    // optimize the special common case: replacement of one character by
+    // another one (in UTF-8 case we can only do this for ASCII characters)
+    //
+    // benchmarks show that this special version is around 3 times faster
+    // (depending on the proportion of matching characters and UTF-8/wchar_t
+    // build)
+    if ( strOld.m_impl.length() == 1 && strNew.m_impl.length() == 1 )
+    {
+        const wxStringCharType chOld = strOld.m_impl[0],
+                               chNew = strNew.m_impl[0];
 
-    size_t dwPos = 0;
+        // this loop is the simplified version of the one below
+        for ( size_t pos = 0; ; )
+        {
+            pos = m_impl.find(chOld, pos);
+            if ( pos == npos )
+                break;
+
+            m_impl[pos++] = chNew;
 
-    while ( (*this)[dwPos] != wxT('\0') )
+            uiCount++;
+
+            if ( !bReplaceAll )
+                break;
+        }
+    }
+    else // general case
     {
-        //DO NOT USE STRSTR HERE
-        //this string can contain embedded null characters,
-        //so strstr will function incorrectly
-        dwPos = find(strOld, dwPos);
-        if ( dwPos == npos )
-            break;                  // exit the loop
-        else
+        const size_t uiOldLen = strOld.m_impl.length();
+        const size_t uiNewLen = strNew.m_impl.length();
+
+        for ( size_t pos = 0; ; )
         {
-            //replace this occurance of the old string with the new one
-            replace(dwPos, uiOldLen, strNew, uiNewLen);
+            pos = m_impl.find(strOld.m_impl, pos);
+            if ( pos == npos )
+                break;
 
-            //move up pos past the string that was replaced
-            dwPos += uiNewLen;
+            // replace this occurrence of the old string with the new one
+            m_impl.replace(pos, uiOldLen, strNew.m_impl);
 
-            //increase replace count
-            ++uiCount;
+            // move up pos past the string that was replaced
+            pos += uiNewLen;
 
-            // stop now?
+            // increase replace count
+            uiCount++;
+
+            // stop after the first one?
             if ( !bReplaceAll )
-                break;                  // exit the loop
+                break;
         }
     }