28 #include <unordered_set>
29 #include <type_traits>
45 template <
class Iterator>
46 std::string
toHex(Iterator _it, Iterator _end, std::string
const& _prefix)
48 typedef std::iterator_traits<Iterator> traits;
49 static_assert(
sizeof(
typename traits::value_type) == 1,
"toHex needs byte-sized element type");
51 static char const* hexdigits =
"0123456789abcdef";
52 size_t off = _prefix.size();
53 std::string hex(std::distance(_it, _end)*2 + off,
'0');
54 hex.replace(0, off, _prefix);
55 for (; _it != _end; _it++)
57 hex[off++] = hexdigits[(*_it >> 4) & 0x0f];
58 hex[off++] = hexdigits[*_it & 0x0f];
65 template <
class T> std::string
toHex(T
const& _data)
67 return toHex(_data.begin(), _data.end(),
"");
74 return toHex(_data.begin(), _data.end(),
"0x");
83 bool isHex(std::string
const& _s) noexcept;
86 template <
class T>
static bool isHash(std::string
const& _hash)
88 return (_hash.size() == T::size * 2 || (_hash.size() == T::size * 2 + 2 && _hash.substr(0, 2) ==
"0x")) &&
isHex(_hash);
95 return std::string((
char const*)_b.data(), (
char const*)(_b.data() + _b.size()));
102 return std::string((
char const*)_b.
data(), (
char const*)(_b.
data() + _b.
size()));
108 return bytes((
byte const*)_b.data(), (
byte const*)(_b.data() + _b.size()));
123 template <
class T,
class Out>
126 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
127 for (
auto i = o_out.size(); i != 0; _val >>= 8, i--)
129 T v = _val & (T)0xff;
130 o_out[i - 1] = (
typename Out::value_type)(uint8_t)v;
137 template <
class T,
class _In>
142 ret = (T)((ret << 8) | (
byte)(
typename std::make_unsigned<decltype(i)>::type)i);
157 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
159 for (T v = _val; v; ++i, v >>= 8) {}
160 bytes ret(std::max<unsigned>(_min, i), 0);
166 return (_min || _val) ?
bytes{ _val } :
bytes{};
174 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
176 for (T v = _val; v; ++i, v >>= 8) {}
177 std::string ret(std::max<unsigned>(_min, i),
'\0');
196 std::string
escaped(std::string
const& _s,
bool _all =
true);
201 template <
class T,
class _U>
204 unsigned s = std::min<unsigned>(_t.size(), _u.size());
205 for (
unsigned i = 0;; ++i)
206 if (i == s || _t[i] != _u[i])
215 static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed,
"only unsigned types or bigint supported");
217 for (; _i != 0; ++i, _i >>= 8) {}
226 static_assert(std::is_pod<typename T::value_type>::value,
"");
227 memmove(_t.data(), _t.data() + _elements, (_t.size() - _elements) *
sizeof(_t[0]));
228 _t.resize(_t.size() - _elements);
233 template <
class T,
class _U>
236 static_assert(std::is_pod<typename T::value_type>::value,
"");
238 memmove(_t.data() + 1, _t.data(), (_t.size() - 1) *
sizeof(_e));
243 template <
class T,
class U>
244 inline std::vector<T>&
operator+=(std::vector<T>& _a, U
const& _b)
246 _a.insert(_a.end(), std::begin(_b), std::end(_b));
251 template <
class T,
class U>
254 _a.insert(std::begin(_b), std::end(_b));
259 template <
class T,
class U>
260 std::unordered_set<T>&
operator+=(std::unordered_set<T>& _a, U
const& _b)
262 _a.insert(std::begin(_b), std::end(_b));
267 template <
class T,
class U> std::set<T>
operator+(std::set<T> _a, U
const& _b)
273 template <
class T,
class U> std::unordered_set<T>
operator+(std::unordered_set<T> _a, U
const& _b)
279 template <
class T,
class U> std::vector<T>
operator+(std::vector<T> _a, U
const& _b)
285 template<
class T,
class U>
286 std::vector<T>
keysOf(std::map<T, U>
const& _m)
289 for (
auto const& i: _m)
290 ret.push_back(i.first);
294 template<
class T,
class U>
295 std::vector<T>
keysOf(std::unordered_map<T, U>
const& _m)
298 for (
auto const& i: _m)
299 ret.push_back(i.first);
303 template<
class T,
class U>
307 ret.reserve(_m.size());
308 for (
auto const& i: _m)
309 ret.push_back(i.second);
313 template<
class T,
class U>
314 std::vector<U>
valuesOf(std::unordered_map<T, U>
const& _m)
317 ret.reserve(_m.size());
318 for (
auto const& i: _m)
319 ret.push_back(i.second);
323 template <
class T,
class V>
326 return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
330 bool contains(std::unordered_set<V>
const& _set, V
const& _v)
332 return _set.find(_v) != _set.end();
335 template <
class K,
class V>
336 bool contains(std::unordered_map<K, V>
const& _map, K
const& _k)
338 return _map.find(_k) != _map.end();
342 bool contains(std::set<V>
const& _set, V
const& _v)
344 return _set.find(_v) != _set.end();