diff options
author | pasta <pasta@dashboost.org> | 2022-10-19 15:16:04 -0500 |
---|---|---|
committer | pasta <pasta@dashboost.org> | 2022-12-10 14:34:44 -0600 |
commit | 935acdcc79d1dc5ac04a83b92e5919ddbfa29329 (patch) | |
tree | d38d0afc26dd7d85a346a676d6ab93ca2e7a6fba /src/uint256.cpp | |
parent | 1ea02791f3d81c7716d9ea455971203f74d7a107 (diff) |
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
Diffstat (limited to 'src/uint256.cpp')
-rw-r--r-- | src/uint256.cpp | 15 |
1 files changed, 2 insertions, 13 deletions
diff --git a/src/uint256.cpp b/src/uint256.cpp index cd9cbb566a..7f81c3c448 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -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*); |