implement startsWith and endsWith.

This commit is contained in:
aaron0x 2016-04-18 01:14:13 +08:00
parent d11b69ca00
commit 43d96a5bcb
3 changed files with 71 additions and 0 deletions

View File

@ -623,6 +623,22 @@ S cat(const S& delim, const It& begin, const It& end)
}
template <class S>
bool startsWith(const S& str, const S& prefix)
/// Tests whether the string starts with the given prefix.
{
return equal(prefix.begin(), prefix.end(), str.begin());
}
template <class S>
bool endsWith(const S& str, const S& suffix)
/// Tests whether the string ends with the given suffix.
{
return equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
//
// case-insensitive string equality
//

View File

@ -47,6 +47,8 @@ using Poco::replaceInPlace;
using Poco::remove;
using Poco::removeInPlace;
using Poco::cat;
using Poco::startsWith;
using Poco::endsWith;
using Poco::strToInt;
using Poco::strToFloat;
using Poco::strToDouble;
@ -435,6 +437,55 @@ void StringTest::testCat()
}
void StringTest::testStartsWith()
{
std::string s1("o");
assert (startsWith(s1, std::string("o")));
assert (startsWith(s1, std::string("")));
assert (!startsWith(s1, std::string("O")));
assert (!startsWith(s1, std::string("1")));
std::string s2("");
assert (startsWith(s2, std::string("")));
assert (!startsWith(s2, std::string("o")));
std::string s3("oO");
assert (startsWith(s3, std::string("o")));
assert (!startsWith(s3, std::string(" o")));
}
void StringTest::testEndsWith()
{
std::string s1("o");
assert (endsWith(s1, std::string("o")));
assert (endsWith(s1, std::string("")));
assert (!endsWith(s1, std::string("O")));
assert (!endsWith(s1, std::string("1")));
std::string s2("");
assert (endsWith(s2, std::string("")));
assert (!endsWith(s2, std::string("o")));
std::string s3("Oo");
assert (endsWith(s3, std::string("o")));
assert (!endsWith(s3, std::string("o ")));
}
void StringTest::testStringToInt()
{
//gcc on Mac emits warnings that cannot be suppressed
@ -1187,6 +1238,8 @@ CppUnit::Test* StringTest::suite()
CppUnit_addTest(pSuite, StringTest, testReplace);
CppUnit_addTest(pSuite, StringTest, testReplaceInPlace);
CppUnit_addTest(pSuite, StringTest, testCat);
CppUnit_addTest(pSuite, StringTest, testStartsWith);
CppUnit_addTest(pSuite, StringTest, testEndsWith);
CppUnit_addTest(pSuite, StringTest, testStringToInt);
CppUnit_addTest(pSuite, StringTest, testStringToFloat);
CppUnit_addTest(pSuite, StringTest, testStringToDouble);

View File

@ -44,6 +44,8 @@ public:
void testReplace();
void testReplaceInPlace();
void testCat();
void testStartsWith();
void testEndsWith();
void testStringToInt();
void testStringToFloat();