Add C++20-style starts_with()/ends_with() functions to wxString

C++20 introduces these two functions, along with some overloads, to
std::string. Add similar functions to wxString, which simply call the
already existing StartsWith() and EndsWith().

Closes https://github.com/wxWidgets/wxWidgets/pull/1452
This commit is contained in:
Lauri Nurmi
2019-07-30 10:01:24 +03:00
committed by Vadim Zeitlin
parent a8c4cbee09
commit 4739b9dedd
3 changed files with 46 additions and 0 deletions

View File

@@ -3255,6 +3255,20 @@ public:
size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
{ return find_last_not_of(sz.data(), nStart, n); } { return find_last_not_of(sz.data(), nStart, n); }
bool starts_with(const wxString &str) const
{ return StartsWith(str); }
bool starts_with(const char *sz) const
{ return StartsWith(sz); }
bool starts_with(const wchar_t *sz) const
{ return StartsWith(sz); }
bool ends_with(const wxString &str) const
{ return EndsWith(str); }
bool ends_with(const char *sz) const
{ return EndsWith(sz); }
bool ends_with(const wchar_t *sz) const
{ return EndsWith(sz); }
// string += string // string += string
wxString& operator+=(const wxString& s) wxString& operator+=(const wxString& s)
{ {

View File

@@ -1711,6 +1711,14 @@ public:
wxString substr(size_t nStart = 0, size_t nLen = npos) const; wxString substr(size_t nStart = 0, size_t nLen = npos) const;
void swap(wxString& str); void swap(wxString& str);
bool starts_with(const wxString &str) const;
bool starts_with(const char *sz) const;
bool starts_with(const wchar_t *sz) const;
bool ends_with(const wxString &str) const;
bool ends_with(const char *sz) const;
bool ends_with(const wchar_t *sz) const;
//@} //@}

View File

@@ -43,6 +43,7 @@ private:
CPPUNIT_TEST( StdFind ); CPPUNIT_TEST( StdFind );
CPPUNIT_TEST( StdFindFirst ); CPPUNIT_TEST( StdFindFirst );
CPPUNIT_TEST( StdFindLast ); CPPUNIT_TEST( StdFindLast );
CPPUNIT_TEST( StdStartsEndsWith );
CPPUNIT_TEST( StdInsert ); CPPUNIT_TEST( StdInsert );
CPPUNIT_TEST( StdReplace ); CPPUNIT_TEST( StdReplace );
CPPUNIT_TEST( StdRFind ); CPPUNIT_TEST( StdRFind );
@@ -65,6 +66,7 @@ private:
void StdFind(); void StdFind();
void StdFindFirst(); void StdFindFirst();
void StdFindLast(); void StdFindLast();
void StdStartsEndsWith();
void StdInsert(); void StdInsert();
void StdReplace(); void StdReplace();
void StdRFind(); void StdRFind();
@@ -396,6 +398,28 @@ void StdStringTestCase::StdFindLast()
CPPUNIT_ASSERT( s1.find_last_of(wxT("a"), 18) == 18u ); CPPUNIT_ASSERT( s1.find_last_of(wxT("a"), 18) == 18u );
} }
void StdStringTestCase::StdStartsEndsWith()
{
const wxString s(wxT("Hello, world!"));
CPPUNIT_ASSERT_EQUAL( true, s.starts_with(wxT("Hello")) );
CPPUNIT_ASSERT_EQUAL( true, s.starts_with(wxT("Hello, ")) );
CPPUNIT_ASSERT_EQUAL( true, s.starts_with(wxT("Hello, world!")) );
CPPUNIT_ASSERT_EQUAL( false, s.starts_with(wxT("Hello, world!!!")) );
CPPUNIT_ASSERT_EQUAL( true, s.starts_with(wxT("")) );
CPPUNIT_ASSERT_EQUAL( false, s.starts_with(wxT("Goodbye")) );
CPPUNIT_ASSERT_EQUAL( false, s.starts_with(wxT("Hi")) );
CPPUNIT_ASSERT_EQUAL( true, s.ends_with(wxT("Hello, world!")) );
CPPUNIT_ASSERT_EQUAL( true, s.ends_with(wxT("world!")) );
CPPUNIT_ASSERT_EQUAL( false, s.ends_with(wxT("Hello")) );
CPPUNIT_ASSERT_EQUAL( true, s.ends_with(wxT("!")) );
CPPUNIT_ASSERT_EQUAL( true, s.ends_with(wxT("")) );
CPPUNIT_ASSERT_EQUAL( false, s.ends_with(wxT("very long string")) );
CPPUNIT_ASSERT_EQUAL( false, s.ends_with(wxT("?")) );
CPPUNIT_ASSERT_EQUAL( false, s.ends_with(wxT("Hello, world")) );
CPPUNIT_ASSERT_EQUAL( false, s.ends_with(wxT("Gello, world!")) );
}
void StdStringTestCase::StdInsert() void StdStringTestCase::StdInsert()
{ {
wxString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10; wxString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;