Browse Source

fixed namespaces in CommonJS && return 0x00 instead of 0x

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
f62a223f93
  1. 18
      libdevcore/CommonJS.cpp
  2. 16
      libdevcore/CommonJS.h

18
libdevcore/CommonJS.cpp

@ -23,15 +23,17 @@
#include "CommonJS.h" #include "CommonJS.h"
using namespace std;
namespace dev namespace dev
{ {
bytes jsToBytes(std::string const& _s) bytes jsToBytes(string const& _s)
{ {
if (_s.substr(0, 2) == "0x") if (_s.substr(0, 2) == "0x")
// Hex // Hex
return fromHex(_s.substr(2)); return fromHex(_s.substr(2));
else if (_s.find_first_not_of("0123456789") == std::string::npos) else if (_s.find_first_not_of("0123456789") == string::npos)
// Decimal // Decimal
return toCompactBigEndian(bigint(_s)); return toCompactBigEndian(bigint(_s));
else else
@ -42,7 +44,7 @@ bytes padded(bytes _b, unsigned _l)
{ {
while (_b.size() < _l) while (_b.size() < _l)
_b.insert(_b.begin(), 0); _b.insert(_b.begin(), 0);
return asBytes(asString(_b).substr(_b.size() - std::max(_l, _l))); return asBytes(asString(_b).substr(_b.size() - max(_l, _l)));
} }
bytes paddedRight(bytes _b, unsigned _l) bytes paddedRight(bytes _b, unsigned _l)
@ -54,7 +56,7 @@ bytes paddedRight(bytes _b, unsigned _l)
bytes unpadded(bytes _b) bytes unpadded(bytes _b)
{ {
auto p = asString(_b).find_last_not_of((char)0); auto p = asString(_b).find_last_not_of((char)0);
_b.resize(p == std::string::npos ? 0 : (p + 1)); _b.resize(p == string::npos ? 0 : (p + 1));
return _b; return _b;
} }
@ -72,18 +74,18 @@ bytes unpadLeft(bytes _b)
return _b; return _b;
} }
std::string fromRaw(h256 _n, unsigned* _inc) string fromRaw(h256 _n, unsigned* _inc)
{ {
if (_n) if (_n)
{ {
std::string s((char const*)_n.data(), 32); string s((char const*)_n.data(), 32);
auto l = s.find_first_of('\0'); auto l = s.find_first_of('\0');
if (!l) if (!l)
return ""; return "";
if (l != std::string::npos) if (l != string::npos)
{ {
auto p = s.find_first_not_of('\0', l); auto p = s.find_first_not_of('\0', l);
if (!(p == std::string::npos || (_inc && p == 31))) if (!(p == string::npos || (_inc && p == 31)))
return ""; return "";
if (_inc) if (_inc)
*_inc = (byte)s[31]; *_inc = (byte)s[31];

16
libdevcore/CommonJS.h

@ -38,12 +38,12 @@ template <unsigned S> std::string toJS(FixedHash<S> const& _h)
template <unsigned N> std::string toJS(boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N, N, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> const& _n) template <unsigned N> std::string toJS(boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N, N, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> const& _n)
{ {
return "0x" + toHex(toCompactBigEndian(_n)); return "0x" + toHex(toCompactBigEndian(_n, 1));
} }
inline std::string toJS(dev::bytes const& _n) inline std::string toJS(bytes const& _n)
{ {
return "0x" + dev::toHex(_n); return "0x" + toHex(_n);
} }
template< typename T >std::string toJS( T const& i ) template< typename T >std::string toJS( T const& i )
@ -81,7 +81,7 @@ template <unsigned N> FixedHash<N> jsToFixed(std::string const& _s)
inline std::string jsToFixed(double _s) inline std::string jsToFixed(double _s)
{ {
return toJS(dev::u256(_s * (double)(dev::u256(1) << 128))); return toJS(u256(_s * (double)(u256(1) << 128)));
} }
template <unsigned N> boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> jsToInt(std::string const& _s) template <unsigned N> boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> jsToInt(std::string const& _s)
@ -106,13 +106,13 @@ inline int jsToInt(std::string const& _s)
inline std::string jsToDecimal(std::string const& _s) inline std::string jsToDecimal(std::string const& _s)
{ {
return dev::toString(jsToU256(_s)); return toString(jsToU256(_s));
} }
inline std::string jsFromBinary(dev::bytes _s, unsigned _padding = 32) inline std::string jsFromBinary(bytes _s, unsigned _padding = 32)
{ {
_s.resize(std::max<unsigned>(_s.size(), _padding)); _s.resize(std::max<unsigned>(_s.size(), _padding));
return "0x" + dev::toHex(_s); return "0x" + toHex(_s);
} }
inline std::string jsFromBinary(std::string const& _s, unsigned _padding = 32) inline std::string jsFromBinary(std::string const& _s, unsigned _padding = 32)
@ -122,7 +122,7 @@ inline std::string jsFromBinary(std::string const& _s, unsigned _padding = 32)
inline double jsFromFixed(std::string const& _s) inline double jsFromFixed(std::string const& _s)
{ {
return (double)jsToU256(_s) / (double)(dev::u256(1) << 128); return (double)jsToU256(_s) / (double)(u256(1) << 128);
} }
} }

Loading…
Cancel
Save