From: Vadim Zeitlin Date: Thu, 30 Aug 2007 18:13:29 +0000 (+0000) Subject: fix resize() when it's used for truncating a string in UTF-8 build; added test for... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/fe1b98f5d55ae9d518b59072c38bd73955c2555d fix resize() when it's used for truncating a string in UTF-8 build; added test for this bug git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48464 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/string.h b/include/wx/string.h index 436a6831f4..8178d100d9 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -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); } diff --git a/tests/strings/stdstrings.cpp b/tests/strings/stdstrings.cpp index 0b09e6ffef..585d65c6e4 100644 --- a/tests/strings/stdstrings.cpp +++ b/tests/strings/stdstrings.cpp @@ -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()