From c565abe1c595587ff98fcc6c29537883df0defa4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 29 Sep 2008 12:08:44 +0000 Subject: [PATCH] fix After{First,Last}() to work for strings with non-ASCII characters in UTF-8 build and added unit tests for this git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55944 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/string.cpp | 7 ++++--- tests/strings/strings.cpp | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/common/string.cpp b/src/common/string.cpp index 595caf7125..573e5073c2 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1285,7 +1285,7 @@ wxString wxString::AfterLast(wxUniChar ch) const if ( iPos == wxNOT_FOUND ) str = *this; else - str = wx_str() + iPos + 1; + str.assign(*this, iPos + 1, npos); return str; } @@ -1308,7 +1308,8 @@ wxString wxString::Left(size_t nCount) const wxString wxString::BeforeFirst(wxUniChar ch) const { int iPos = Find(ch); - if ( iPos == wxNOT_FOUND ) iPos = length(); + if ( iPos == wxNOT_FOUND ) + iPos = length(); return wxString(*this, 0, iPos); } @@ -1331,7 +1332,7 @@ wxString wxString::AfterFirst(wxUniChar ch) const wxString str; int iPos = Find(ch); if ( iPos != wxNOT_FOUND ) - str = wx_str() + iPos + 1; + str.assign(*this, iPos + 1, npos); return str; } diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index a3fec56598..3542704e49 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -60,6 +60,7 @@ private: CPPUNIT_TEST( CStrDataImplicitConversion ); CPPUNIT_TEST( ExplicitConversion ); CPPUNIT_TEST( IndexedAccess ); + CPPUNIT_TEST( BeforeAndAfter ); CPPUNIT_TEST_SUITE_END(); void String(); @@ -91,6 +92,7 @@ private: void CStrDataImplicitConversion(); void ExplicitConversion(); void IndexedAccess(); + void BeforeAndAfter(); DECLARE_NO_COPY_CLASS(StringTestCase) }; @@ -841,3 +843,24 @@ void StringTestCase::IndexedAccess() CPPUNIT_ASSERT_EQUAL( 'r', s[2] ); } +void StringTestCase::BeforeAndAfter() +{ + const wxString s(L"letter=\u00e9;\u00e7a=l\u00e0"); + + CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=') ); + CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!') ); + CPPUNIT_ASSERT_EQUAL( L"letter=\u00e9", s.BeforeFirst(';') ); + + CPPUNIT_ASSERT_EQUAL( L"letter=\u00e9;\u00e7a", s.BeforeLast('=') ); + CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!') ); + CPPUNIT_ASSERT_EQUAL( L"letter=\u00e9", s.BeforeLast(';') ); + + CPPUNIT_ASSERT_EQUAL( L"\u00e9;\u00e7a=l\u00e0", s.AfterFirst('=') ); + CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') ); + CPPUNIT_ASSERT_EQUAL( L"\u00e7a=l\u00e0", s.AfterFirst(';') ); + + CPPUNIT_ASSERT_EQUAL( L"l\u00e0", s.AfterLast('=') ); + CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') ); + CPPUNIT_ASSERT_EQUAL( L"\u00e7a=l\u00e0", s.AfterLast(';') ); +} +