Ethereum  PoC-8
The C++ Implementation of Ethereum
CommonJS.cpp
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 #include "CommonJS.h"
25 
26 using namespace std;
27 
28 namespace dev
29 {
30 
31 bytes jsToBytes(string const& _s, OnFailed _f)
32 {
33  try
34  {
35  return fromHex(_s, WhenError::Throw);
36  }
37  catch (...)
38  {
39  if (_f == OnFailed::InterpretRaw)
40  return asBytes(_s);
41  else if (_f == OnFailed::Throw)
42  throw invalid_argument("Cannot intepret '" + _s + "' as bytes; must be 0x-prefixed hex or decimal.");
43  }
44  return bytes();
45 }
46 
47 bytes padded(bytes _b, unsigned _l)
48 {
49  while (_b.size() < _l)
50  _b.insert(_b.begin(), 0);
51  return asBytes(asString(_b).substr(_b.size() - max(_l, _l)));
52 }
53 
54 bytes paddedRight(bytes _b, unsigned _l)
55 {
56  _b.resize(_l);
57  return _b;
58 }
59 
61 {
62  auto p = asString(_b).find_last_not_of((char)0);
63  _b.resize(p == string::npos ? 0 : (p + 1));
64  return _b;
65 }
66 
68 {
69  unsigned int i = 0;
70  if (_b.size() == 0)
71  return _b;
72 
73  while (i < _b.size() && _b[i] == byte(0))
74  i++;
75 
76  if (i != 0)
77  _b.erase(_b.begin(), _b.begin() + i);
78  return _b;
79 }
80 
81 string fromRaw(h256 _n)
82 {
83  if (_n)
84  {
85  string s((char const*)_n.data(), 32);
86  auto l = s.find_first_of('\0');
87  if (!l)
88  return "";
89  if (l != string::npos)
90  s.resize(l);
91  for (auto i: s)
92  if (i < 32)
93  return "";
94  return s;
95  }
96  return "";
97 }
98 
99 }
100 
dev::fromRaw
string fromRaw(h256 _n)
Convert h256 into user-readable string (by directly using std::string constructor)....
Definition: CommonJS.cpp:81
CommonJS.h
dev::FixedHash::data
byte * data()
Definition: FixedHash.h:138
dev::FixedHash
Definition: FixedHash.h:47
dev::padded
bytes padded(bytes _b, unsigned _l)
Add '0' on, or remove items from, the front of _b until it is of length _l.
Definition: CommonJS.cpp:47
dev::paddedRight
bytes paddedRight(bytes _b, unsigned _l)
Add '0' on, or remove items from, the back of _b until it is of length _l.
Definition: CommonJS.cpp:54
dev::OnFailed
OnFailed
Definition: CommonJS.h:70
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::jsToBytes
bytes jsToBytes(string const &_s, OnFailed _f)
Definition: CommonJS.cpp:31
dev::asBytes
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
Definition: CommonData.h:106
dev::asString
std::string asString(bytes const &_b)
Definition: CommonData.h:93
std
Definition: FixedHash.h:393
dev
Definition: Address.cpp:21
dev::unpadLeft
bytes unpadLeft(bytes _b)
Remove all 0 byte on the head of _s.
Definition: CommonJS.cpp:67
dev::unpadded
bytes unpadded(bytes _b)
Removing all trailing '0'. Returns empty array if input contains only '0' char.
Definition: CommonJS.cpp:60
dev::fromHex
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:81