Fabcoin Core  0.16.2
P2P Digital Currency
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 
34 namespace dev
35 {
36 
37 // String conversion functions, mainly to/from hex/nibble/byte representations.
38 
39 enum class WhenError
40 {
41  DontThrow = 0,
42  Throw = 1,
43 };
44 
45 enum class HexPrefix
46 {
47  DontAdd = 0,
48  Add = 1,
49 };
53 template <class T>
54 std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd)
55 {
56  std::ostringstream ret;
57  unsigned ii = 0;
58  for (auto i: _data)
60  return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();
61 }
62 
66 bytes fromHex(std::string const& _s, WhenError _throw = WhenError::DontThrow);
67 
69 bool isHex(std::string const& _s) noexcept;
70 
72 template <class T> static bool isHash(std::string const& _hash)
73 {
74  return (_hash.size() == T::size * 2 || (_hash.size() == T::size * 2 + 2 && _hash.substr(0, 2) == "0x")) && isHex(_hash);
75 }
76 
79 inline std::string asString(bytes const& _b)
80 {
81  return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
82 }
83 
86 inline std::string asString(bytesConstRef _b)
87 {
88  return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
89 }
90 
92 inline bytes asBytes(std::string const& _b)
93 {
94  return bytes((byte const*)_b.data(), (byte const*)(_b.data() + _b.size()));
95 }
96 
99 bytes asNibbles(bytesConstRef const& _s);
100 
101 
102 // Big-endian to/from host endian conversion functions.
103 
109 template <class T, class Out>
110 inline void toBigEndian(T _val, Out& o_out)
111 {
112  static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
113  for (auto i = o_out.size(); i != 0; _val >>= 8, i--)
114  {
115  T v = _val & (T)0xff;
116  o_out[i - 1] = (typename Out::value_type)(uint8_t)v;
117  }
118 }
119 
123 template <class T, class _In>
124 inline T fromBigEndian(_In const& _bytes)
125 {
126  T ret = (T)0;
127  for (auto i: _bytes)
128  ret = (T)((ret << 8) | (byte)(typename std::make_unsigned<decltype(i)>::type)i);
129  return ret;
130 }
131 
133 inline std::string toBigEndianString(u256 _val) { std::string ret(32, '\0'); toBigEndian(_val, ret); return ret; }
134 inline std::string toBigEndianString(u160 _val) { std::string ret(20, '\0'); toBigEndian(_val, ret); return ret; }
135 inline bytes toBigEndian(u256 _val) { bytes ret(32); toBigEndian(_val, ret); return ret; }
136 inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); return ret; }
137 
140 template <class T>
141 inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
142 {
143  static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
144  int i = 0;
145  for (T v = _val; v; ++i, v >>= 8) {}
146  bytes ret(std::max<unsigned>(_min, i), 0);
147  toBigEndian(_val, ret);
148  return ret;
149 }
150 inline bytes toCompactBigEndian(byte _val, unsigned _min = 0)
151 {
152  return (_min || _val) ? bytes{ _val } : bytes{};
153 }
154 
157 template <class T>
158 inline std::string toCompactBigEndianString(T _val, unsigned _min = 0)
159 {
160  static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
161  int i = 0;
162  for (T v = _val; v; ++i, v >>= 8) {}
163  std::string ret(std::max<unsigned>(_min, i), '\0');
164  toBigEndian(_val, ret);
165  return ret;
166 }
167 
169 inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
170 {
171  std::string str = toHex(toBigEndian(val));
172  return (prefix == HexPrefix::Add) ? "0x" + str : str;
173 }
174 
175 inline std::string toCompactHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd, unsigned _min = 0)
176 {
177  std::string str = toHex(toCompactBigEndian(val, _min));
178  return (prefix == HexPrefix::Add) ? "0x" + str : str;
179 }
180 
181 // Algorithms for string and string-like collections.
182 
185 std::string escaped(std::string const& _s, bool _all = true);
186 
190 template <class T, class _U>
191 unsigned commonPrefix(T const& _t, _U const& _u)
192 {
193  unsigned s = std::min<unsigned>(_t.size(), _u.size());
194  for (unsigned i = 0;; ++i)
195  if (i == s || _t[i] != _u[i])
196  return i;
197  return s;
198 }
199 
201 std::string randomWord();
202 
204 template <class T>
205 inline unsigned bytesRequired(T _i)
206 {
207  static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
208  unsigned i = 0;
209  for (; _i != 0; ++i, _i >>= 8) {}
210  return i;
211 }
212 
215 template <class T>
216 void trimFront(T& _t, unsigned _elements)
217 {
218  static_assert(std::is_pod<typename T::value_type>::value, "");
219  memmove(_t.data(), _t.data() + _elements, (_t.size() - _elements) * sizeof(_t[0]));
220  _t.resize(_t.size() - _elements);
221 }
222 
225 template <class T, class _U>
226 void pushFront(T& _t, _U _e)
227 {
228  static_assert(std::is_pod<typename T::value_type>::value, "");
229  _t.push_back(_e);
230  memmove(_t.data() + 1, _t.data(), (_t.size() - 1) * sizeof(_e));
231  _t[0] = _e;
232 }
233 
235 template <class T>
236 inline std::vector<T>& operator+=(std::vector<typename std::enable_if<std::is_pod<T>::value, T>::type>& _a, std::vector<T> const& _b)
237 {
238  auto s = _a.size();
239  _a.resize(_a.size() + _b.size());
240  memcpy(_a.data() + s, _b.data(), _b.size() * sizeof(T));
241  return _a;
242 
243 }
244 
246 template <class T>
247 inline std::vector<T>& operator+=(std::vector<typename std::enable_if<!std::is_pod<T>::value, T>::type>& _a, std::vector<T> const& _b)
248 {
249  _a.reserve(_a.size() + _b.size());
250  for (auto& i: _b)
251  _a.push_back(i);
252  return _a;
253 }
254 
256 template <class T, class U> std::set<T>& operator+=(std::set<T>& _a, U const& _b)
257 {
258  for (auto const& i: _b)
259  _a.insert(i);
260  return _a;
261 }
262 
264 template <class T, class U> std::unordered_set<T>& operator+=(std::unordered_set<T>& _a, U const& _b)
265 {
266  for (auto const& i: _b)
267  _a.insert(i);
268  return _a;
269 }
270 
272 template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U const& _b)
273 {
274  for (auto const& i: _b)
275  _a.push_back(i);
276  return _a;
277 }
278 
280 template <class T, class U> std::set<T> operator+(std::set<T> _a, U const& _b)
281 {
282  return _a += _b;
283 }
284 
286 template <class T, class U> std::unordered_set<T> operator+(std::unordered_set<T> _a, U const& _b)
287 {
288  return _a += _b;
289 }
290 
292 template <class T, class U> std::vector<T> operator+(std::vector<T> _a, U const& _b)
293 {
294  return _a += _b;
295 }
296 
298 template <class T>
299 inline std::vector<T> operator+(std::vector<T> const& _a, std::vector<T> const& _b)
300 {
301  std::vector<T> ret(_a);
302  return ret += _b;
303 }
304 
306 template <class T>
307 inline std::set<T>& operator+=(std::set<T>& _a, std::set<T> const& _b)
308 {
309  for (auto& i: _b)
310  _a.insert(i);
311  return _a;
312 }
313 
315 template <class T>
316 inline std::set<T> operator+(std::set<T> const& _a, std::set<T> const& _b)
317 {
318  std::set<T> ret(_a);
319  return ret += _b;
320 }
321 
322 template <class A, class B>
323 std::unordered_map<A, B>& operator+=(std::unordered_map<A, B>& _x, std::unordered_map<A, B> const& _y)
324 {
325  for (auto const& i: _y)
326  _x.insert(i);
327  return _x;
328 }
329 
330 template <class A, class B>
331 std::unordered_map<A, B> operator+(std::unordered_map<A, B> const& _x, std::unordered_map<A, B> const& _y)
332 {
333  std::unordered_map<A, B> ret(_x);
334  return ret += _y;
335 }
336 
338 std::string toString(string32 const& _s);
339 
340 template<class T, class U>
341 std::vector<T> keysOf(std::map<T, U> const& _m)
342 {
343  std::vector<T> ret;
344  for (auto const& i: _m)
345  ret.push_back(i.first);
346  return ret;
347 }
348 
349 template<class T, class U>
350 std::vector<T> keysOf(std::unordered_map<T, U> const& _m)
351 {
352  std::vector<T> ret;
353  for (auto const& i: _m)
354  ret.push_back(i.first);
355  return ret;
356 }
357 
358 template<class T, class U>
359 std::vector<U> valuesOf(std::map<T, U> const& _m)
360 {
361  std::vector<U> ret;
362  ret.reserve(_m.size());
363  for (auto const& i: _m)
364  ret.push_back(i.second);
365  return ret;
366 }
367 
368 template<class T, class U>
369 std::vector<U> valuesOf(std::unordered_map<T, U> const& _m)
370 {
371  std::vector<U> ret;
372  ret.reserve(_m.size());
373  for (auto const& i: _m)
374  ret.push_back(i.second);
375  return ret;
376 }
377 
378 template <class T, class V>
379 bool contains(T const& _t, V const& _v)
380 {
381  return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
382 }
383 
384 }
unsigned commonPrefix(T const &_t, _U const &_u)
Definition: CommonData.h:191
std::string toCompactHex(u256 val, HexPrefix prefix=HexPrefix::DontAdd, unsigned _min=0)
Definition: CommonData.h:175
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
std::vector< T > keysOf(std::map< T, U > const &_m)
Definition: CommonData.h:341
uint8_t byte
Definition: Common.h:57
void toBigEndian(T _val, Out &o_out)
Converts a templated integer value to the big-endian byte-stream represented on a templated collectio...
Definition: CommonData.h:110
#define T(i, x)
const char * prefix
Definition: rest.cpp:623
std::string randomWord()
Creates a random, printable, word.
Definition: CommonData.cpp:88
void trimFront(T &_t, unsigned _elements)
Trims a given number of elements from the front of a collection.
Definition: CommonData.h:216
unsigned bytesRequired(T _i)
Determine bytes required to encode the given integer value.
Definition: CommonData.h:205
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
WhenError
Definition: CommonData.h:39
std::set< T > operator+(std::set< T > _a, U const &_b)
Insert the contents of a container into a set.
Definition: CommonData.h:280
std::vector< U > valuesOf(std::map< T, U > const &_m)
Definition: CommonData.h:359
std::string escaped(std::string const &_s, bool _all=true)
Escapes a string into the C-string representation.
Definition: CommonData.cpp:60
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
Definition: Common.h:127
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
bool contains(T const &_t, V const &_v)
Definition: CommonData.h:379
bool isHex(std::string const &_s) noexcept
std::string toCompactBigEndianString(T _val, unsigned _min=0)
Convenience function for toBigEndian.
Definition: CommonData.h:158
std::vector< byte > bytes
Definition: Common.h:75
bytes toCompactBigEndian(T _val, unsigned _min=0)
Convenience function for toBigEndian.
Definition: CommonData.h:141
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string&#39;s (byte) data.
Definition: CommonData.h:92
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bytes asNibbles(bytesConstRef const &_s)
Definition: CommonData.cpp:129
size_t size() const
Definition: vector_ref.h:55
std::array< char, 32 > string32
Definition: Common.h:150
void pushFront(T &_t, _U _e)
Pushes an element on to the front of a collection.
Definition: CommonData.h:226
_T * data() const
Definition: vector_ref.h:51
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
uint8_t const size_t const size
Definition: sha3.h:20
std::string toBigEndianString(u256 _val)
Convenience functions for toBigEndian.
Definition: CommonData.h:133
void * memcpy(void *a, const void *b, size_t c)
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:236
HexPrefix
Definition: CommonData.h:45
uint8_t byte
Definition: Common.h:10
T fromBigEndian(_In const &_bytes)
Converts a big-endian byte-stream represented on a templated collection to a templated integer value...
Definition: CommonData.h:124
void * memmove(void *a, const void *b, size_t c)
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Definition: CommonData.h:79