Fixing conversion warnings in CommonData.cpp

Co-authored-by: Kamil Śliwak <kamil.sliwak@codepoets.it>
This commit is contained in:
Djordje Mijovic
2020-06-11 08:26:57 +02:00
co-authored by Kamil Śliwak
parent d7f29a33b9
commit c8759b8031
2 changed files with 42 additions and 8 deletions
+36 -2
View File
@@ -64,10 +64,44 @@ public:
FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); }
/// Explicitly construct, copying from a byte array.
explicit FixedHash(bytes const& _b, ConstructFromHashType _t = FailIfDifferent) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N)); else { m_data.fill(0); if (_t != FailIfDifferent) { auto c = std::min<unsigned>(_b.size(), N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } }
explicit FixedHash(bytes const& _array, ConstructFromHashType _sizeMismatchBehavior = FailIfDifferent)
{
if (_array.size() == N)
memcpy(m_data.data(), _array.data(), _array.size());
else
{
m_data.fill(0);
if (_sizeMismatchBehavior != FailIfDifferent)
{
auto bytesToCopy = std::min<size_t>(_array.size(), N);
for (size_t i = 0; i < bytesToCopy; ++i)
if (_sizeMismatchBehavior == AlignRight)
m_data[N - 1 - i] = _array[_array.size() - 1 - i];
else
m_data[i] = _array[i];
}
}
}
/// Explicitly construct, copying from a byte array.
explicit FixedHash(bytesConstRef _b, ConstructFromHashType _t = FailIfDifferent) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N)); else { m_data.fill(0); if (_t != FailIfDifferent) { auto c = std::min<unsigned>(_b.size(), N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } }
explicit FixedHash(bytesConstRef _b, ConstructFromHashType _t = FailIfDifferent)
{
if (_b.size() == N)
memcpy(m_data.data(), _b.data(), std::min<size_t>(_b.size(), N));
else
{
m_data.fill(0);
if (_t != FailIfDifferent)
{
auto c = std::min<size_t>(_b.size(), N);
for (size_t i = 0; i < c; ++i)
if (_t == AlignRight)
m_data[N - 1 - i] = _b[_b.size() - 1 - i];
else
m_data[i] = _b[i];
}
}
}
/// Explicitly construct, copying from a string.
explicit FixedHash(std::string const& _s, ConstructFromStringType _t = FromHex, ConstructFromHashType _ht = FailIfDifferent): FixedHash(_t == FromHex ? fromHex(_s, WhenError::Throw) : solidity::util::asBytes(_s), _ht) {}