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 || (_hash.size() == T::size * 2 + 2 && _hash.substr(0, 2) ==
"0x"))
102 return std::string((
char const*) _b.data(), (
char const*) (_b.data() + _b.size()));
110 return std::string((
char const*) _b.
data(), (
char const*) (_b.
data() + _b.
size()));
117 return bytes((uint8_t
const*) _b.data(), (uint8_t
const*) (_b.data() + _b.size()));
130template<
class T,
class _In>
135 for (
auto i : _bytes)
136 ret = (T) ((ret << 8) | (uint8_t) (
typename std::make_unsigned<
decltype(i)>::type) i);
143 return (_min || _val) ?
bytes {_val} :
bytes {};
151template<
class T,
class _U>
155 unsigned s = std::min<unsigned>(_t.size(), _u.size());
156 for (
unsigned i = 0;; ++i)
157 if (i == s || _t[i] != _u[i])
168 static_assert(std::is_pod<typename T::value_type>::value,
"");
169 memmove(_t.data(), _t.data() + _elements, (_t.size() - _elements) *
sizeof(_t[0]));
170 _t.resize(_t.size() - _elements);
175template<
class T,
class _U>
179 static_assert(std::is_pod<typename T::value_type>::value,
"");
181 memmove(_t.data() + 1, _t.data(), (_t.size() - 1) *
sizeof(_e));
187inline std::vector<T>&
188operator+=(std::vector<
typename std::enable_if<std::is_pod<T>::value, T>::type>& _a, std::vector<T>
const& _b)
191 _a.resize(_a.size() + _b.size());
192 memcpy(_a.data() + s, _b.data(), _b.size() *
sizeof(T));
198inline std::vector<T>&
199operator+=(std::vector<
typename std::enable_if<!std::is_pod<T>::value, T>::type>& _a, std::vector<T>
const& _b)
201 _a.reserve(_a.size() + _b.size());
208template<
class T,
class U>
212 for (
auto const& i : _b)
218template<
class T,
class U>
219std::unordered_set<T>&
222 for (
auto const& i : _b)
228template<
class T,
class U>
232 for (
auto const& i : _b)
238template<
class T,
class U>
246template<
class T,
class U>
254template<
class T,
class U>
264operator+(std::vector<T>
const& _a, std::vector<T>
const& _b)
266 std::vector<T> ret(_a);
270template<
class T,
class U>
275 for (
auto const& i : _m)
276 ret.push_back(i.first);
280template<
class T,
class U>
282keysOf(std::unordered_map<T, U>
const& _m)
285 for (
auto const& i : _m)
286 ret.push_back(i.first);
290template<
class T,
class U>
295 ret.reserve(_m.size());
296 for (
auto const& i : _m)
297 ret.push_back(i.second);
301template<
class T,
class U>
306 ret.reserve(_m.size());
307 for (
auto const& i : _m)
308 ret.push_back(i.second);
312template<
class T,
class V>
316 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)