Skip to content

Commit d2bb681

Browse files
committed
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).
1 parent a47e596 commit d2bb681

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/netaddress.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
#include <hash.h>
99
#include <tinyformat.h>
10-
#include <util/strencodings.h>
1110
#include <util/asmap.h>
11+
#include <util/strencodings.h>
12+
#include <util/string.h>
1213

1314
#include <algorithm>
1415
#include <array>
@@ -50,13 +51,6 @@ void CNetAddr::SetIP(const CNetAddr& ipIn)
5051
m_addr = ipIn.m_addr;
5152
}
5253

53-
template <typename T1, size_t PREFIX_LEN>
54-
inline bool HasPrefix(const T1& obj, const std::array<uint8_t, PREFIX_LEN>& prefix)
55-
{
56-
return obj.size() >= PREFIX_LEN &&
57-
std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
58-
}
59-
6054
void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
6155
{
6256
assert(ipv6.size() == ADDR_IPV6_SIZE);

src/util/string.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <attributes.h>
99

10+
#include <algorithm>
11+
#include <array>
1012
#include <cstring>
1113
#include <locale>
1214
#include <sstream>
@@ -74,4 +76,15 @@ std::string ToString(const T& t)
7476
return oss.str();
7577
}
7678

79+
/**
80+
* Check whether a container begins with the given prefix.
81+
*/
82+
template <typename T1, size_t PREFIX_LEN>
83+
NODISCARD inline bool HasPrefix(const T1& obj,
84+
const std::array<uint8_t, PREFIX_LEN>& prefix)
85+
{
86+
return obj.size() >= PREFIX_LEN &&
87+
std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
88+
}
89+
7790
#endif // BITCOIN_UTIL_STRENCODINGS_H

0 commit comments

Comments
 (0)