28#include <unordered_set>
43template<
class Iterator>
45toHex(Iterator _it, Iterator _end, std::string _prefix)
47 typedef std::iterator_traits<Iterator> traits;
48 static_assert(
sizeof(
typename traits::value_type) == 1,
"toHex needs byte-sized element type");
50 static char const* hexdigits =
"0123456789abcdef";
51 size_t off = _prefix.size();
52 std::string hex(std::distance(_it, _end) * 2 + off,
'0');
53 hex.replace(0, off, _prefix);
54 for (; _it != _end; _it++) {
55 hex[off++] = hexdigits[(*_it >> 4) & 0x0f];
56 hex[off++] = hexdigits[*_it & 0x0f];
67 return toHex(_data.begin(), _data.end(),
"");
76 return toHex(_data.begin(), _data.end(),
"0x");
86bool isHex(std::string
const& _s)
noexcept;
93 return (_hash.size() == T::size * 2
94 || (_hash.size() == T::size * 2 + 2 && _hash.substr(0, 2) ==
"0x"))
103 return std::string((
char const*) _b.data(), (
char const*) (_b.data() + _b.size()));
111 return std::string((
char const*) _b.
data(), (
char const*) (_b.
data() + _b.
size()));
118 return bytes((uint8_t
const*) _b.data(), (uint8_t
const*) (_b.data() + _b.size()));
131template<
class T,
class _In>
136 for (
auto i : _bytes)
137 ret = (T)((ret << 8) | (uint8_t)(
typename std::make_unsigned<
decltype(i)>::type) i);
144 return (_min || _val) ?
bytes {_val} :
bytes {};
152template<
class T,
class _U>
156 unsigned s = std::min<unsigned>(_t.size(), _u.size());
157 for (
unsigned i = 0;; ++i)
158 if (i == s || _t[i] != _u[i])
169 static_assert(std::is_pod<typename T::value_type>::value,
"");
170 memmove(_t.data(), _t.data() + _elements, (_t.size() - _elements) *
sizeof(_t[0]));
171 _t.resize(_t.size() - _elements);
176template<
class T,
class _U>
180 static_assert(std::is_pod<typename T::value_type>::value,
"");
182 memmove(_t.data() + 1, _t.data(), (_t.size() - 1) *
sizeof(_e));
188inline std::vector<T>&
189operator+=(std::vector<
typename std::enable_if<std::is_pod<T>::value, T>::type>& _a,
190 std::vector<T>
const& _b)
193 _a.resize(_a.size() + _b.size());
194 memcpy(_a.data() + s, _b.data(), _b.size() *
sizeof(T));
200inline std::vector<T>&
201operator+=(std::vector<
typename std::enable_if<!std::is_pod<T>::value, T>::type>& _a,
202 std::vector<T>
const& _b)
204 _a.reserve(_a.size() + _b.size());
211template<
class T,
class U>
215 for (
auto const& i : _b)
221template<
class T,
class U>
222std::unordered_set<T>&
225 for (
auto const& i : _b)
231template<
class T,
class U>
235 for (
auto const& i : _b)
241template<
class T,
class U>
249template<
class T,
class U>
257template<
class T,
class U>
267operator+(std::vector<T>
const& _a, std::vector<T>
const& _b)
269 std::vector<T> ret(_a);
273template<
class T,
class U>
278 for (
auto const& i : _m)
279 ret.push_back(i.first);
283template<
class T,
class U>
285keysOf(std::unordered_map<T, U>
const& _m)
288 for (
auto const& i : _m)
289 ret.push_back(i.first);
293template<
class T,
class U>
298 ret.reserve(_m.size());
299 for (
auto const& i : _m)
300 ret.push_back(i.second);
304template<
class T,
class U>
309 ret.reserve(_m.size());
310 for (
auto const& i : _m)
311 ret.push_back(i.second);
315template<
class T,
class V>
319 return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
A modifiable reference to an existing object or vector in memory.
T fromBigEndian(_In const &_bytes)
Converts a big-endian byte-stream represented on a templated collection to a templated integer value.
std::set< T > operator+(std::set< T > _a, U const &_b)
Insert the contents of a container into a set.
bool isHex(std::string const &_s) noexcept
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.
vector_ref< uint8_t const > bytesConstRef
bytes asNibbles(bytesConstRef const &_s)
void trimFront(T &_t, unsigned _elements)
Trims a given number of elements from the front of a collection.
bool contains(T const &_t, V const &_v)
static bool isHash(std::string const &_hash)
std::vector< uint8_t > bytes
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
std::string toHexPrefixed(T const &_data)
void pushFront(T &_t, _U _e)
Pushes an element on to the front of a collection.
std::vector< U > valuesOf(std::map< T, U > const &_m)
unsigned commonPrefix(T const &_t, _U const &_u)
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
std::string toHex(Iterator _it, Iterator _end, std::string _prefix)
bytes toCompactBigEndian(uint8_t _val, unsigned _min=0)
std::vector< T > keysOf(std::map< T, U > const &_m)