Introduce CaseSensitivity enum class

The enum is a bit more verbose, but leads to more readable code:

str.Equals("foo", true)
// vs.
str.Equals("foo", CString::CaseSensitive)

Deprecate the old Equals() and leave out the length parameter
from the new version => use StartsWith() or StrCmp() instead.
This commit is contained in:
J-P Nurmi
2014-09-29 16:10:26 +02:00
parent 227f2cfb29
commit e86f43d841
3 changed files with 31 additions and 4 deletions
+15 -4
View File
@@ -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:
+8
View File
@@ -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);
+8
View File
@@ -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
}