common/Util.hpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ test/WhiteBoxTests.cpp | 51 ++++++++++++++++++++++++++++++++++++++++- test/test.cpp | 1 3 files changed, 110 insertions(+), 2 deletions(-)
New commits: commit 32271b8d5c108c452db1089c100c40a62002db1f Author: Ashod Nakashian <[email protected]> Date: Sat Dec 17 09:55:28 2016 -0500 loolwsd: string trimming helpers Change-Id: I5e47f92b624890421bd7022084063cdea77da12b Reviewed-on: https://gerrit.libreoffice.org/32155 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/common/Util.hpp b/common/Util.hpp index e567972..58a33e2 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 6ca75bc..0b9620a 100644 --- a/test/WhiteBoxTests.cpp +++ b/test/WhiteBoxTests.cpp @@ -14,8 +14,8 @@ #include <ChildSession.hpp> #include <Common.hpp> #include <Kit.hpp> -#include <Protocol.hpp> #include <MessageQueue.hpp> +#include <Protocol.hpp> #include <Util.hpp> /// 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 ba80912..0f8b359 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); _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
