Ring Daemon 16.0.0
Loading...
Searching...
No Matches
CommonData.h
Go to the documentation of this file.
1/*
2 This file is part of cpp-ethereum.
3
4 cpp-ethereum is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 cpp-ethereum is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16*/
24#pragma once
25
26#include <vector>
27#include <algorithm>
28#include <unordered_set>
29#include <type_traits>
30#include <cstring>
31#include <string>
32#include "Common.h"
33
34namespace dev {
35
36// String conversion functions, mainly to/from hex/nibble/byte representations.
37
38enum class WhenError {
39 DontThrow = 0,
40 Throw = 1,
41};
42
43template<class Iterator>
44std::string
45toHex(Iterator _it, Iterator _end, std::string _prefix)
46{
47 typedef std::iterator_traits<Iterator> traits;
48 static_assert(sizeof(typename traits::value_type) == 1, "toHex needs byte-sized element type");
49
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];
57 }
58 return hex;
59}
60
63template<class T>
64std::string
65toHex(T const& _data)
66{
67 return toHex(_data.begin(), _data.end(), "");
68}
69
72template<class T>
73std::string
74toHexPrefixed(T const& _data)
75{
76 return toHex(_data.begin(), _data.end(), "0x");
77}
78
83bytes fromHex(std::string const& _s, WhenError _throw = WhenError::DontThrow);
84
86bool isHex(std::string const& _s) noexcept;
87
89template<class T>
90static bool
91isHash(std::string const& _hash)
92{
93 return (_hash.size() == T::size * 2
94 || (_hash.size() == T::size * 2 + 2 && _hash.substr(0, 2) == "0x"))
95 && isHex(_hash);
96}
97
100inline std::string
101asString(bytes const& _b)
102{
103 return std::string((char const*) _b.data(), (char const*) (_b.data() + _b.size()));
104}
105
108inline std::string
110{
111 return std::string((char const*) _b.data(), (char const*) (_b.data() + _b.size()));
112}
113
115inline bytes
116asBytes(std::string const& _b)
117{
118 return bytes((uint8_t const*) _b.data(), (uint8_t const*) (_b.data() + _b.size()));
119}
120
123bytes asNibbles(bytesConstRef const& _s);
124
125// Big-endian to/from host endian conversion functions.
126
131template<class T, class _In>
132inline T
133fromBigEndian(_In const& _bytes)
134{
135 T ret = (T) 0;
136 for (auto i : _bytes)
137 ret = (T)((ret << 8) | (uint8_t)(typename std::make_unsigned<decltype(i)>::type) i);
138 return ret;
139}
140
141inline bytes
142toCompactBigEndian(uint8_t _val, unsigned _min = 0)
143{
144 return (_min || _val) ? bytes {_val} : bytes {};
145}
146
147// Algorithms for string and string-like collections.
148
152template<class T, class _U>
153unsigned
154commonPrefix(T const& _t, _U const& _u)
155{
156 unsigned s = std::min<unsigned>(_t.size(), _u.size());
157 for (unsigned i = 0;; ++i)
158 if (i == s || _t[i] != _u[i])
159 return i;
160 return s;
161}
162
165template<class T>
166void
167trimFront(T& _t, unsigned _elements)
168{
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);
172}
173
176template<class T, class _U>
177void
178pushFront(T& _t, _U _e)
179{
180 static_assert(std::is_pod<typename T::value_type>::value, "");
181 _t.push_back(_e);
182 memmove(_t.data() + 1, _t.data(), (_t.size() - 1) * sizeof(_e));
183 _t[0] = _e;
184}
185
187template<class T>
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)
191{
192 auto s = _a.size();
193 _a.resize(_a.size() + _b.size());
194 memcpy(_a.data() + s, _b.data(), _b.size() * sizeof(T));
195 return _a;
196}
197
199template<class 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)
203{
204 _a.reserve(_a.size() + _b.size());
205 for (auto& i : _b)
206 _a.push_back(i);
207 return _a;
208}
209
211template<class T, class U>
212std::set<T>&
213operator+=(std::set<T>& _a, U const& _b)
214{
215 for (auto const& i : _b)
216 _a.insert(i);
217 return _a;
218}
219
221template<class T, class U>
222std::unordered_set<T>&
223operator+=(std::unordered_set<T>& _a, U const& _b)
224{
225 for (auto const& i : _b)
226 _a.insert(i);
227 return _a;
228}
229
231template<class T, class U>
232std::vector<T>&
233operator+=(std::vector<T>& _a, U const& _b)
234{
235 for (auto const& i : _b)
236 _a.push_back(i);
237 return _a;
238}
239
241template<class T, class U>
242std::set<T>
243operator+(std::set<T> _a, U const& _b)
244{
245 return _a += _b;
246}
247
249template<class T, class U>
250std::unordered_set<T>
251operator+(std::unordered_set<T> _a, U const& _b)
252{
253 return _a += _b;
254}
255
257template<class T, class U>
258std::vector<T>
259operator+(std::vector<T> _a, U const& _b)
260{
261 return _a += _b;
262}
263
265template<class T>
266inline std::vector<T>
267operator+(std::vector<T> const& _a, std::vector<T> const& _b)
268{
269 std::vector<T> ret(_a);
270 return ret += _b;
271}
272
273template<class T, class U>
274std::vector<T>
275keysOf(std::map<T, U> const& _m)
276{
277 std::vector<T> ret;
278 for (auto const& i : _m)
279 ret.push_back(i.first);
280 return ret;
281}
282
283template<class T, class U>
284std::vector<T>
285keysOf(std::unordered_map<T, U> const& _m)
286{
287 std::vector<T> ret;
288 for (auto const& i : _m)
289 ret.push_back(i.first);
290 return ret;
291}
292
293template<class T, class U>
294std::vector<U>
295valuesOf(std::map<T, U> const& _m)
296{
297 std::vector<U> ret;
298 ret.reserve(_m.size());
299 for (auto const& i : _m)
300 ret.push_back(i.second);
301 return ret;
302}
303
304template<class T, class U>
305std::vector<U>
306valuesOf(std::unordered_map<T, U> const& _m)
307{
308 std::vector<U> ret;
309 ret.reserve(_m.size());
310 for (auto const& i : _m)
311 ret.push_back(i.second);
312 return ret;
313}
314
315template<class T, class V>
316bool
317contains(T const& _t, V const& _v)
318{
319 return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
320}
321
322} // namespace dev
A modifiable reference to an existing object or vector in memory.
Definition vector_ref.h:21
_T * data() const
Definition vector_ref.h:93
size_t size() const
Definition vector_ref.h:97
Definition Address.h:25
T fromBigEndian(_In const &_bytes)
Converts a big-endian byte-stream represented on a templated collection to a templated integer value.
Definition CommonData.h:133
std::set< T > operator+(std::set< T > _a, U const &_b)
Insert the contents of a container into a set.
Definition CommonData.h:243
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.
Definition CommonData.h:189
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.
Definition CommonData.h:167
bool contains(T const &_t, V const &_v)
Definition CommonData.h:317
static bool isHash(std::string const &_hash)
Definition CommonData.h:91
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.
Definition CommonData.h:101
std::string toHexPrefixed(T const &_data)
Definition CommonData.h:74
void pushFront(T &_t, _U _e)
Pushes an element on to the front of a collection.
Definition CommonData.h:178
WhenError
Definition CommonData.h:38
std::vector< U > valuesOf(std::map< T, U > const &_m)
Definition CommonData.h:295
unsigned commonPrefix(T const &_t, _U const &_u)
Definition CommonData.h:154
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
Definition CommonData.h:116
std::string toHex(Iterator _it, Iterator _end, std::string _prefix)
Definition CommonData.h:45
bytes toCompactBigEndian(uint8_t _val, unsigned _min=0)
Definition CommonData.h:142
std::vector< T > keysOf(std::map< T, U > const &_m)
Definition CommonData.h:275