summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2022-06-18 10:32:27 -0400
committerthebearon <19438782+thebearon@users.noreply.github.com>2022-08-05 06:49:12 +0200
commita947cfbf1ad0cc4c50106cb697cf7d112afc8a50 (patch)
treec01b5fc615ba8121c6350df6453dd4625453da0c
parentwsd: test: cleanup and cosmetics (diff)
downloadonline-a947cfbf1ad0cc4c50106cb697cf7d112afc8a50.tar.gz
online-a947cfbf1ad0cc4c50106cb697cf7d112afc8a50.zip
wsd: Message supports new find and contains operations
Change-Id: Id5112dda76fe2aaa27392d59f9b8ecfa3cfabe54 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk> (cherry picked from commit bb1978770a79453542cca112f9af55c0279b9c9e)
-rw-r--r--common/Message.hpp11
-rw-r--r--common/Util.hpp22
2 files changed, 33 insertions, 0 deletions
diff --git a/common/Message.hpp b/common/Message.hpp
index 3e9c8d0b42..9cb0616ccd 100644
--- a/common/Message.hpp
+++ b/common/Message.hpp
@@ -8,6 +8,7 @@
#pragma once
#include <atomic>
+#include <cstring>
#include <string>
#include <vector>
#include <functional>
@@ -15,6 +16,7 @@
#include "Protocol.hpp"
#include "StringVector.hpp"
#include "Log.hpp"
+#include "Util.hpp"
/// The payload type used to send/receive data.
class Message
@@ -76,6 +78,15 @@ public:
bool firstTokenMatches(const std::string& target) const { return _tokens[0] == target; }
std::string operator[](size_t index) const { return _tokens[index]; }
+ /// Find a subarray in the raw message.
+ int find(const char* sub, const std::size_t subLen) const
+ {
+ return Util::findSubArray(&_data[0], _data.size(), sub, subLen);
+ }
+
+ /// Returns true iff the subarray exists in the raw message.
+ bool contains(const char* p, const std::size_t len) const { return find(p, len) >= 0; }
+
const std::string& firstLine()
{
assignFirstLineIfEmpty();
diff --git a/common/Util.hpp b/common/Util.hpp
index d403476402..9cdd720c27 100644
--- a/common/Util.hpp
+++ b/common/Util.hpp
@@ -864,6 +864,28 @@ int main(int argc, char**argv)
return 0;
}
+ /// Return the position of sub-array @sub in array @data, if found, -1 otherwise.
+ inline int findSubArray(const char* data, const std::size_t dataLen, const char* sub,
+ const std::size_t subLen)
+ {
+ assert(subLen < std::numeric_limits<int>::max() && "Invalid sub-array length to find");
+ if (sub && subLen && dataLen >= subLen)
+ {
+ for (std::size_t i = 0; i < dataLen; ++i)
+ {
+ std::size_t j;
+ for (j = 0; j < subLen && i + j < dataLen && data[i + j] == sub[j]; ++j)
+ {
+ }
+
+ if (j >= subLen)
+ return i;
+ }
+ }
+
+ return -1;
+ }
+
inline
std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
{