28 #include <unordered_set> 29 #include <type_traits> 56 std::ostringstream ret;
69 bool isHex(std::string
const& _s) noexcept;
72 template <
class T>
static bool isHash(std::string
const& _hash)
74 return (_hash.size() ==
T::size * 2 || (_hash.size() ==
T::size * 2 + 2 && _hash.substr(0, 2) ==
"0x")) &&
isHex(_hash);
81 return std::string((
char const*)_b.data(), (
char const*)(_b.data() + _b.size()));
88 return std::string((
char const*)_b.
data(), (
char const*)(_b.
data() + _b.
size()));
94 return bytes((
byte const*)_b.data(), (
byte const*)(_b.data() + _b.size()));
109 template <
class T,
class Out>
112 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
113 for (
auto i = o_out.size(); i != 0; _val >>= 8, i--)
115 T v = _val & (
T)0xff;
116 o_out[i - 1] = (
typename Out::value_type)(uint8_t)v;
123 template <
class T,
class _In>
128 ret = (
T)((ret << 8) | (
byte)(
typename std::make_unsigned<decltype(i)>
::type)i);
143 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
145 for (
T v = _val; v; ++i, v >>= 8) {}
146 bytes ret(std::max<unsigned>(_min, i), 0);
152 return (_min || _val) ?
bytes{ _val } :
bytes{};
160 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
162 for (
T v = _val; v; ++i, v >>= 8) {}
163 std::string ret(std::max<unsigned>(_min, i),
'\0');
185 std::string
escaped(std::string
const& _s,
bool _all =
true);
190 template <
class T,
class _U>
193 unsigned s = std::min<unsigned>(_t.size(), _u.size());
194 for (
unsigned i = 0;; ++i)
195 if (i == s || _t[i] != _u[i])
207 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
209 for (; _i != 0; ++i, _i >>= 8) {}
218 static_assert(std::is_pod<typename T::value_type>::value,
"");
219 memmove(_t.data(), _t.data() + _elements, (_t.size() - _elements) *
sizeof(_t[0]));
220 _t.resize(_t.size() - _elements);
225 template <
class T,
class _U>
228 static_assert(std::is_pod<typename T::value_type>::value,
"");
230 memmove(_t.data() + 1, _t.data(), (_t.size() - 1) *
sizeof(_e));
236 inline std::vector<T>&
operator+=(std::vector<
typename std::enable_if<std::is_pod<T>::value,
T>::
type>& _a, std::vector<T>
const& _b)
239 _a.resize(_a.size() + _b.size());
240 memcpy(_a.data() + s, _b.data(), _b.size() *
sizeof(
T));
247 inline std::vector<T>&
operator+=(std::vector<
typename std::enable_if<!std::is_pod<T>::value,
T>::
type>& _a, std::vector<T>
const& _b)
249 _a.reserve(_a.size() + _b.size());
256 template <
class T,
class U> std::set<T>&
operator+=(std::set<T>& _a, U
const& _b)
258 for (
auto const& i: _b)
264 template <
class T,
class U> std::unordered_set<T>&
operator+=(std::unordered_set<T>& _a, U
const& _b)
266 for (
auto const& i: _b)
272 template <
class T,
class U> std::vector<T>&
operator+=(std::vector<T>& _a, U
const& _b)
274 for (
auto const& i: _b)
280 template <
class T,
class U> std::set<T>
operator+(std::set<T> _a, U
const& _b)
286 template <
class T,
class U> std::unordered_set<T>
operator+(std::unordered_set<T> _a, U
const& _b)
292 template <
class T,
class U> std::vector<T>
operator+(std::vector<T> _a, U
const& _b)
299 inline std::vector<T>
operator+(std::vector<T>
const& _a, std::vector<T>
const& _b)
301 std::vector<T> ret(_a);
307 inline std::set<T>&
operator+=(std::set<T>& _a, std::set<T>
const& _b)
316 inline std::set<T>
operator+(std::set<T>
const& _a, std::set<T>
const& _b)
322 template <
class A,
class B>
323 std::unordered_map<A, B>&
operator+=(std::unordered_map<A, B>& _x, std::unordered_map<A, B>
const& _y)
325 for (
auto const& i: _y)
330 template <
class A,
class B>
331 std::unordered_map<A, B>
operator+(std::unordered_map<A, B>
const& _x, std::unordered_map<A, B>
const& _y)
333 std::unordered_map<A, B> ret(_x);
340 template<
class T,
class U>
341 std::vector<T>
keysOf(std::map<T, U>
const& _m)
344 for (
auto const& i: _m)
345 ret.push_back(i.first);
349 template<
class T,
class U>
350 std::vector<T>
keysOf(std::unordered_map<T, U>
const& _m)
353 for (
auto const& i: _m)
354 ret.push_back(i.first);
358 template<
class T,
class U>
362 ret.reserve(_m.size());
363 for (
auto const& i: _m)
364 ret.push_back(i.second);
368 template<
class T,
class U>
369 std::vector<U>
valuesOf(std::unordered_map<T, U>
const& _m)
372 ret.reserve(_m.size());
373 for (
auto const& i: _m)
374 ret.push_back(i.second);
378 template <
class T,
class V>
381 return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
unsigned commonPrefix(T const &_t, _U const &_u)
std::string toCompactHex(u256 val, HexPrefix prefix=HexPrefix::DontAdd, unsigned _min=0)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
std::vector< T > keysOf(std::map< T, U > const &_m)
void toBigEndian(T _val, Out &o_out)
Converts a templated integer value to the big-endian byte-stream represented on a templated collectio...
std::string randomWord()
Creates a random, printable, word.
void trimFront(T &_t, unsigned _elements)
Trims a given number of elements from the front of a collection.
unsigned bytesRequired(T _i)
Determine bytes required to encode the given integer value.
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
std::set< T > operator+(std::set< T > _a, U const &_b)
Insert the contents of a container into a set.
std::vector< U > valuesOf(std::map< T, U > const &_m)
std::string escaped(std::string const &_s, bool _all=true)
Escapes a string into the C-string representation.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
bool contains(T const &_t, V const &_v)
bool isHex(std::string const &_s) noexcept
std::string toCompactBigEndianString(T _val, unsigned _min=0)
Convenience function for toBigEndian.
std::vector< byte > bytes
bytes toCompactBigEndian(T _val, unsigned _min=0)
Convenience function for toBigEndian.
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
bytes asNibbles(bytesConstRef const &_s)
std::array< char, 32 > string32
void pushFront(T &_t, _U _e)
Pushes an element on to the front of a collection.
PlatformStyle::TableColorType type
uint8_t const size_t const size
std::string toBigEndianString(u256 _val)
Convenience functions for toBigEndian.
void * memcpy(void *a, const void *b, size_t c)
std::vector< T > & operator+=(std::vector< typename std::enable_if< std::is_pod< T >::value, T >::type > &_a, std::vector< T > const &_b)
Concatenate two vectors of elements of POD types.
T fromBigEndian(_In const &_bytes)
Converts a big-endian byte-stream represented on a templated collection to a templated integer value...
void * memmove(void *a, const void *b, size_t c)
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.