diff --git a/docs/changes.txt b/docs/changes.txt index dff8589114..4cb400ad3e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -403,6 +403,10 @@ Major new features in this release 2.9.2: ------ +All: + +- Added "rest" argument to wxString::Before{First,Last}(). + All (GUI): - Added wxRichMessageDialog (Rickard Westerlund, GSoC 2010 project). diff --git a/include/wx/string.h b/include/wx/string.h index 32eb250aa9..cb8d6bccfe 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -2210,11 +2210,13 @@ public: // get last nCount characters wxString Right(size_t nCount) const; // get all characters before the first occurrence of ch - // (returns the whole string if ch not found) - wxString BeforeFirst(wxUniChar ch) const; + // (returns the whole string if ch not found) and also put everything + // following the first occurrence of ch into rest if it's non-NULL + wxString BeforeFirst(wxUniChar ch, wxString *rest = NULL) const; // get all characters before the last occurrence of ch - // (returns empty string if ch not found) - wxString BeforeLast(wxUniChar ch) const; + // (returns empty string if ch not found) and also put everything + // following the last occurrence of ch into rest if it's non-NULL + wxString BeforeLast(wxUniChar ch, wxString *rest = NULL) const; // get all characters after the first occurrence of ch // (returns empty string if ch not found) wxString AfterFirst(wxUniChar ch) const; diff --git a/interface/wx/string.h b/interface/wx/string.h index 35f17ba7d3..98ed23b861 100644 --- a/interface/wx/string.h +++ b/interface/wx/string.h @@ -783,14 +783,32 @@ public: /** Gets all characters before the first occurrence of @e ch. Returns the whole string if @a ch is not found. + + @param ch The character to look for. + @param rest Filled with the part of the string following the first + occurrence of @a ch or cleared if it was not found. The same string + is returned by AfterFirst() but it is more efficient to use this + output parameter if both the "before" and "after" parts are needed + than calling both functions one after the other. This parameter is + available in wxWidgets version 2.9.2 and later only. + @return Part of the string before the first occurrence of @a ch. */ - wxString BeforeFirst(wxUniChar ch) const; + wxString BeforeFirst(wxUniChar ch, wxString *rest = NULL) const; /** Gets all characters before the last occurrence of @e ch. Returns the empty string if @a ch is not found. + + @param ch The character to look for. + @param rest Filled with the part of the string following the last + occurrence of @a ch or the copy of this string if it was not found. + The same string is returned by AfterLast() but it is more efficient + to use this output parameter if both the "before" and "after" parts + are needed than calling both functions one after the other. This + parameter is available in wxWidgets version 2.9.2 and later only. + @return Part of the string before the last occurrence of @a ch. */ - wxString BeforeLast(wxUniChar ch) const; + wxString BeforeLast(wxUniChar ch, wxString *rest = NULL) const; //@} diff --git a/src/common/string.cpp b/src/common/string.cpp index 54a7281a58..e5db843659 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1349,22 +1349,43 @@ wxString wxString::Left(size_t nCount) const // get all characters before the first occurrence of ch // (returns the whole string if ch not found) -wxString wxString::BeforeFirst(wxUniChar ch) const +wxString wxString::BeforeFirst(wxUniChar ch, wxString *rest) const { int iPos = Find(ch); if ( iPos == wxNOT_FOUND ) - iPos = length(); + { + iPos = length(); + if ( rest ) + rest->clear(); + } + else + { + if ( rest ) + rest->assign(*this, iPos + 1, npos); + } + return wxString(*this, 0, iPos); } /// get all characters before the last occurrence of ch /// (returns empty string if ch not found) -wxString wxString::BeforeLast(wxUniChar ch) const +wxString wxString::BeforeLast(wxUniChar ch, wxString *rest) const { wxString str; int iPos = Find(ch, true); - if ( iPos != wxNOT_FOUND && iPos != 0 ) - str = wxString(c_str(), iPos); + if ( iPos != wxNOT_FOUND ) + { + if ( iPos != 0 ) + str.assign(*this, 0, iPos); + + if ( rest ) + rest->assign(*this, iPos + 1, npos); + } + else + { + if ( rest ) + *rest = *this; + } return str; } diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index cea9fed389..4d7664115e 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -970,18 +970,33 @@ void StringTestCase::BeforeAndAfter() { const wxString s(L"letter=\xe9;\xe7a=l\xe0"); - CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=') ); - CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!') ); - CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeFirst(';') ); + wxString r; + + CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=', &r) ); + CPPUNIT_ASSERT_EQUAL( L"\xe9;\xe7a=l\xe0", r ); + + CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!', &r) ); + CPPUNIT_ASSERT_EQUAL( "", r ); + + CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeFirst(';', &r) ); + CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", r ); + + + CPPUNIT_ASSERT_EQUAL( L"letter=\xe9;\xe7a", s.BeforeLast('=', &r) ); + CPPUNIT_ASSERT_EQUAL( L"l\xe0", r ); + + CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!', &r) ); + CPPUNIT_ASSERT_EQUAL( s, r ); + + CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeLast(';', &r) ); + CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", r ); - CPPUNIT_ASSERT_EQUAL( L"letter=\xe9;\xe7a", s.BeforeLast('=') ); - CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!') ); - CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeLast(';') ); CPPUNIT_ASSERT_EQUAL( L"\xe9;\xe7a=l\xe0", s.AfterFirst('=') ); CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') ); CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterFirst(';') ); + CPPUNIT_ASSERT_EQUAL( L"l\xe0", s.AfterLast('=') ); CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') ); CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterLast(';') );