aboutsummaryrefslogtreecommitdiff
path: root/src/util/string.h
diff options
context:
space:
mode:
authorVasil Dimov <[email protected]>2020-08-31 10:39:00 +0200
committerVasil Dimov <[email protected]>2020-09-11 13:35:39 +0200
commitd2bb681f96fb327b4c4d5b2b113692ca22fdffbf (patch)
tree21e9dd765e4d7d389f55cc0b5c6fca7ddce2ff2d /src/util/string.h
parentMerge #19841: Implement Keccak and SHA3_256 (diff)
downloaddiscoin-d2bb681f96fb327b4c4d5b2b113692ca22fdffbf.tar.xz
discoin-d2bb681f96fb327b4c4d5b2b113692ca22fdffbf.zip
util: move HasPrefix() so it can be reused
Move the function `HasPrefix()` from `netaddress.cpp` to `util/string.h` so it can be reused by `CNetAddr` methods (and possibly others).
Diffstat (limited to 'src/util/string.h')
-rw-r--r--src/util/string.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/util/string.h b/src/util/string.h
index cdb41630c..a0c87bd00 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -7,6 +7,8 @@
#include <attributes.h>
+#include <algorithm>
+#include <array>
#include <cstring>
#include <locale>
#include <sstream>
@@ -74,4 +76,15 @@ std::string ToString(const T& t)
return oss.str();
}
+/**
+ * Check whether a container begins with the given prefix.
+ */
+template <typename T1, size_t PREFIX_LEN>
+NODISCARD inline bool HasPrefix(const T1& obj,
+ const std::array<uint8_t, PREFIX_LEN>& prefix)
+{
+ return obj.size() >= PREFIX_LEN &&
+ std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
+}
+
#endif // BITCOIN_UTIL_STRENCODINGS_H