[Libreoffice-commits] online.git: common/Util.hpp

Ashod Nakashian (via logerrit) logerrit at kemper.freedesktop.org
Tue Jun 23 04:38:53 UTC 2020


 common/Util.hpp |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

New commits:
commit 8e784f2bdd9cc789fe896a31d13d22aa8d39e0bb
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Sat Jun 20 14:06:41 2020 -0400
Commit:     Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Tue Jun 23 06:38:34 2020 +0200

    wsd: tokenization and hexify utils
    
    Change-Id: I3a8eb39092ad5ed1a8589c05a893fd0ce6445e8b
    Reviewed-on: https://gerrit.libreoffice.org/c/online/+/96827
    Tested-by: Jenkins
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice at gmail.com>
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/common/Util.hpp b/common/Util.hpp
index 9c82b3068..f6ca50825 100644
--- a/common/Util.hpp
+++ b/common/Util.hpp
@@ -224,6 +224,21 @@ namespace Util
         return os.str();
     }
 
+    /// Dump a string as hex by splitting on multiple lines per width.
+    /// Useful for debugging and logging data that contain non-printables.
+    inline std::string stringifyHexLine(const std::string& s, const std::size_t width = 16)
+    {
+        std::ostringstream oss;
+        for (std::size_t i = 0; i < s.size(); i += width)
+        {
+            const std::size_t rem = std::min(width, s.size() - i);
+            oss << stringifyHexLine(std::vector<char>(s.data(), s.data() + s.size()), i, rem);
+            oss << '\n';
+        }
+
+        return oss.str();
+    }
+
     /// Dump data as hex and chars to stream
     inline void dumpHex (std::ostream &os, const char *legend, const char *prefix,
                          const std::vector<char> &buffer, bool skipDup = true,
@@ -433,6 +448,39 @@ namespace Util
         return StringVector(s, std::move(tokens));
     }
 
+    /// Tokenize by the delimiter string.
+    inline StringVector tokenize(const std::string& s, const char* delimiter, int len = -1)
+    {
+        if (s.empty() || len == 0 || delimiter == nullptr || *delimiter == '\0')
+            return StringVector();
+
+        if (len < 0)
+            len = std::strlen(delimiter);
+
+        std::size_t start = 0;
+        std::size_t end = s.find(delimiter, start);
+
+        std::vector<StringToken> tokens;
+        tokens.reserve(16);
+
+        tokens.emplace_back(start, end - start);
+        start = end + len;
+
+        while (end != std::string::npos)
+        {
+            end = s.find(delimiter, start);
+            tokens.emplace_back(start, end - start);
+            start = end + len;
+        }
+
+        return StringVector(s, std::move(tokens));
+    }
+
+    inline StringVector tokenize(const std::string& s, const std::string& delimiter)
+    {
+        return tokenize(s, delimiter.data(), delimiter.size());
+    }
+
     /** Tokenize based on any of the characters in 'delimiters'.
 
         Ie. when there is '\n\r' in there, any of them means a delimiter.


More information about the Libreoffice-commits mailing list