diff --git a/common/Util.hpp b/common/Util.hpp index e567972139..58a33e2830 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -120,6 +120,66 @@ namespace Util return s; } + /// Trim spaces from both left and right. Just spaces. + inline std::string& trim(std::string& s) + { + const auto first = s.find_first_not_of(' '); + const auto last = s.find_last_not_of(' '); + if (first != std::string::npos) + { + if (last != std::string::npos) + { + s = s.substr(first, last + 1 - first); + } + else + { + s = s.substr(first); + } + } + else + { + if (last != std::string::npos) + { + s = s.substr(0, last + 1); + } + else + { + s.clear(); + } + } + + return s; + } + + /// Trim spaces from both left and right and copy. Just spaces. + inline std::string trimmed(const std::string& s) + { + const auto first = s.find_first_not_of(' '); + const auto last = s.find_last_not_of(' '); + if (first != std::string::npos) + { + if (last != std::string::npos) + { + return s.substr(first, last + 1 - first); + } + + return s.substr(first); + } + + if (last != std::string::npos) + { + return s.substr(0, last + 1); + } + + return std::string(); + } + + /// Trim spaces from left and right. Just spaces. + inline std::string trimmed(const char* s) + { + return trimmed(std::string(s)); + } + /// Given one or more patterns to allow, and one or more to deny, /// the match member will return true if, and only if, the subject /// matches the allowed list, but not the deny. diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp index 6ca75bcbb8..0b9620a597 100644 --- a/test/WhiteBoxTests.cpp +++ b/test/WhiteBoxTests.cpp @@ -14,8 +14,8 @@ #include #include #include -#include #include +#include #include /// WhiteBox unit-tests. @@ -73,6 +73,55 @@ void WhiteBoxTests::testLOOLProtocolFunctions() CPPUNIT_ASSERT(LOOLProtocol::getTokenKeywordFromMessage(message, "mumble", map, mumble)); CPPUNIT_ASSERT_EQUAL(2, mumble); + CPPUNIT_ASSERT_EQUAL(1UL, Util::trimmed("A").size()); + CPPUNIT_ASSERT_EQUAL(std::string("A"), Util::trimmed("A")); + + CPPUNIT_ASSERT_EQUAL(1UL, Util::trimmed(" X").size()); + CPPUNIT_ASSERT_EQUAL(std::string("X"), Util::trimmed(" X")); + + CPPUNIT_ASSERT_EQUAL(1UL, Util::trimmed("Y ").size()); + CPPUNIT_ASSERT_EQUAL(std::string("Y"), Util::trimmed("Y ")); + + CPPUNIT_ASSERT_EQUAL(1UL, Util::trimmed(" Z ").size()); + CPPUNIT_ASSERT_EQUAL(std::string("Z"), Util::trimmed(" Z ")); + + CPPUNIT_ASSERT_EQUAL(0UL, Util::trimmed(" ").size()); + CPPUNIT_ASSERT_EQUAL(std::string(""), Util::trimmed(" ")); + + CPPUNIT_ASSERT_EQUAL(0UL, Util::trimmed(" ").size()); + CPPUNIT_ASSERT_EQUAL(std::string(""), Util::trimmed(" ")); + + std::string s; + + s = "A"; + CPPUNIT_ASSERT_EQUAL(1UL, Util::trim(s).size()); + s = "A"; + CPPUNIT_ASSERT_EQUAL(std::string("A"), Util::trim(s)); + + s = " X"; + CPPUNIT_ASSERT_EQUAL(1UL, Util::trim(s).size()); + s = " X"; + CPPUNIT_ASSERT_EQUAL(std::string("X"), Util::trim(s)); + + s = "Y "; + CPPUNIT_ASSERT_EQUAL(1UL, Util::trim(s).size()); + s = "Y "; + CPPUNIT_ASSERT_EQUAL(std::string("Y"), Util::trim(s)); + + s = " Z "; + CPPUNIT_ASSERT_EQUAL(1UL, Util::trim(s).size()); + s = " Z "; + CPPUNIT_ASSERT_EQUAL(std::string("Z"), Util::trim(s)); + + s = " "; + CPPUNIT_ASSERT_EQUAL(0UL, Util::trim(s).size()); + s = " "; + CPPUNIT_ASSERT_EQUAL(std::string(""), Util::trim(s)); + + s = " "; + CPPUNIT_ASSERT_EQUAL(0UL, Util::trim(s).size()); + s = " "; + CPPUNIT_ASSERT_EQUAL(std::string(""), Util::trim(s)); } void WhiteBoxTests::testRegexListMatcher() diff --git a/test/test.cpp b/test/test.cpp index ba809121df..0f8b359487 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -58,7 +58,6 @@ int main(int /*argc*/, char** /*argv*/) { Log::initialize("tst"); - CPPUNIT_NS::TestResult controller; CPPUNIT_NS::TestResultCollector result; controller.addListener(&result);