mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
refactor: Make HexStr take a span
Make HexStr take a span of bytes, instead of an awkward pair of templated iterators.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#define BITCOIN_UTIL_STRENCODINGS_H
|
||||
|
||||
#include <attributes.h>
|
||||
#include <span.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
@@ -119,27 +120,11 @@ NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
|
||||
*/
|
||||
NODISCARD bool ParseDouble(const std::string& str, double *out);
|
||||
|
||||
template<typename T>
|
||||
std::string HexStr(const T itbegin, const T itend)
|
||||
{
|
||||
std::string rv;
|
||||
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
rv.reserve(std::distance(itbegin, itend) * 2);
|
||||
for(T it = itbegin; it < itend; ++it)
|
||||
{
|
||||
unsigned char val = (unsigned char)(*it);
|
||||
rv.push_back(hexmap[val>>4]);
|
||||
rv.push_back(hexmap[val&15]);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline std::string HexStr(const T& vch)
|
||||
{
|
||||
return HexStr(vch.begin(), vch.end());
|
||||
}
|
||||
/**
|
||||
* Convert a span of bytes to a lower-case hexadecimal string.
|
||||
*/
|
||||
std::string HexStr(const Span<const uint8_t> s);
|
||||
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
|
||||
|
||||
/**
|
||||
* Format a paragraph of text to a fixed width, adding spaces for
|
||||
|
||||
Reference in New Issue
Block a user