mirror of
https://github.com/LibreOffice/online.git
synced 2025-08-20 23:24:34 +00:00
loolwsd: string trimming helpers
Change-Id: I5e47f92b624890421bd7022084063cdea77da12b Reviewed-on: https://gerrit.libreoffice.org/32155 Reviewed-by: Ashod Nakashian <ashnakash@gmail.com> Tested-by: Ashod Nakashian <ashnakash@gmail.com>
This commit is contained in:

committed by
Ashod Nakashian

parent
8099b84c3e
commit
32271b8d5c
@ -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.
|
||||
|
@ -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()
|
||||
|
@ -58,7 +58,6 @@ int main(int /*argc*/, char** /*argv*/)
|
||||
{
|
||||
Log::initialize("tst");
|
||||
|
||||
|
||||
CPPUNIT_NS::TestResult controller;
|
||||
CPPUNIT_NS::TestResultCollector result;
|
||||
controller.addListener(&result);
|
||||
|
Reference in New Issue
Block a user