Added some new method variants to Common::String

svn-id: r41333
This commit is contained in:
Max Horn 2009-06-07 13:04:03 +00:00
parent 985bc454b2
commit a39048877a
3 changed files with 19 additions and 0 deletions

View file

@ -294,6 +294,10 @@ String &String::operator +=(char c) {
return *this;
}
bool String::hasPrefix(const String &x) const {
return hasPrefix(x.c_str());
}
bool String::hasPrefix(const char *x) const {
assert(x != 0);
// Compare x with the start of _str.
@ -307,6 +311,10 @@ bool String::hasPrefix(const char *x) const {
return *x == 0;
}
bool String::hasSuffix(const String &x) const {
return hasSuffix(x.c_str());
}
bool String::hasSuffix(const char *x) const {
assert(x != 0);
// Compare x with the end of _str.
@ -323,6 +331,10 @@ bool String::hasSuffix(const char *x) const {
return *x == 0;
}
bool String::contains(const String &x) const {
return strstr(c_str(), x.c_str()) != NULL;
}
bool String::contains(const char *x) const {
assert(x != 0);
return strstr(c_str(), x) != NULL;

View file

@ -143,9 +143,13 @@ public:
int compareTo(const char *x) const; // strcmp clone
int compareToIgnoreCase(const char *x) const; // stricmp clone
bool hasSuffix(const String &x) const;
bool hasSuffix(const char *x) const;
bool hasPrefix(const String &x) const;
bool hasPrefix(const char *x) const;
bool contains(const String &x) const;
bool contains(const char *x) const;
bool contains(char x) const;

View file

@ -190,6 +190,9 @@ class StringTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(str.contains("haha"), true);
TS_ASSERT_EQUALS(str.contains("hahb"), false);
TS_ASSERT_EQUALS(str.contains("test"), true);
TS_ASSERT_EQUALS(str.contains('/'), true);
TS_ASSERT_EQUALS(str.contains('x'), false);
}
void test_toLowercase() {