From: Thomas Geymayer Date: Mon, 15 Jul 2013 20:26:11 +0000 (+0200) Subject: nasal::String: add ends_with method X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=dde2bbcbe7290b5f8a14140c61166f8fc6aecb0c;p=simgear.git nasal::String: add ends_with method --- diff --git a/simgear/nasal/cppbind/NasalString.cxx b/simgear/nasal/cppbind/NasalString.cxx index 41388b3d..dd3cb5ab 100644 --- a/simgear/nasal/cppbind/NasalString.cxx +++ b/simgear/nasal/cppbind/NasalString.cxx @@ -136,6 +136,12 @@ namespace nasal return rhs.size() <= size() && compare(0, npos, rhs) == 0; } + //---------------------------------------------------------------------------- + bool String::ends_with(const String& rhs) const + { + return rhs.size() <= size() && compare(size() - rhs.size(), npos, rhs) == 0; + } + //---------------------------------------------------------------------------- size_t String::find(const char c, size_t pos) const { diff --git a/simgear/nasal/cppbind/NasalString.hxx b/simgear/nasal/cppbind/NasalString.hxx index d30dfebd..ad9e506f 100644 --- a/simgear/nasal/cppbind/NasalString.hxx +++ b/simgear/nasal/cppbind/NasalString.hxx @@ -59,6 +59,7 @@ namespace nasal int compare(size_t pos, size_t len, const String& rhs) const; bool starts_with(const String& rhs) const; + bool ends_with(const String& rhs) const; size_t find(const char c, size_t pos = 0) const; size_t find_first_of(const String& chr, size_t pos = 0) const; diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index 29cb556a..6d75b966 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -286,6 +286,13 @@ int main(int argc, char* argv[]) VERIFY( !string.starts_with(String(c, "Test1")) ); VERIFY( !string.starts_with(String(c, "bb")) ); VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) ); + VERIFY( string.ends_with(String(c, "t")) ); + VERIFY( string.ends_with(String(c, "st")) ); + VERIFY( string.ends_with(String(c, "est")) ); + VERIFY( string.ends_with(String(c, "Test")) ); + VERIFY( !string.ends_with(String(c, "1Test")) ); + VERIFY( !string.ends_with(String(c, "abc")) ); + VERIFY( !string.ends_with(String(c, "estasdasd")) ); VERIFY( string.find('e') == 1 ); VERIFY( string.find('9') == String::npos ); VERIFY( string.find_first_of(String(c, "st")) == 2 );