]> git.saurik.com Git - wxWidgets.git/commitdiff
Don't cache incorrect length in wxString::assign(char*, size_t).
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 23 Mar 2012 15:13:13 +0000 (15:13 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 23 Mar 2012 15:13:13 +0000 (15:13 +0000)
The length of the string here is specified in bytes and is not the same as the
string length for non-ASCII UTF-8 strings, so don't cache it as the string
length.

Just invalidate the cached length instead as we simply don't know what the
real length of the string is going to be.

See #14130.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70987 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

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

index 4591e1b07516e11265d23a9c8422aad45e85d25e..03c21a7acf5cf6c3f873ef01bfbfe449e31d97a5 100644 (file)
@@ -2669,7 +2669,7 @@ public:
 
   wxString& assign(const char *sz, size_t n)
   {
-      wxSTRING_SET_CACHED_LENGTH(n);
+      wxSTRING_INVALIDATE_CACHE();
 
       SubstrBufFromMB str(ImplStr(sz, n));
       m_impl.assign(str.data, str.len);
index 879fab790769646b46ecbd9718b707d4a8f59223..13904976aceb053bc345dac94dacb5238e88ef6d 100644 (file)
@@ -192,6 +192,12 @@ void StringTestCase::Constructors()
 
     CPPUNIT_ASSERT_EQUAL( 0, wxString(wxString(), 17).length() );
 
+    // This string has 3 characters (<h>, <e'> and <l>), not 4!
+    wxString s3("h\xc3\xa9llo", 4);
+    CPPUNIT_ASSERT_EQUAL( 3, s3.length() );
+    CPPUNIT_ASSERT_EQUAL( 'l', (char)s3[2] );
+
+
     static const char *s = "?really!";
     const char *start = wxStrchr(s, 'r');
     const char *end = wxStrchr(s, '!');
@@ -219,6 +225,9 @@ void StringTestCase::StaticConstructors()
     CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
     CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
 
+    CPPUNIT_ASSERT_EQUAL( 2, wxString::FromUTF8("h\xc3\xa9llo", 3).length() );
+
+
     //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
 }