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
This commit is contained in:
Vadim Zeitlin 2008-09-29 12:08:44 +00:00
parent b74134f61d
commit c565abe1c5
2 changed files with 27 additions and 3 deletions

View File

@ -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;
}

View File

@ -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(';') );
}