CString::Starts/EndsWith(): allow specifying case sensitivity

This commit is contained in:
J-P Nurmi
2014-09-29 16:16:03 +02:00
parent e86f43d841
commit 65f739980d
3 changed files with 30 additions and 6 deletions

View File

@@ -491,14 +491,18 @@ public:
/** Check whether the string starts with a given prefix.
* @param sPrefix The prefix.
* @param cs CaseSensitive if you want the comparision to be case
* sensitive, CaseInsensitive (default) otherwise.
* @return True if the string starts with prefix, false otherwise.
*/
bool StartsWith(const CString& sPrefix) const;
bool StartsWith(const CString& sPrefix, CaseSensitivity cs = CaseInsensitive) const;
/** Check whether the string ends with a given suffix.
* @param sSuffix The suffix.
* @param cs CaseSensitive if you want the comparision to be case
* sensitive, CaseInsensitive (default) otherwise.
* @return True if the string ends with suffix, false otherwise.
*/
bool EndsWith(const CString& sSuffix) const;
bool EndsWith(const CString& sSuffix, CaseSensitivity cs = CaseInsensitive) const;
/** Remove characters from the beginning of this string.
* @param uLen The number of characters to remove.

View File

@@ -1100,12 +1100,12 @@ bool CString::TrimSuffix(const CString& sSuffix) {
}
}
bool CString::StartsWith(const CString& sPrefix) const {
return Left(sPrefix.length()).Equals(sPrefix);
bool CString::StartsWith(const CString& sPrefix, CaseSensitivity cs) const {
return Left(sPrefix.length()).Equals(sPrefix, cs);
}
bool CString::EndsWith(const CString& sSuffix) const {
return Right(sSuffix.length()).Equals(sSuffix);
bool CString::EndsWith(const CString& sSuffix, CaseSensitivity cs) const {
return Right(sSuffix.length()).Equals(sSuffix, cs);
}

View File

@@ -168,3 +168,23 @@ TEST(StringTest, Equals) {
EXPECT_TRUE(CS("ABC").Equals("abc", false)); // deprecated
EXPECT_FALSE(CS("ABC").Equals("abc", true)); // deprecated
}
TEST(StringTest, StartsWith) {
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("Hello"));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("Hello", CString::CaseInsensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("Hello", CString::CaseSensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("hello"));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("hello", CString::CaseInsensitive));
EXPECT_FALSE(CString("Hello, I'm Bob").StartsWith("hello", CString::CaseSensitive));
}
TEST(StringTest, EndsWith) {
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("Bob"));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("Bob", CString::CaseInsensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("Bob", CString::CaseSensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("bob"));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("bob", CString::CaseInsensitive));
EXPECT_FALSE(CString("Hello, I'm Bob").EndsWith("bob", CString::CaseSensitive));
}