mirror of
https://github.com/znc/znc.git
synced 2026-08-07 01:13:25 +02:00
Add CString::Contains() for convenience
This is more convenient and readable than comparing Find() to npos,
which is a common task:
$ git grep "find(" | grep "npos" | wc -l
49
This commit is contained in:
@@ -510,6 +510,13 @@ public:
|
||||
* @return True if the string ends with suffix, false otherwise.
|
||||
*/
|
||||
bool EndsWith(const CString& sSuffix, CaseSensitivity cs = CaseInsensitive) const;
|
||||
/**
|
||||
* Check whether the string contains a given string.
|
||||
* @param s The string to search.
|
||||
* @param bCaseSensitive Whether the search is case sensitive.
|
||||
* @return True if this string contains the other string, falser otherwise.
|
||||
*/
|
||||
bool Contains(const CString& s, CaseSensitivity cs = CaseInsensitive) const;
|
||||
|
||||
/** Remove characters from the beginning of this string.
|
||||
* @param uLen The number of characters to remove.
|
||||
|
||||
@@ -1116,6 +1116,10 @@ bool CString::EndsWith(const CString& sSuffix, CaseSensitivity cs) const {
|
||||
return Right(sSuffix.length()).Equals(sSuffix, cs);
|
||||
}
|
||||
|
||||
bool CString::Contains(const CString& s, CaseSensitivity cs) const {
|
||||
return Find(s, cs) != npos;
|
||||
}
|
||||
|
||||
|
||||
CString CString::TrimPrefix_n(const CString& sPrefix) const {
|
||||
CString sRet = *this;
|
||||
|
||||
@@ -198,3 +198,17 @@ TEST(StringTest, EndsWith) {
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("bob", CString::CaseInsensitive));
|
||||
EXPECT_FALSE(CString("Hello, I'm Bob").EndsWith("bob", CString::CaseSensitive));
|
||||
}
|
||||
|
||||
TEST(StringTest, Contains) {
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("Hello"));
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("Hello", CString::CaseInsensitive));
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("Hello", CString::CaseSensitive));
|
||||
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("i'm"));
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("i'm", CString::CaseInsensitive));
|
||||
EXPECT_FALSE(CString("Hello, I'm Bob").Contains("i'm", CString::CaseSensitive));
|
||||
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("i'm bob"));
|
||||
EXPECT_TRUE(CString("Hello, I'm Bob").Contains("i'm bob", CString::CaseInsensitive));
|
||||
EXPECT_FALSE(CString("Hello, I'm Bob").Contains("i'm bob", CString::CaseSensitive));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user