From 65f739980d282d46ef33876d71dfdab9dcdd4758 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 29 Sep 2014 16:16:03 +0200 Subject: [PATCH] CString::Starts/EndsWith(): allow specifying case sensitivity --- include/znc/ZNCString.h | 8 ++++++-- src/ZNCString.cpp | 8 ++++---- test/StringTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index d97f9aaa..a9032fc7 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -491,14 +491,18 @@ public: /** 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; /** 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 f264f9e3..69a16806 100644 --- a/src/ZNCString.cpp +++ b/src/ZNCString.cpp @@ -1100,12 +1100,12 @@ bool CString::TrimSuffix(const CString& sSuffix) { } } -bool CString::StartsWith(const CString& sPrefix) const { - return Left(sPrefix.length()).Equals(sPrefix); +bool CString::StartsWith(const CString& sPrefix, CaseSensitivity cs) const { + return Left(sPrefix.length()).Equals(sPrefix, cs); } -bool CString::EndsWith(const CString& sSuffix) const { - return Right(sSuffix.length()).Equals(sSuffix); +bool CString::EndsWith(const CString& sSuffix, CaseSensitivity cs) const { + return Right(sSuffix.length()).Equals(sSuffix, cs); } diff --git a/test/StringTest.cpp b/test/StringTest.cpp index 97eca857..9aa33486 100644 --- a/test/StringTest.cpp +++ b/test/StringTest.cpp @@ -168,3 +168,23 @@ TEST(StringTest, Equals) { EXPECT_TRUE(CS("ABC").Equals("abc", false)); // deprecated EXPECT_FALSE(CS("ABC").Equals("abc", true)); // deprecated } + +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)); +}