diff --git a/include/wx/string.h b/include/wx/string.h index e6f6931300..6aa8d4d323 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -3255,6 +3255,20 @@ public: 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); } + 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 wxString& operator+=(const wxString& s) { diff --git a/interface/wx/string.h b/interface/wx/string.h index 65d828c8f5..9cecd3ff16 100644 --- a/interface/wx/string.h +++ b/interface/wx/string.h @@ -1711,6 +1711,14 @@ public: wxString substr(size_t nStart = 0, size_t nLen = npos) const; 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; + //@} diff --git a/tests/strings/stdstrings.cpp b/tests/strings/stdstrings.cpp index 71cafa3632..f67722590c 100644 --- a/tests/strings/stdstrings.cpp +++ b/tests/strings/stdstrings.cpp @@ -43,6 +43,7 @@ private: CPPUNIT_TEST( StdFind ); CPPUNIT_TEST( StdFindFirst ); CPPUNIT_TEST( StdFindLast ); + CPPUNIT_TEST( StdStartsEndsWith ); CPPUNIT_TEST( StdInsert ); CPPUNIT_TEST( StdReplace ); CPPUNIT_TEST( StdRFind ); @@ -65,6 +66,7 @@ private: void StdFind(); void StdFindFirst(); void StdFindLast(); + void StdStartsEndsWith(); void StdInsert(); void StdReplace(); void StdRFind(); @@ -396,6 +398,28 @@ void StdStringTestCase::StdFindLast() 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() { wxString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;