[Libreoffice-commits] online.git: loolwsd/LOOLProtocol.cpp loolwsd/LOOLProtocol.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Tue Mar 29 02:48:14 UTC 2016


 loolwsd/LOOLProtocol.cpp |   24 ++++++++----------------
 loolwsd/LOOLProtocol.hpp |   39 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 44 insertions(+), 19 deletions(-)

New commits:
commit 9f39e465a6f898fee916b2d63639c56f140e85ad
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sun Mar 27 14:30:03 2016 -0400

    loolwsd: cleanup of the command parser
    
    Change-Id: I654e42ab6de92b7120ae2212d7e08c682d3d36f0
    Reviewed-on: https://gerrit.libreoffice.org/23584
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/LOOLProtocol.cpp b/loolwsd/LOOLProtocol.cpp
index 7c635be..b514636 100644
--- a/loolwsd/LOOLProtocol.cpp
+++ b/loolwsd/LOOLProtocol.cpp
@@ -150,28 +150,20 @@ namespace LOOLProtocol
         return true;
     }
 
-    std::string getFirstLine(const char *message, int length)
-    {
-        if (message == nullptr || length <= 0)
-            return "";
-
-        const char *endOfLine = static_cast<const char *>(std::memchr(message, '\n', length));
-        if (endOfLine == nullptr)
-            return std::string(message, length);
-        else
-            return std::string(message, endOfLine-message);
-    }
-
-    std::string getAbbreviatedMessage(const char *message, int length)
+    std::string getAbbreviatedMessage(const char *message, const int length)
     {
         if (message == nullptr || length <= 0)
+        {
             return "";
+        }
 
         const auto firstLine = getFirstLine(message, length);
-        std::string result = "'" + firstLine + "'";
         if (firstLine.size() < static_cast<std::string::size_type>(length))
-            result += "...";
-        return result;
+        {
+            return std::string('\'' + firstLine + "'...");
+        }
+
+        return std::string('\'' + firstLine + '\'');
     }
 };
 
diff --git a/loolwsd/LOOLProtocol.hpp b/loolwsd/LOOLProtocol.hpp
index 5978041..9a5ac8b 100644
--- a/loolwsd/LOOLProtocol.hpp
+++ b/loolwsd/LOOLProtocol.hpp
@@ -79,7 +79,7 @@ namespace LOOLProtocol
     std::tuple<int, int, std::string> ParseVersion(const std::string& version);
 
     bool stringToInteger(const std::string& input, int& value);
-	
+
     bool getTokenInteger(const std::string& token, const std::string& name, int& value);
     bool getTokenString(const std::string& token, const std::string& name, std::string& value);
     bool getTokenKeyword(const std::string& token, const std::string& name, const std::map<std::string, int>& map, int& value);
@@ -87,8 +87,41 @@ namespace LOOLProtocol
     // Functions that parse messages. All return false if parsing fails
     bool parseStatus(const std::string& message, LibreOfficeKitDocumentType& type, int& nParts, int& currentPart, int& width, int& height);
 
-    std::string getFirstLine(const char *message, int length);
-    std::string getAbbreviatedMessage(const char *message, int length);
+    /// Returns the first token of a message given a delimiter.
+    inline
+    std::string getFirstToken(const char *message, const int length, const char delim = ' ')
+    {
+        if (message == nullptr || length <= 0)
+        {
+            return "";
+        }
+
+        const char *endOfLine = static_cast<const char *>(std::memchr(message, delim, length));
+        const auto size = (endOfLine == nullptr ? length : endOfLine - message);
+        return std::string(message, size);
+    }
+
+    inline
+    std::string getFirstToken(const std::vector<char>& message)
+    {
+        return getFirstToken(message.data(), message.size());
+    }
+
+    /// Returns the first line of a message.
+    inline
+    std::string getFirstLine(const char *message, const int length)
+    {
+        return getFirstToken(message, length, '\n');
+    }
+
+    inline
+    std::string getFirstLine(const std::vector<char>& message)
+    {
+        return getFirstLine(message.data(), message.size());
+    }
+
+    /// Returns an abbereviation of the message (the first line, indicating truncation).
+    std::string getAbbreviatedMessage(const char *message, const int length);
 };
 
 #endif


More information about the Libreoffice-commits mailing list