[Libreoffice-commits] online.git: common/Protocol.hpp common/StringVector.cpp common/StringVector.hpp

Ashod Nakashian (via logerrit) logerrit at kemper.freedesktop.org
Tue Jun 2 15:25:39 UTC 2020


 common/Protocol.hpp     |   58 +++++++++++++++++++++++++++---------------------
 common/StringVector.cpp |   19 ---------------
 common/StringVector.hpp |   19 +++++++++++++--
 3 files changed, 49 insertions(+), 47 deletions(-)

New commits:
commit 21ae15a49b92ad5116807c35c67db2e4415dfa1e
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Mon Jun 1 22:22:42 2020 -0400
Commit:     Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Tue Jun 2 17:25:19 2020 +0200

    wsd: optimized tokenization
    
    Change-Id: I79b5117b89c982c15cee5c1e230a44785059affd
    Reviewed-on: https://gerrit.libreoffice.org/c/online/+/95336
    Tested-by: Jenkins
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice at gmail.com>
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/common/Protocol.hpp b/common/Protocol.hpp
index 1327c5fc5..60d1aa1ce 100644
--- a/common/Protocol.hpp
+++ b/common/Protocol.hpp
@@ -95,60 +95,68 @@ namespace LOOLProtocol
     bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value);
 
     /// Tokenize space-delimited values until we hit new-line or the end.
-    inline
-    StringVector tokenize(const char* data, const size_t size, const char delimiter = ' ')
+    template <typename T>
+    inline void tokenize(const char* data, const std::size_t size, const T& delimiter,
+                         std::vector<StringToken>& tokens)
     {
-        std::vector<StringToken> tokens;
-        if (size == 0 || data == nullptr)
-        {
-            return StringVector(std::string(), {});
-        }
-        tokens.reserve(8);
+        if (size == 0 || data == nullptr || *data == '\0')
+            return;
+
+        tokens.reserve(16);
 
         const char* start = data;
         const char* end = data;
-        for (size_t i = 0; i < size && data[i] != '\n'; ++i, ++end)
+        for (std::size_t i = 0; i < size && data[i] != '\n'; ++i, ++end)
         {
             if (data[i] == delimiter)
             {
                 if (start != end && *start != delimiter)
-                {
                     tokens.emplace_back(start - data, end - start);
-                }
 
                 start = end;
             }
             else if (*start == delimiter)
-            {
                 ++start;
-            }
         }
 
         if (start != end && *start != delimiter && *start != '\n')
-        {
             tokens.emplace_back(start - data, end - start);
-        }
+    }
 
+    /// Tokenize single-char delimited values until we hit new-line or the end.
+    inline StringVector tokenize(const char* data, const std::size_t size,
+                                 const char delimiter = ' ')
+    {
+        if (size == 0 || data == nullptr || *data == '\0')
+            return StringVector();
+
+        std::vector<StringToken> tokens;
+        tokenize(data, size, delimiter, tokens);
         return StringVector(std::string(data, size), tokens);
     }
 
-    inline
-    StringVector tokenize(const std::string& s, const char delimiter = ' ')
+    /// Tokenize single-char delimited values until we hit new-line or the end.
+    inline StringVector tokenize(const std::string& s, const char delimiter = ' ')
     {
-        return tokenize(s.data(), s.size(), delimiter);
+        if (s.empty())
+            return StringVector();
+
+        std::vector<StringToken> tokens;
+        tokenize(s.data(), s.size(), delimiter, tokens);
+        return StringVector(s, tokens);
     }
 
     inline
     StringVector tokenize(const std::string& s, const char* delimiter)
     {
-        std::vector<StringToken> tokens;
-        if (s.size() == 0)
-        {
-            return StringVector(std::string(), {});
-        }
+        if (s.empty())
+            return StringVector();
+
+        std::size_t start = 0;
+        std::size_t end = s.find(delimiter, start);
 
-        size_t start = 0;
-        size_t end = s.find(delimiter, start);
+        std::vector<StringToken> tokens;
+        tokens.reserve(16);
 
         tokens.emplace_back(start, end - start);
         start = end + std::strlen(delimiter);
diff --git a/common/StringVector.cpp b/common/StringVector.cpp
index 2fef5aa0b..f44ec2233 100644
--- a/common/StringVector.cpp
+++ b/common/StringVector.cpp
@@ -9,25 +9,6 @@
 
 #include "StringVector.hpp"
 
-StringVector::StringVector() = default;
-
-StringVector::StringVector(const std::string& string, const std::vector<StringToken>& tokens)
-    : _string(string),
-    _tokens(tokens)
-{
-}
-
-std::string StringVector::operator[](std::size_t index) const
-{
-    if (index >= _tokens.size())
-    {
-        return std::string();
-    }
-
-    const StringToken& token = _tokens[index];
-    return _string.substr(token._index, token._length);
-}
-
 bool StringVector::equals(std::size_t index, const StringVector& other, std::size_t otherIndex)
 {
     if (index >= _tokens.size())
diff --git a/common/StringVector.hpp b/common/StringVector.hpp
index fa0e94491..568ebf989 100644
--- a/common/StringVector.hpp
+++ b/common/StringVector.hpp
@@ -40,12 +40,25 @@ class StringVector
     std::vector<StringToken> _tokens;
 
 public:
-    explicit StringVector();
+    explicit StringVector() = default;
 
-    explicit StringVector(const std::string& string, const std::vector<StringToken>& tokens);
+    explicit StringVector(const std::string& string, const std::vector<StringToken>& tokens)
+        : _string(string)
+        , _tokens(tokens)
+    {
+    }
 
     /// Unlike std::vector, gives an empty string if index is unexpected.
-    std::string operator[](std::size_t index) const;
+    std::string operator[](std::size_t index) const
+    {
+        if (index >= _tokens.size())
+        {
+            return std::string();
+        }
+
+        const StringToken& token = _tokens[index];
+        return _string.substr(token._index, token._length);
+    }
 
     std::size_t size() const { return _tokens.size(); }
 


More information about the Libreoffice-commits mailing list