mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-15 12:51:00 +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:
parent
a47e596486
commit
d2bb681f96
2 changed files with 15 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue