27 #pragma warning(disable:4724) // potential mod by 0, line 78 of boost/random/uniform_int_distribution.hpp (boost 1.55) 29 #include <boost/random/uniform_int_distribution.hpp> 40 int fromHexChar(
char _i) noexcept
42 if (_i >=
'0' && _i <=
'9')
44 if (_i >=
'a' && _i <=
'f')
46 if (_i >=
'A' && _i <=
'F')
55 if (_s.compare(0, 2,
"0x") == 0)
57 return std::all_of(it, _s.end(), [](
char c){
return fromHexChar(
c) != -1; });
62 static const map<char, char> prettyEscapes{{
'\r',
'r'}, {
'\n',
'n'}, {
'\t',
't'}, {
'\v',
'v'}};
64 ret.reserve(_s.size() + 2);
67 if (i ==
'"' && !_all)
69 else if (i ==
'\\' && !_all)
71 else if (prettyEscapes.count(i) && !_all)
74 ret += prettyEscapes.find(i)->second;
76 else if (i <
' ' || _all)
79 ret.push_back(
"0123456789abcdef"[(uint8_t)i / 16]);
80 ret.push_back(
"0123456789abcdef"[(uint8_t)i % 16]);
90 static std::mt19937_64 s_eng(0);
91 std::string ret(boost::random::uniform_int_distribution<int>(1, 5)(s_eng),
' ');
92 char const n[] =
"qwertyuiop";
93 boost::random::uniform_int_distribution<int>
d(0,
sizeof(n) - 2);
101 unsigned s = (_s.size() >= 2 && _s[0] ==
'0' && _s[1] ==
'x') ? 2 : 0;
102 std::vector<uint8_t> ret;
103 ret.reserve((_s.size() - s + 1) / 2);
107 int h = fromHexChar(_s[s++]);
110 else if (_throw == WhenError::Throw)
111 BOOST_THROW_EXCEPTION(BadHexCharacter());
115 for (
unsigned i = s; i < _s.size(); i += 2)
117 int h = fromHexChar(_s[i]);
118 int l = fromHexChar(_s[i + 1]);
119 if (h != -1 && l != -1)
120 ret.push_back((
byte)(h * 16 + l));
121 else if (_throw == WhenError::Throw)
122 BOOST_THROW_EXCEPTION(BadHexCharacter());
131 std::vector<uint8_t> ret;
132 ret.reserve(_s.
size() * 2);
135 ret.push_back(i / 16);
136 ret.push_back(i % 16);
144 for (
unsigned i = 0; i < 32 && _s[i]; ++i)
145 ret.push_back(_s[i]);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::string randomWord()
Creates a random, printable, word.
std::hash for asio::adress
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
std::string escaped(std::string const &_s, bool _all=true)
Escapes a string into the C-string representation.
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
bool isHex(std::string const &_s) noexcept
std::vector< byte > bytes
bytes asNibbles(bytesConstRef const &_s)
std::array< char, 32 > string32