diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index a9032fc7..c346da29 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -489,6 +489,13 @@ public: */ CString TrimSuffix_n(const CString& sSuffix) const; + /** Find the position of the given substring. + * @param s The substring to search for. + * @param cs CaseSensitive if you want the comparision to be case + * sensitive, CaseInsensitive (default) otherwise. + * @return The position of the substring if found, CString::npos otherwise. + */ + size_t Find(const CString& s, CaseSensitivity cs = CaseInsensitive) const; /** Check whether the string starts with a given prefix. * @param sPrefix The prefix. * @param cs CaseSensitive if you want the comparision to be case diff --git a/src/ZNCString.cpp b/src/ZNCString.cpp index 69a16806..121b0da6 100644 --- a/src/ZNCString.cpp +++ b/src/ZNCString.cpp @@ -1100,6 +1100,14 @@ bool CString::TrimSuffix(const CString& sSuffix) { } } +size_t CString::Find(const CString& s, CaseSensitivity cs) const { + if (cs == CaseSensitive) { + return find(s); + } else { + return AsLower().find(s.AsLower()); + } +} + bool CString::StartsWith(const CString& sPrefix, CaseSensitivity cs) const { return Left(sPrefix.length()).Equals(sPrefix, cs); } diff --git a/test/StringTest.cpp b/test/StringTest.cpp index 9aa33486..433fd9a6 100644 --- a/test/StringTest.cpp +++ b/test/StringTest.cpp @@ -169,6 +169,16 @@ TEST(StringTest, Equals) { EXPECT_FALSE(CS("ABC").Equals("abc", true)); // deprecated } +TEST(StringTest, Find) { + EXPECT_EQ(CString("Hello, I'm Bob").Find("Hello"), 0u); + EXPECT_EQ(CString("Hello, I'm Bob").Find("Hello", CString::CaseInsensitive), 0u); + EXPECT_EQ(CString("Hello, I'm Bob").Find("Hello", CString::CaseSensitive), 0u); + + EXPECT_EQ(CString("Hello, I'm Bob").Find("i'm"), 7u); + EXPECT_EQ(CString("Hello, I'm Bob").Find("i'm", CString::CaseInsensitive), 7u); + EXPECT_EQ(CString("Hello, I'm Bob").Find("i'm", CString::CaseSensitive), CString::npos); +} + TEST(StringTest, StartsWith) { EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("Hello")); EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("Hello", CString::CaseInsensitive));