From d2bb681f96fb327b4c4d5b2b113692ca22fdffbf Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Mon, 31 Aug 2020 10:39:00 +0200 Subject: 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). --- src/util/string.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/util') diff --git a/src/util/string.h b/src/util/string.h index cdb41630c6..a0c87bd00c 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -7,6 +7,8 @@ #include +#include +#include #include #include #include @@ -74,4 +76,15 @@ std::string ToString(const T& t) return oss.str(); } +/** + * Check whether a container begins with the given prefix. + */ +template +NODISCARD inline bool HasPrefix(const T1& obj, + const std::array& prefix) +{ + return obj.size() >= PREFIX_LEN && + std::equal(std::begin(prefix), std::end(prefix), std::begin(obj)); +} + #endif // BITCOIN_UTIL_STRENCODINGS_H -- cgit v1.2.3