Ring Daemon
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 || (_hash.size() == T::size * 2 + 2 && _hash.substr(0, 2) == "0x"))
94 && isHex(_hash);
95}
96
99inline std::string
100asString(bytes const& _b)
101{
102 return std::string((char const*) _b.data(), (char const*) (_b.data() + _b.size()));
103}
104
107inline std::string
109{
110 return std::string((char const*) _b.data(), (char const*) (_b.data() + _b.size()));
111}
112
114inline bytes
115asBytes(std::string const& _b)
116{
117 return bytes((uint8_t const*) _b.data(), (uint8_t const*) (_b.data() + _b.size()));
118}
119
122bytes asNibbles(bytesConstRef const& _s);
123
124// Big-endian to/from host endian conversion functions.
125
130template<class T, class _In>
131inline T
132fromBigEndian(_In const& _bytes)
133{
134 T ret = (T) 0;
135 for (auto i : _bytes)
136 ret = (T) ((ret << 8) | (uint8_t) (typename std::make_unsigned<decltype(i)>::type) i);
137 return ret;
138}
139
140inline bytes
141toCompactBigEndian(uint8_t _val, unsigned _min = 0)
142{
143 return (_min || _val) ? bytes {_val} : bytes {};
144}
145
146// Algorithms for string and string-like collections.
147
151template<class T, class _U>
152unsigned
153commonPrefix(T const& _t, _U const& _u)
154{
155 unsigned s = std::min<unsigned>(_t.size(), _u.size());
156 for (unsigned i = 0;; ++i)
157 if (i == s || _t[i] != _u[i])
158 return i;
159 return s;
160}
161
164template<class T>
165void
166trimFront(T& _t, unsigned _elements)
167{
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);
171}
172
175template<class T, class _U>
176void
177pushFront(T& _t, _U _e)
178{
179 static_assert(std::is_pod<typename T::value_type>::value, "");
180 _t.push_back(_e);
181 memmove(_t.data() + 1, _t.data(), (_t.size() - 1) * sizeof(_e));
182 _t[0] = _e;
183}
184
186template<class T>
187inline std::vector<T>&
188operator+=(std::vector<typename std::enable_if<std::is_pod<T>::value, T>::type>& _a, std::vector<T> const& _b)
189{
190 auto s = _a.size();
191 _a.resize(_a.size() + _b.size());
192 memcpy(_a.data() + s, _b.data(), _b.size() * sizeof(T));
193 return _a;
194}
195
197template<class 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)
200{
201 _a.reserve(_a.size() + _b.size());
202 for (auto& i : _b)
203 _a.push_back(i);
204 return _a;
205}
206
208template<class T, class U>
209std::set<T>&
210operator+=(std::set<T>& _a, U const& _b)
211{
212 for (auto const& i : _b)
213 _a.insert(i);
214 return _a;
215}
216
218template<class T, class U>
219std::unordered_set<T>&
220operator+=(std::unordered_set<T>& _a, U const& _b)
221{
222 for (auto const& i : _b)
223 _a.insert(i);
224 return _a;
225}
226
228template<class T, class U>
229std::vector<T>&
230operator+=(std::vector<T>& _a, U const& _b)
231{
232 for (auto const& i : _b)
233 _a.push_back(i);
234 return _a;
235}
236
238template<class T, class U>
239std::set<T>
240operator+(std::set<T> _a, U const& _b)
241{
242 return _a += _b;
243}
244
246template<class T, class U>
247std::unordered_set<T>
248operator+(std::unordered_set<T> _a, U const& _b)
249{
250 return _a += _b;
251}
252
254template<class T, class U>
255std::vector<T>
256operator+(std::vector<T> _a, U const& _b)
257{
258 return _a += _b;
259}
260
262template<class T>
263inline std::vector<T>
264operator+(std::vector<T> const& _a, std::vector<T> const& _b)
265{
266 std::vector<T> ret(_a);
267 return ret += _b;
268}
269
270template<class T, class U>
271std::vector<T>
272keysOf(std::map<T, U> const& _m)
273{
274 std::vector<T> ret;
275 for (auto const& i : _m)
276 ret.push_back(i.first);
277 return ret;
278}
279
280template<class T, class U>
281std::vector<T>
282keysOf(std::unordered_map<T, U> const& _m)
283{
284 std::vector<T> ret;
285 for (auto const& i : _m)
286 ret.push_back(i.first);
287 return ret;
288}
289
290template<class T, class U>
291std::vector<U>
292valuesOf(std::map<T, U> const& _m)
293{
294 std::vector<U> ret;
295 ret.reserve(_m.size());
296 for (auto const& i : _m)
297 ret.push_back(i.second);
298 return ret;
299}
300
301template<class T, class U>
302std::vector<U>
303valuesOf(std::unordered_map<T, U> const& _m)
304{
305 std::vector<U> ret;
306 ret.reserve(_m.size());
307 for (auto const& i : _m)
308 ret.push_back(i.second);
309 return ret;
310}
311
312template<class T, class V>
313bool
314contains(T const& _t, V const& _v)
315{
316 return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
317}
318
319} // 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:88
size_t size() const
Definition vector_ref.h:92
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:132
std::set< T > operator+(std::set< T > _a, U const &_b)
Insert the contents of a container into a set.
Definition CommonData.h:240
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:188
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:166
bool contains(T const &_t, V const &_v)
Definition CommonData.h:314
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:100
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:177
WhenError
Definition CommonData.h:38
std::vector< U > valuesOf(std::map< T, U > const &_m)
Definition CommonData.h:292
unsigned commonPrefix(T const &_t, _U const &_u)
Definition CommonData.h:153
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
Definition CommonData.h:115
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:141
std::vector< T > keysOf(std::map< T, U > const &_m)
Definition CommonData.h:272