diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index c346da29..546701b7 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -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. diff --git a/src/ZNCString.cpp b/src/ZNCString.cpp index 121b0da6..c6b7bfe8 100644 --- a/src/ZNCString.cpp +++ b/src/ZNCString.cpp @@ -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; diff --git a/test/StringTest.cpp b/test/StringTest.cpp index 433fd9a6..1c09ee45 100644 --- a/test/StringTest.cpp +++ b/test/StringTest.cpp @@ -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)); +}