From d41adb4ef41d463a97002f52f4d74f9ed9007bc4 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Mon, 31 Aug 2020 10:39:00 +0200 Subject: [PATCH] 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/netaddress.cpp | 12 +++--------- src/util/string.h | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 1b67dad75658a..c815455ce3144 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -6,9 +6,10 @@ #include "netaddress.h" #include "hash.h" -#include "utilstrencodings.h" -#include "util/asmap.h" #include "tinyformat.h" +#include "util/asmap.h" +#include "util/string.h" +#include "utilstrencodings.h" #include #include @@ -50,13 +51,6 @@ void CNetAddr::SetIP(const CNetAddr& ipIn) m_addr = ipIn.m_addr; } -template -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)); -} - void CNetAddr::SetLegacyIPv6(Span ipv6) { assert(ipv6.size() == ADDR_IPV6_SIZE); diff --git a/src/util/string.h b/src/util/string.h index d07cb04af0adc..5a0c032df1140 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -5,6 +5,10 @@ #ifndef BITCOIN_UTIL_STRING_H #define BITCOIN_UTIL_STRING_H +#include "attributes.h" + +#include +#include #include #include #include @@ -41,4 +45,15 @@ inline bool ValidAsCString(const std::string& str) noexcept return str.size() == strlen(str.c_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