mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #9083 from ethereum/tohex
Add shortcut to to/fromHex for empty input and fix signedness warning
This commit is contained in:
commit
a3b64fd19d
@ -53,6 +53,9 @@ string solidity::util::toHex(uint8_t _data, HexCase _case)
|
|||||||
|
|
||||||
string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
|
string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
|
||||||
{
|
{
|
||||||
|
if (_data.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
std::string ret(_data.size() * 2 + (_prefix == HexPrefix::Add ? 2 : 0), 0);
|
std::string ret(_data.size() * 2 + (_prefix == HexPrefix::Add ? 2 : 0), 0);
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
@ -64,7 +67,7 @@ string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _cas
|
|||||||
|
|
||||||
// Mixed case will be handled inside the loop.
|
// Mixed case will be handled inside the loop.
|
||||||
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
|
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
|
||||||
int rix = _data.size() - 1;
|
size_t rix = _data.size() - 1;
|
||||||
for (uint8_t c: _data)
|
for (uint8_t c: _data)
|
||||||
{
|
{
|
||||||
// switch hex case every four hexchars
|
// switch hex case every four hexchars
|
||||||
@ -95,6 +98,9 @@ int solidity::util::fromHex(char _i, WhenError _throw)
|
|||||||
|
|
||||||
bytes solidity::util::fromHex(std::string const& _s, WhenError _throw)
|
bytes solidity::util::fromHex(std::string const& _s, WhenError _throw)
|
||||||
{
|
{
|
||||||
|
if (_s.empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
|
unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
|
||||||
std::vector<uint8_t> ret;
|
std::vector<uint8_t> ret;
|
||||||
ret.reserve((_s.size() - s + 1) / 2);
|
ret.reserve((_s.size() - s + 1) / 2);
|
||||||
|
@ -299,7 +299,7 @@ template <class T>
|
|||||||
inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
|
inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
|
||||||
{
|
{
|
||||||
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
|
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
|
||||||
int i = 0;
|
unsigned i = 0;
|
||||||
for (T v = _val; v; ++i, v >>= 8) {}
|
for (T v = _val; v; ++i, v >>= 8) {}
|
||||||
bytes ret(std::max<unsigned>(_min, i), 0);
|
bytes ret(std::max<unsigned>(_min, i), 0);
|
||||||
toBigEndian(_val, ret);
|
toBigEndian(_val, ret);
|
||||||
|
Loading…
Reference in New Issue
Block a user