]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
Fix double-click support for wxListBox (#10548)
[wxWidgets.git] / src / common / string.cpp
index 56af51ae22c28dbe3e8306171f575729270236ac..e07b7eea8f74554e9350b0a402b5977528296c34 100644 (file)
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 
 #include "wx/hashmap.h"
+#include "wx/vector.h"
 
 // string handling functions used by wxString:
 #if wxUSE_UNICODE_UTF8
@@ -1397,30 +1398,62 @@ size_t wxString::Replace(const wxString& strOld,
                 break;
         }
     }
-    else // general case
+    else if ( !bReplaceAll)
+    {
+        size_t pos = m_impl.find(strOld, 0);
+        if ( pos != npos )
+        {
+            m_impl.replace(pos, strOld.m_impl.length(), strNew.m_impl);
+            uiCount = 1;
+        }
+    }
+    else // replace all occurrences
     {
         const size_t uiOldLen = strOld.m_impl.length();
         const size_t uiNewLen = strNew.m_impl.length();
 
-        for ( size_t pos = 0; ; )
+        // first scan the string to find all positions at which the replacement
+        // should be made
+        wxVector<size_t> replacePositions;
+
+        size_t pos;
+        for ( pos = m_impl.find(strOld.m_impl, 0);
+              pos != npos;
+              pos = m_impl.find(strOld.m_impl, pos + uiOldLen))
         {
-            pos = m_impl.find(strOld.m_impl, pos);
-            if ( pos == npos )
-                break;
+            replacePositions.push_back(pos);
+            ++uiCount;
+        }
 
-            // replace this occurrence of the old string with the new one
-            m_impl.replace(pos, uiOldLen, strNew.m_impl);
+        if ( !uiCount )
+            return 0;
 
-            // move up pos past the string that was replaced
-            pos += uiNewLen;
+        // allocate enough memory for the whole new string
+        wxString tmp;
+        tmp.m_impl.reserve(m_impl.length() + uiCount*(uiNewLen - uiOldLen));
 
-            // increase replace count
-            uiCount++;
+        // copy this string to tmp doing replacements on the fly
+        size_t replNum = 0;
+        for ( pos = 0; replNum < uiCount; replNum++ )
+        {
+            const size_t nextReplPos = replacePositions[replNum];
 
-            // stop after the first one?
-            if ( !bReplaceAll )
-                break;
+            if ( pos != nextReplPos )
+            {
+                tmp.m_impl.append(m_impl, pos, nextReplPos - pos);
+            }
+
+            tmp.m_impl.append(strNew.m_impl);
+            pos = nextReplPos + uiOldLen;
         }
+
+        if ( pos != m_impl.length() )
+        {
+            // append the rest of the string unchanged
+            tmp.m_impl.append(m_impl, pos, m_impl.length() - pos);
+        }
+
+        swap(tmp);
     }
 
     return uiCount;