]> git.saurik.com Git - wxWidgets.git/commitdiff
fix resize() when it's used for truncating a string in UTF-8 build; added test for...
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 30 Aug 2007 18:13:29 +0000 (18:13 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 30 Aug 2007 18:13:29 +0000 (18:13 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48464 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/string.h
tests/strings/stdstrings.cpp

index 436a6831f4544ee00cf9a47362a0811afb060c33..8178d100d976309036b60a19a044075eaf1602c3 100644 (file)
@@ -1087,19 +1087,25 @@ public:
 
   void resize(size_t nSize, wxUniChar ch = wxT('\0'))
   {
+    const size_t len = length();
+    if ( nSize == len)
+        return;
+
 #if wxUSE_UNICODE_UTF8
-    if ( !ch.IsAscii() )
+    if ( nSize < len )
     {
-        size_t len = length();
-        if ( nSize == len)
-            return;
-        else if ( nSize < len )
-            erase(nSize);
-        else
-            append(nSize - len, ch);
+        // we can't use wxStringImpl::resize() for truncating the string as it
+        // counts in bytes, not characters
+        erase(nSize);
+        return;
     }
+
+    // we also can't use (presumably more efficient) resize() if we have to
+    // append characters taking more than one byte
+    if ( !ch.IsAscii() )
+        append(nSize - len, ch);
     else
-#endif
+#endif // wxUSE_UNICODE_UTF8
         m_impl.resize(nSize, (wxStringCharType)ch);
   }
 
index 0b09e6ffef63a9d09afa5f920ac3e84e38c22da2..585d65c6e40742a10384d72925fe5786742968ed 100644 (file)
@@ -488,6 +488,11 @@ void StdStringTestCase::StdResize()
     CPPUNIT_ASSERT( s2 == _T("abcABCdefD") );
     CPPUNIT_ASSERT( s3 == _T("abcABCdefDEF  ") );
     CPPUNIT_ASSERT( s4 == _T("abcABCdefDEFWW") );
+
+    wxString s =
+        wxString::FromUTF8("\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
+    s.resize(3);
+    WX_ASSERT_STR_EQUAL("\xd0\x9f\xd1\x80\xd0\xb8", s);
 }
 
 void StdStringTestCase::StdRiter()