[Libreoffice-commits] online.git: Branch 'distro/collabora/collabora-online-cd-3-2' - common/Protocol.hpp common/Util.hpp kit/ForKit.cpp test/WhiteBoxTests.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Tue Jul 17 17:42:32 UTC 2018


 common/Protocol.hpp    |   42 +++++-------------------------------------
 common/Util.hpp        |   34 ++++++++++++++++++++++++++++++++++
 kit/ForKit.cpp         |    2 +-
 test/WhiteBoxTests.cpp |   10 +++++-----
 4 files changed, 45 insertions(+), 43 deletions(-)

New commits:
commit 5462a66763f2cee6af8c5e692dbea04552bbc554
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Jul 13 00:18:30 2018 -0400

    wsd: move string utilities into Util
    
    Change-Id: Idc578dff4e8ee5e48c1b7780d3feb2d21c6a9b13
    Reviewed-on: https://gerrit.libreoffice.org/57539
    Reviewed-by: Jan Holesovsky <kendy at collabora.com>
    Tested-by: Jan Holesovsky <kendy at collabora.com>

diff --git a/common/Protocol.hpp b/common/Protocol.hpp
index 1e2158a91..fd39423bc 100644
--- a/common/Protocol.hpp
+++ b/common/Protocol.hpp
@@ -21,6 +21,8 @@
 
 #include <Poco/Net/WebSocket.h>
 
+#include <Util.hpp>
+
 #define LOK_USE_UNSTABLE_API
 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
 
@@ -144,45 +146,11 @@ namespace LOOLProtocol
         return getTokenInteger(tokenize(message), name, value);
     }
 
-    inline size_t getDelimiterPosition(const char* message, const int length, const char delim)
-    {
-        if (message && length > 0)
-        {
-            const char *founddelim = static_cast<const char *>(std::memchr(message, delim, length));
-            const auto size = (founddelim == nullptr ? length : founddelim - message);
-            return size;
-        }
-
-        return 0;
-    }
-
-    inline
-    std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
-    {
-        const auto size = getDelimiterPosition(message, length, delim);
-        return std::string(message, size);
-    }
-
-    /// Split a string in two at the delimeter, removing it.
-    inline
-    std::pair<std::string, std::string> split(const char* s, const int length, const char delimeter = ' ')
-    {
-        const auto size = getDelimiterPosition(s, length, delimeter);
-        return std::make_pair(std::string(s, size), std::string(s+size+1));
-    }
-
-    /// Split a string in two at the delimeter, removing it.
-    inline
-    std::pair<std::string, std::string> split(const std::string& s, const char delimeter = ' ')
-    {
-        return split(s.c_str(), s.size(), delimeter);
-    }
-
     /// Returns the first token of a message.
     inline
     std::string getFirstToken(const char *message, const int length, const char delim = ' ')
     {
-        return getDelimitedInitialSubstring(message, length, delim);
+        return Util::getDelimitedInitialSubstring(message, length, delim);
     }
 
     template <typename T>
@@ -246,7 +214,7 @@ namespace LOOLProtocol
     inline
     std::string getFirstLine(const char *message, const int length)
     {
-        return getDelimitedInitialSubstring(message, length, '\n');
+        return Util::getDelimitedInitialSubstring(message, length, '\n');
     }
 
     /// Returns the first line of any data which payload char*.
@@ -282,7 +250,7 @@ namespace LOOLProtocol
 
     inline std::string getAbbreviatedMessage(const std::string& message)
     {
-        const auto pos = getDelimiterPosition(message.data(), std::min(message.size(), 500UL), '\n');
+        const auto pos = Util::getDelimiterPosition(message.data(), std::min(message.size(), 501UL), '\n');
 
         // If first line is less than the length (minus newline), add ellipsis.
         if (pos < static_cast<std::string::size_type>(message.size()) - 1)
diff --git a/common/Util.hpp b/common/Util.hpp
index d8717b234..3d457997f 100644
--- a/common/Util.hpp
+++ b/common/Util.hpp
@@ -242,6 +242,40 @@ namespace Util
         return false;
     }
 
+    inline size_t getDelimiterPosition(const char* message, const int length, const char delim)
+    {
+        if (message && length > 0)
+        {
+            const char *founddelim = static_cast<const char *>(std::memchr(message, delim, length));
+            const auto size = (founddelim == nullptr ? length : founddelim - message);
+            return size;
+        }
+
+        return 0;
+    }
+
+    inline
+    std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
+    {
+        const auto size = getDelimiterPosition(message, length, delim);
+        return std::string(message, size);
+    }
+
+    /// Split a string in two at the delimeter, removing it.
+    inline
+    std::pair<std::string, std::string> split(const char* s, const int length, const char delimeter = ' ')
+    {
+        const auto size = getDelimiterPosition(s, length, delimeter);
+        return std::make_pair(std::string(s, size), std::string(s+size+1));
+    }
+
+    /// Split a string in two at the delimeter, removing it.
+    inline
+    std::pair<std::string, std::string> split(const std::string& s, const char delimeter = ' ')
+    {
+        return split(s.c_str(), s.size(), delimeter);
+    }
+
     /// Check for the URI scheme validity.
     /// For now just a basic sanity check, can be extended if necessary.
     bool isValidURIScheme(const std::string& scheme);
diff --git a/kit/ForKit.cpp b/kit/ForKit.cpp
index d77c01c30..2548a1c61 100644
--- a/kit/ForKit.cpp
+++ b/kit/ForKit.cpp
@@ -453,7 +453,7 @@ int main(int argc, char** argv)
             std::vector<std::string> tokens = LOOLProtocol::tokenize(rlimits, ';');
             for (const std::string& cmdLimit : tokens)
             {
-                const auto pair = LOOLProtocol::split(cmdLimit, ':');
+                const auto pair = Util::split(cmdLimit, ':');
                 std::vector<std::string> tokensLimit = { "setconfig", pair.first, pair.second };
                 if (!Rlimit::handleSetrlimitCommand(tokensLimit))
                 {
diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp
index 7928fb152..8cd237cab 100644
--- a/test/WhiteBoxTests.cpp
+++ b/test/WhiteBoxTests.cpp
@@ -144,11 +144,11 @@ void WhiteBoxTests::testLOOLProtocolFunctions()
 
 void WhiteBoxTests::testMessageAbbreviation()
 {
-    CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring(nullptr, 5, '\n'));
-    CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring(nullptr, -1, '\n'));
-    CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring("abc", 0, '\n'));
-    CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getDelimitedInitialSubstring("abc", -1, '\n'));
-    CPPUNIT_ASSERT_EQUAL(std::string("ab"), LOOLProtocol::getDelimitedInitialSubstring("abc", 2, '\n'));
+    CPPUNIT_ASSERT_EQUAL(std::string(), Util::getDelimitedInitialSubstring(nullptr, 5, '\n'));
+    CPPUNIT_ASSERT_EQUAL(std::string(), Util::getDelimitedInitialSubstring(nullptr, -1, '\n'));
+    CPPUNIT_ASSERT_EQUAL(std::string(), Util::getDelimitedInitialSubstring("abc", 0, '\n'));
+    CPPUNIT_ASSERT_EQUAL(std::string(), Util::getDelimitedInitialSubstring("abc", -1, '\n'));
+    CPPUNIT_ASSERT_EQUAL(std::string("ab"), Util::getDelimitedInitialSubstring("abc", 2, '\n'));
 
     CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getAbbreviatedMessage(nullptr, 5));
     CPPUNIT_ASSERT_EQUAL(std::string(), LOOLProtocol::getAbbreviatedMessage(nullptr, -1));


More information about the Libreoffice-commits mailing list