diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index 6a63aa6f..d97f9aaa 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -57,6 +57,11 @@ static const unsigned char base64_table[256] = { XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, }; +enum class CaseSensitivity { + CaseInsensitive, + CaseSensitive +}; + /** * @brief String class that is used inside ZNC. * @@ -75,6 +80,9 @@ public: EDEBUG } EEscape; + static const CaseSensitivity CaseSensitive = CaseSensitivity::CaseSensitive; + static const CaseSensitivity CaseInsensitive = CaseSensitivity::CaseInsensitive; + explicit CString(bool b) : std::string(b ? "true" : "false") {} explicit CString(char c); explicit CString(unsigned char c); @@ -149,12 +157,15 @@ public: /** * Check if this string is equal to some other string. * @param s The string to compare to. - * @param bCaseSensitive True if you want the comparision to be case - * sensitive. - * @param uLen Number of characters to consider. + * @param cs CaseSensitive if you want the comparision to be case + * sensitive, CaseInsensitive (default) otherwise. * @return True if the strings are equal. */ - bool Equals(const CString& s, bool bCaseSensitive = false, CString::size_type uLen = CString::npos) const; + bool Equals(const CString& s, CaseSensitivity cs = CaseInsensitive) const; + /** + * @deprecated + */ + bool Equals(const CString& s, bool bCaseSensitive, CString::size_type uLen = CString::npos) const; /** * Do a wildcard comparision between two strings. * For example, the following returns true: diff --git a/src/ZNCString.cpp b/src/ZNCString.cpp index 4e803881..f264f9e3 100644 --- a/src/ZNCString.cpp +++ b/src/ZNCString.cpp @@ -79,6 +79,14 @@ int CString::StrCmp(const CString& s, CString::size_type uLen) const { return strcmp(c_str(), s.c_str()); } +bool CString::Equals(const CString& s, CaseSensitivity cs) const { + if (cs == CaseSensitive) { + return (StrCmp(s) == 0); + } else { + return (CaseCmp(s) == 0); + } +} + bool CString::Equals(const CString& s, bool bCaseSensitive, CString::size_type uLen) const { if (bCaseSensitive) { return (StrCmp(s, uLen) == 0); diff --git a/test/StringTest.cpp b/test/StringTest.cpp index 02be98fb..97eca857 100644 --- a/test/StringTest.cpp +++ b/test/StringTest.cpp @@ -160,3 +160,11 @@ TEST(StringTest, Hash) { EXPECT_EQ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", CS("").SHA256()); EXPECT_EQ("ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", CS("a").SHA256()); } + +TEST(StringTest, Equals) { + EXPECT_TRUE(CS("ABC").Equals("abc")); + EXPECT_TRUE(CS("ABC").Equals("abc", CString::CaseInsensitive)); + EXPECT_FALSE(CS("ABC").Equals("abc", CString::CaseSensitive)); + EXPECT_TRUE(CS("ABC").Equals("abc", false)); // deprecated + EXPECT_FALSE(CS("ABC").Equals("abc", true)); // deprecated +}