Merge pull request #686 from jpnurmi/string

CString improvements
This commit is contained in:
Alexey Sokolov
2014-09-29 23:39:42 +01:00
3 changed files with 111 additions and 10 deletions
+35 -6
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:
@@ -478,16 +489,34 @@ 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
* sensitive, CaseInsensitive (default) otherwise.
* @return True if the string starts with prefix, false otherwise.
*/
bool StartsWith(const CString& sPrefix) const;
bool StartsWith(const CString& sPrefix, CaseSensitivity cs = CaseInsensitive) const;
/** Check whether the string ends with a given suffix.
* @param sSuffix The suffix.
* @param cs CaseSensitive if you want the comparision to be case
* sensitive, CaseInsensitive (default) otherwise.
* @return True if the string ends with suffix, false otherwise.
*/
bool EndsWith(const CString& sSuffix) const;
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.
+24 -4
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);
@@ -1092,12 +1100,24 @@ bool CString::TrimSuffix(const CString& sSuffix) {
}
}
bool CString::StartsWith(const CString& sPrefix) const {
return Left(sPrefix.length()).Equals(sPrefix);
size_t CString::Find(const CString& s, CaseSensitivity cs) const {
if (cs == CaseSensitive) {
return find(s);
} else {
return AsLower().find(s.AsLower());
}
}
bool CString::EndsWith(const CString& sSuffix) const {
return Right(sSuffix.length()).Equals(sSuffix);
bool CString::StartsWith(const CString& sPrefix, CaseSensitivity cs) const {
return Left(sPrefix.length()).Equals(sPrefix, cs);
}
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;
}
+52
View File
@@ -160,3 +160,55 @@ 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
}
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));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("Hello", CString::CaseSensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("hello"));
EXPECT_TRUE(CString("Hello, I'm Bob").StartsWith("hello", CString::CaseInsensitive));
EXPECT_FALSE(CString("Hello, I'm Bob").StartsWith("hello", CString::CaseSensitive));
}
TEST(StringTest, EndsWith) {
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("Bob"));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("Bob", CString::CaseInsensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("Bob", CString::CaseSensitive));
EXPECT_TRUE(CString("Hello, I'm Bob").EndsWith("bob"));
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));
}