mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-03 09:43:55 +02:00
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).
This commit is contained in:
@@ -7,8 +7,9 @@
|
|||||||
|
|
||||||
#include <hash.h>
|
#include <hash.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <util/strencodings.h>
|
|
||||||
#include <util/asmap.h>
|
#include <util/asmap.h>
|
||||||
|
#include <util/strencodings.h>
|
||||||
|
#include <util/string.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
@@ -50,13 +51,6 @@ void CNetAddr::SetIP(const CNetAddr& ipIn)
|
|||||||
m_addr = ipIn.m_addr;
|
m_addr = ipIn.m_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T1, size_t PREFIX_LEN>
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
|
void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
|
||||||
{
|
{
|
||||||
assert(ipv6.size() == ADDR_IPV6_SIZE);
|
assert(ipv6.size() == ADDR_IPV6_SIZE);
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#include <attributes.h>
|
#include <attributes.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -74,4 +76,15 @@ std::string ToString(const T& t)
|
|||||||
return oss.str();
|
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
|
#endif // BITCOIN_UTIL_STRENCODINGS_H
|
||||||
|
|||||||
Reference in New Issue
Block a user