Added "rest" argument to wxString::Before{First,Last}().

This allows to search the string just once, in BeforeXXX(), when both the
parts of the string before and after some character are needed instead of
having to do it twice in both BeforeXXX() and AfterXXX().

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65862 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2010-10-22 14:17:37 +00:00
parent f48a115976
commit 6becc1e617
5 changed files with 77 additions and 17 deletions

View File

@ -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).

View File

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

View File

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

View File

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

View File

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