convert classic for-loops to range-for loops. revert use of CHAR_BIT macro (we use fixed width types, not char)

This commit is contained in:
arvidn 2018-11-07 21:36:21 +01:00 committed by Arvid Norberg
parent 0ad3bb139d
commit 30706adfb7
1 changed files with 27 additions and 23 deletions

View File

@ -38,7 +38,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <array> #include <array>
#include <climits> // for CHAR_BIT
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/range/combine.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
@ -70,10 +73,11 @@ namespace aux {
{ {
static_assert(N % 32 == 0, "N must be a multiple of 32"); static_assert(N % 32 == 0, "N must be a multiple of 32");
static constexpr std::size_t number_size = N / 32; static constexpr std::size_t number_size = N / 32;
constexpr static int bits_in_byte = 8;
public: public:
// the size of the hash in bytes // the size of the hash in bytes
static constexpr std::size_t size() noexcept { return N / CHAR_BIT; } static constexpr std::size_t size() noexcept { return N / bits_in_byte; }
// constructs an all-zero digest // constructs an all-zero digest
digest32() noexcept { clear(); } digest32() noexcept { clear(); }
@ -82,7 +86,7 @@ namespace aux {
digest32& operator=(digest32 const&) noexcept = default; digest32& operator=(digest32 const&) noexcept = default;
// returns an all-F digest. i.e. the maximum value // returns an all-F digest. i.e. the maximum value
// representable by an N bit number (N/CHAR_BIT bytes). This is // representable by an N bit number (N/8 bytes). This is
// a static member function. // a static member function.
static digest32 (max)() noexcept static digest32 (max)() noexcept
{ {
@ -92,7 +96,7 @@ namespace aux {
} }
// returns an all-zero digest. i.e. the minimum value // returns an all-zero digest. i.e. the minimum value
// representable by an N bit number (N/CHAR_BIT bytes). This is // representable by an N bit number (N/8 bytes). This is
// a static member function. // a static member function.
static digest32 (min)() noexcept static digest32 (min)() noexcept
{ {
@ -101,8 +105,8 @@ namespace aux {
return ret; return ret;
} }
// copies N/CHAR_BIT bytes from the pointer provided, into the digest. // copies N/8 bytes from the pointer provided, into the digest.
// The passed in string MUST be at least N/CHAR_BIT bytes. 0-terminators // The passed in string MUST be at least N/8 bytes. 0-terminators
// are ignored, ``s`` is treated like a raw memory buffer. // are ignored, ``s`` is treated like a raw memory buffer.
explicit digest32(char const* s) noexcept explicit digest32(char const* s) noexcept
{ {
@ -122,7 +126,7 @@ namespace aux {
} }
void assign(span<char const> s) noexcept void assign(span<char const> s) noexcept
{ {
TORRENT_ASSERT(s.size() >= N / CHAR_BIT); TORRENT_ASSERT(s.size() >= N / bits_in_byte);
std::size_t const sl = s.size() < size() ? s.size() : size(); std::size_t const sl = s.size() < size() ? s.size() : size();
std::memcpy(m_number.data(), s.data(), sl); std::memcpy(m_number.data(), s.data(), sl);
} }
@ -137,9 +141,8 @@ namespace aux {
// return true if the digest is all zero. // return true if the digest is all zero.
bool is_all_zeros() const noexcept bool is_all_zeros() const noexcept
{ {
for (auto const& v : m_number) return std::all_of(m_number.begin(), m_number.end()
if (v != 0) return false; , [](std::uint32_t v) { return v == 0; });
return true;
} }
// shift left ``n`` bits. // shift left ``n`` bits.
@ -167,10 +170,10 @@ namespace aux {
} }
bool operator<(digest32 const& n) const noexcept bool operator<(digest32 const& n) const noexcept
{ {
for (std::size_t i = 0; i < number_size; ++i) for (auto const v : boost::combine(m_number, n.m_number))
{ {
std::uint32_t const lhs = aux::network_to_host(m_number[i]); std::uint32_t const lhs = aux::network_to_host(boost::get<0>(v));
std::uint32_t const rhs = aux::network_to_host(n.m_number[i]); std::uint32_t const rhs = aux::network_to_host(boost::get<1>(v));
if (lhs < rhs) return true; if (lhs < rhs) return true;
if (lhs > rhs) return false; if (lhs > rhs) return false;
} }
@ -186,8 +189,8 @@ namespace aux {
digest32 operator~() const noexcept digest32 operator~() const noexcept
{ {
digest32 ret; digest32 ret;
for (std::size_t i = 0; i < number_size; ++i) for (auto const v : boost::combine(m_number, ret.m_number))
ret.m_number[i] = ~m_number[i]; boost::get<1>(v) = ~boost::get<0>(v);
return ret; return ret;
} }
@ -202,8 +205,9 @@ namespace aux {
// in-place bit-wise XOR with the passed in digest. // in-place bit-wise XOR with the passed in digest.
digest32& operator^=(digest32 const& n) noexcept digest32& operator^=(digest32 const& n) noexcept
{ {
for (std::size_t i = 0; i < number_size; ++i)
m_number[i] ^= n.m_number[i]; for (auto const v : boost::combine(m_number, n.m_number))
boost::get<0>(v) ^= boost::get<1>(v);
return *this; return *this;
} }
@ -218,16 +222,16 @@ namespace aux {
// in-place bit-wise AND of the passed in digest // in-place bit-wise AND of the passed in digest
digest32& operator&=(digest32 const& n) noexcept digest32& operator&=(digest32 const& n) noexcept
{ {
for (std::size_t i = 0; i < number_size; ++i) for (auto const v : boost::combine(m_number, n.m_number))
m_number[i] &= n.m_number[i]; boost::get<0>(v) &= boost::get<1>(v);
return *this; return *this;
} }
// in-place bit-wise OR of the two digests. // in-place bit-wise OR of the two digests.
digest32& operator|=(digest32 const& n) noexcept digest32& operator|=(digest32 const& n) noexcept
{ {
for (std::size_t i = 0; i < number_size; ++i) for (auto const v : boost::combine(m_number, n.m_number))
m_number[i] |= n.m_number[i]; boost::get<0>(v) |= boost::get<1>(v);
return *this; return *this;
} }
@ -257,8 +261,8 @@ namespace aux {
iterator end() iterator end()
{ return reinterpret_cast<std::uint8_t*>(m_number.data()) + size(); } { return reinterpret_cast<std::uint8_t*>(m_number.data()) + size(); }
// return a copy of the N/CHAR_BIT bytes representing the digest as a std::string. // return a copy of the N/8 bytes representing the digest as a std::string.
// It's still a binary string with N/CHAR_BIT binary characters. // It's still a binary string with N/8 binary characters.
std::string to_string() const std::string to_string() const
{ {
return std::string(reinterpret_cast<char const*>(m_number.data()), size()); return std::string(reinterpret_cast<char const*>(m_number.data()), size());