mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-24 22:45:41 +01:00
refactor: modernize the implementation of uint256.*
- Constructors of uint256 to utilize Span instead of requiring a std::vector
- converts m_data into a std::array
- Prefers using `WIDTH` instead of `sizeof(m_data)`
- make all the things constexpr
- replace C style functions with c++ equivalents
- memset -> std::fill
- memcpy -> std::copy
Note: In practice, implementations of std::copy avoid multiple assignments and use bulk copy functions such as std::memmove if the value type is TriviallyCopyable and the iterator types satisfy LegacyContiguousIterator. (https://en.cppreference.com/w/cpp/algorithm/copy)
- memcmp -> std::memcmp
This commit is contained in:
@@ -7,15 +7,6 @@
|
||||
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
template <unsigned int BITS>
|
||||
base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
|
||||
{
|
||||
assert(vch.size() == sizeof(m_data));
|
||||
memcpy(m_data, vch.data(), sizeof(m_data));
|
||||
}
|
||||
|
||||
template <unsigned int BITS>
|
||||
std::string base_blob<BITS>::GetHex() const
|
||||
{
|
||||
@@ -29,7 +20,7 @@ std::string base_blob<BITS>::GetHex() const
|
||||
template <unsigned int BITS>
|
||||
void base_blob<BITS>::SetHex(const char* psz)
|
||||
{
|
||||
memset(m_data, 0, sizeof(m_data));
|
||||
std::fill(m_data.begin(), m_data.end(), 0);
|
||||
|
||||
// skip leading spaces
|
||||
while (IsSpace(*psz))
|
||||
@@ -43,7 +34,7 @@ void base_blob<BITS>::SetHex(const char* psz)
|
||||
size_t digits = 0;
|
||||
while (::HexDigit(psz[digits]) != -1)
|
||||
digits++;
|
||||
unsigned char* p1 = (unsigned char*)m_data;
|
||||
unsigned char* p1 = m_data.data();
|
||||
unsigned char* pend = p1 + WIDTH;
|
||||
while (digits > 0 && p1 < pend) {
|
||||
*p1 = ::HexDigit(psz[--digits]);
|
||||
@@ -67,14 +58,12 @@ std::string base_blob<BITS>::ToString() const
|
||||
}
|
||||
|
||||
// Explicit instantiations for base_blob<160>
|
||||
template base_blob<160>::base_blob(const std::vector<unsigned char>&);
|
||||
template std::string base_blob<160>::GetHex() const;
|
||||
template std::string base_blob<160>::ToString() const;
|
||||
template void base_blob<160>::SetHex(const char*);
|
||||
template void base_blob<160>::SetHex(const std::string&);
|
||||
|
||||
// Explicit instantiations for base_blob<256>
|
||||
template base_blob<256>::base_blob(const std::vector<unsigned char>&);
|
||||
template std::string base_blob<256>::GetHex() const;
|
||||
template std::string base_blob<256>::ToString() const;
|
||||
template void base_blob<256>::SetHex(const char*);
|
||||
|
||||
Reference in New Issue
Block a user