mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge remote-tracking branch 'origin/develop' into develop_060
This commit is contained in:
+53
-18
@@ -23,32 +23,59 @@
|
||||
#include <libdevcore/Exceptions.h>
|
||||
#include <libdevcore/Assertions.h>
|
||||
#include <libdevcore/Keccak256.h>
|
||||
#include <libdevcore/FixedHash.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
static char const* upperHexChars = "0123456789ABCDEF";
|
||||
static char const* lowerHexChars = "0123456789abcdef";
|
||||
|
||||
}
|
||||
|
||||
string dev::toHex(uint8_t _data, HexCase _case)
|
||||
{
|
||||
assertThrow(_case != HexCase::Mixed, BadHexCase, "Mixed case can only be used for byte arrays.");
|
||||
|
||||
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
|
||||
|
||||
return std::string{
|
||||
chars[(unsigned(_data) / 16) & 0xf],
|
||||
chars[unsigned(_data) & 0xf]
|
||||
};
|
||||
}
|
||||
|
||||
string dev::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
|
||||
{
|
||||
std::ostringstream ret;
|
||||
if (_prefix == HexPrefix::Add)
|
||||
ret << "0x";
|
||||
std::string ret(_data.size() * 2 + (_prefix == HexPrefix::Add ? 2 : 0), 0);
|
||||
|
||||
size_t i = 0;
|
||||
if (_prefix == HexPrefix::Add)
|
||||
{
|
||||
ret[i++] = '0';
|
||||
ret[i++] = 'x';
|
||||
}
|
||||
|
||||
// Mixed case will be handled inside the loop.
|
||||
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
|
||||
int rix = _data.size() - 1;
|
||||
for (uint8_t c: _data)
|
||||
{
|
||||
// switch hex case every four hexchars
|
||||
auto hexcase = std::nouppercase;
|
||||
if (_case == HexCase::Upper)
|
||||
hexcase = std::uppercase;
|
||||
else if (_case == HexCase::Mixed)
|
||||
hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase;
|
||||
if (_case == HexCase::Mixed)
|
||||
chars = (rix-- & 2) == 0 ? lowerHexChars : upperHexChars;
|
||||
|
||||
ret << std::hex << hexcase << std::setfill('0') << std::setw(2) << size_t(c);
|
||||
ret[i++] = chars[(unsigned(c) / 16) & 0xf];
|
||||
ret[i++] = chars[unsigned(c) & 0xf];
|
||||
}
|
||||
assertThrow(i == ret.size(), Exception, "");
|
||||
|
||||
return ret.str();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int dev::fromHex(char _i, WhenError _throw)
|
||||
@@ -60,7 +87,7 @@ int dev::fromHex(char _i, WhenError _throw)
|
||||
if (_i >= 'A' && _i <= 'F')
|
||||
return _i - 'A' + 10;
|
||||
if (_throw == WhenError::Throw)
|
||||
BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i));
|
||||
assertThrow(false, BadHexCharacter, to_string(_i));
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
@@ -73,22 +100,18 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw)
|
||||
|
||||
if (_s.size() % 2)
|
||||
{
|
||||
int h = fromHex(_s[s++], WhenError::DontThrow);
|
||||
int h = fromHex(_s[s++], _throw);
|
||||
if (h != -1)
|
||||
ret.push_back(h);
|
||||
else if (_throw == WhenError::Throw)
|
||||
BOOST_THROW_EXCEPTION(BadHexCharacter());
|
||||
else
|
||||
return bytes();
|
||||
}
|
||||
for (unsigned i = s; i < _s.size(); i += 2)
|
||||
{
|
||||
int h = fromHex(_s[i], WhenError::DontThrow);
|
||||
int l = fromHex(_s[i + 1], WhenError::DontThrow);
|
||||
int h = fromHex(_s[i], _throw);
|
||||
int l = fromHex(_s[i + 1], _throw);
|
||||
if (h != -1 && l != -1)
|
||||
ret.push_back((uint8_t)(h * 16 + l));
|
||||
else if (_throw == WhenError::Throw)
|
||||
BOOST_THROW_EXCEPTION(BadHexCharacter());
|
||||
else
|
||||
return bytes();
|
||||
}
|
||||
@@ -155,3 +178,15 @@ bool dev::isValidDecimal(string const& _string)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns a quoted string if all characters are printable ASCII chars,
|
||||
// or its hex representation otherwise.
|
||||
std::string dev::formatAsStringOrNumber(std::string const& _value)
|
||||
{
|
||||
if (_value.length() <= 32)
|
||||
for (auto const& c: _value)
|
||||
if (c <= 0x1f || c >= 0x7f || c == '"')
|
||||
return "0x" + h256(_value, h256::AlignLeft).hex();
|
||||
|
||||
return "\"" + _value + "\"";
|
||||
}
|
||||
|
||||
+17
-16
@@ -169,9 +169,12 @@ enum class HexCase
|
||||
Mixed = 2,
|
||||
};
|
||||
|
||||
/// Convert a series of bytes to the corresponding string of hex duplets.
|
||||
/// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte.
|
||||
/// @example toHex("A\x69") == "4169"
|
||||
/// Convert a single byte to a string of hex characters (of length two),
|
||||
/// optionally with uppercase hex letters.
|
||||
std::string toHex(uint8_t _data, HexCase _case = HexCase::Lower);
|
||||
|
||||
/// Convert a series of bytes to the corresponding string of hex duplets,
|
||||
/// optionally with "0x" prefix and with uppercase hex letters.
|
||||
std::string toHex(bytes const& _data, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower);
|
||||
|
||||
/// Converts a (printable) ASCII hex character into the corresponding integer value.
|
||||
@@ -246,10 +249,6 @@ inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
|
||||
toBigEndian(_val, ret);
|
||||
return ret;
|
||||
}
|
||||
inline bytes toCompactBigEndian(uint8_t _val, unsigned _min = 0)
|
||||
{
|
||||
return (_min || _val) ? bytes{ _val } : bytes{};
|
||||
}
|
||||
|
||||
/// Convenience function for conversion of a u256 to hex
|
||||
inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
|
||||
@@ -258,13 +257,18 @@ inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
|
||||
return (prefix == HexPrefix::Add) ? "0x" + str : str;
|
||||
}
|
||||
|
||||
inline std::string toCompactHexWithPrefix(u256 const& _value)
|
||||
{
|
||||
return toHex(toCompactBigEndian(_value, 1), HexPrefix::Add);
|
||||
}
|
||||
|
||||
/// Returns decimal representation for small numbers and hex for large numbers.
|
||||
inline std::string formatNumber(bigint const& _value)
|
||||
{
|
||||
if (_value < 0)
|
||||
return "-" + formatNumber(-_value);
|
||||
if (_value > 0x1000000)
|
||||
return toHex(toCompactBigEndian(_value), HexPrefix::Add);
|
||||
return toHex(toCompactBigEndian(_value, 1), HexPrefix::Add);
|
||||
else
|
||||
return _value.str();
|
||||
}
|
||||
@@ -272,17 +276,11 @@ inline std::string formatNumber(bigint const& _value)
|
||||
inline std::string formatNumber(u256 const& _value)
|
||||
{
|
||||
if (_value > 0x1000000)
|
||||
return toHex(toCompactBigEndian(_value), HexPrefix::Add);
|
||||
return toCompactHexWithPrefix(_value);
|
||||
else
|
||||
return _value.str();
|
||||
}
|
||||
|
||||
inline std::string toCompactHexWithPrefix(u256 val)
|
||||
{
|
||||
std::ostringstream ret;
|
||||
ret << std::hex << val;
|
||||
return "0x" + ret.str();
|
||||
}
|
||||
|
||||
// Algorithms for string and string-like collections.
|
||||
|
||||
@@ -337,7 +335,6 @@ void iterateReplacing(std::vector<T>& _vector, F const& _f)
|
||||
_vector = std::move(modifiedVector);
|
||||
}
|
||||
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename T, typename F, std::size_t... I>
|
||||
@@ -402,6 +399,10 @@ std::string getChecksummedAddress(std::string const& _addr);
|
||||
bool isValidHex(std::string const& _string);
|
||||
bool isValidDecimal(std::string const& _string);
|
||||
|
||||
/// @returns a quoted string if all characters are printable ASCII chars,
|
||||
/// or its hex representation otherwise.
|
||||
std::string formatAsStringOrNumber(std::string const& _value);
|
||||
|
||||
template<typename Container, typename Compare>
|
||||
bool containerEqual(Container const& _lhs, Container const& _rhs, Compare&& _compare)
|
||||
{
|
||||
|
||||
@@ -46,11 +46,11 @@ private:
|
||||
|
||||
DEV_SIMPLE_EXCEPTION(InvalidAddress);
|
||||
DEV_SIMPLE_EXCEPTION(BadHexCharacter);
|
||||
DEV_SIMPLE_EXCEPTION(BadHexCase);
|
||||
DEV_SIMPLE_EXCEPTION(FileError);
|
||||
DEV_SIMPLE_EXCEPTION(DataTooLong);
|
||||
|
||||
// error information to be added to exceptions
|
||||
using errinfo_invalidSymbol = boost::error_info<struct tag_invalidSymbol, char>;
|
||||
using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <libdevcore/IpfsHash.h>
|
||||
|
||||
#include <libdevcore/Assertions.h>
|
||||
#include <libdevcore/Exceptions.h>
|
||||
#include <libdevcore/picosha2.h>
|
||||
#include <libdevcore/CommonData.h>
|
||||
@@ -55,11 +56,7 @@ string base58Encode(bytes const& _data)
|
||||
|
||||
bytes dev::ipfsHash(string _data)
|
||||
{
|
||||
if (_data.length() >= 1024 * 256)
|
||||
BOOST_THROW_EXCEPTION(
|
||||
DataTooLong() <<
|
||||
errinfo_comment("Ipfs hash for large (chunked) files not yet implemented.")
|
||||
);
|
||||
assertThrow(_data.length() < 1024 * 256, DataTooLong, "IPFS hash for large (chunked) files not yet implemented.");
|
||||
|
||||
bytes lengthAsVarint = varintEncoding(_data.size());
|
||||
|
||||
|
||||
+30
-98
@@ -20,109 +20,41 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
|
||||
namespace dev
|
||||
{
|
||||
|
||||
/// Generic visitor used as follows:
|
||||
/// std::visit(GenericVisitor<Class1, Class2>(
|
||||
/// [](Class1& _c) { _c.f(); },
|
||||
/// [](Class2& _c) { _c.g(); }
|
||||
/// ), variant);
|
||||
/// This one does not have a fallback and will fail at
|
||||
/// compile-time if you do not specify all variants.
|
||||
/**
|
||||
* Generic visitor used as follows:
|
||||
* std::visit(GenericVisitor{
|
||||
* [](Class1& _c) { _c.f(); },
|
||||
* [](Class2& _c) { _c.g(); }
|
||||
* }, variant);
|
||||
* This one does not have a fallback and will fail at
|
||||
* compile-time if you do not specify all variants.
|
||||
*
|
||||
* Fallback with no return (it will not fail if you do not specify all variants):
|
||||
* std::visit(GenericVisitor{
|
||||
* VisitorFallback<>{},
|
||||
* [](Class1& _c) { _c.f(); },
|
||||
* [](Class2& _c) { _c.g(); }
|
||||
* }, variant);
|
||||
*
|
||||
* Fallback with return type R (the fallback returns `R{}`:
|
||||
* std::visit(GenericVisitor{
|
||||
* VisitorFallback<R>{},
|
||||
* [](Class1& _c) { _c.f(); },
|
||||
* [](Class2& _c) { _c.g(); }
|
||||
* }, variant);
|
||||
*/
|
||||
|
||||
template <class...>
|
||||
struct GenericVisitor{};
|
||||
template <typename...> struct VisitorFallback;
|
||||
|
||||
template <class Visitable, class... Others>
|
||||
struct GenericVisitor<Visitable, Others...>: public GenericVisitor<Others...>
|
||||
{
|
||||
using GenericVisitor<Others...>::operator ();
|
||||
explicit GenericVisitor(
|
||||
std::function<void(Visitable&)> _visitor,
|
||||
std::function<void(Others&)>... _otherVisitors
|
||||
):
|
||||
GenericVisitor<Others...>(std::move(_otherVisitors)...),
|
||||
m_visitor(std::move(_visitor))
|
||||
{}
|
||||
template <typename R>
|
||||
struct VisitorFallback<R> { template<typename T> R operator()(T&&) const { return {}; } };
|
||||
|
||||
void operator()(Visitable& _v) const { m_visitor(_v); }
|
||||
|
||||
std::function<void(Visitable&)> m_visitor;
|
||||
};
|
||||
template <>
|
||||
struct GenericVisitor<>: public boost::static_visitor<> {
|
||||
void operator()() const {}
|
||||
};
|
||||
|
||||
/// Generic visitor with fallback:
|
||||
/// std::visit(GenericFallbackVisitor<Class1, Class2>(
|
||||
/// [](Class1& _c) { _c.f(); },
|
||||
/// [](Class2& _c) { _c.g(); }
|
||||
/// ), variant);
|
||||
/// This one DOES have a fallback and will NOT fail at
|
||||
/// compile-time if you do not specify all variants.
|
||||
|
||||
template <class...>
|
||||
struct GenericFallbackVisitor{};
|
||||
|
||||
template <class Visitable, class... Others>
|
||||
struct GenericFallbackVisitor<Visitable, Others...>: public GenericFallbackVisitor<Others...>
|
||||
{
|
||||
explicit GenericFallbackVisitor(
|
||||
std::function<void(Visitable&)> _visitor,
|
||||
std::function<void(Others&)>... _otherVisitors
|
||||
):
|
||||
GenericFallbackVisitor<Others...>(std::move(_otherVisitors)...),
|
||||
m_visitor(std::move(_visitor))
|
||||
{}
|
||||
|
||||
using GenericFallbackVisitor<Others...>::operator ();
|
||||
void operator()(Visitable& _v) const { m_visitor(_v); }
|
||||
|
||||
std::function<void(Visitable&)> m_visitor;
|
||||
};
|
||||
template <>
|
||||
struct GenericFallbackVisitor<>: public boost::static_visitor<> {
|
||||
template <class T>
|
||||
void operator()(T&) const { }
|
||||
};
|
||||
|
||||
/// Generic visitor with fallback that can return a value:
|
||||
/// std::visit(GenericFallbackReturnsVisitor<ReturnType, Class1, Class2>(
|
||||
/// [](Class1& _c) { return _c.f(); },
|
||||
/// [](Class2& _c) { return _c.g(); }
|
||||
/// ), variant);
|
||||
/// This one DOES have a fallback and will NOT fail at
|
||||
/// compile-time if you do not specify all variants.
|
||||
/// The fallback {}-constructs the return value.
|
||||
|
||||
template <class R, class...>
|
||||
struct GenericFallbackReturnsVisitor{};
|
||||
|
||||
template <class R, class Visitable, class... Others>
|
||||
struct GenericFallbackReturnsVisitor<R, Visitable, Others...>: public GenericFallbackReturnsVisitor<R, Others...>
|
||||
{
|
||||
explicit GenericFallbackReturnsVisitor(
|
||||
std::function<R(Visitable&)> _visitor,
|
||||
std::function<R(Others&)>... _otherVisitors
|
||||
):
|
||||
GenericFallbackReturnsVisitor<R, Others...>(std::move(_otherVisitors)...),
|
||||
m_visitor(std::move(_visitor))
|
||||
{}
|
||||
|
||||
using GenericFallbackReturnsVisitor<R, Others...>::operator ();
|
||||
R operator()(Visitable& _v) const { return m_visitor(_v); }
|
||||
|
||||
std::function<R(Visitable&)> m_visitor;
|
||||
};
|
||||
template <class R>
|
||||
struct GenericFallbackReturnsVisitor<R>: public boost::static_visitor<R> {
|
||||
template <class T>
|
||||
R operator()(T&) const { return {}; }
|
||||
};
|
||||
template<>
|
||||
struct VisitorFallback<> { template<typename T> void operator()(T&&) const {} };
|
||||
|
||||
template <typename... Visitors> struct GenericVisitor: Visitors... { using Visitors::operator()...; };
|
||||
template <typename... Visitors> GenericVisitor(Visitors...) -> GenericVisitor<Visitors...>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user