17 static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
29 std::string strResult;
30 for (std::string::size_type i = 0; i < str.size(); i++)
32 if (
SAFE_CHARS[rule].find(str[i]) != std::string::npos)
33 strResult.push_back(str[i]);
39 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
40 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
41 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
42 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
43 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
44 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
45 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
46 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
47 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
48 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
49 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
50 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
51 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
52 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
53 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
54 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
61 bool IsHex(
const std::string& str)
63 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
68 return (str.size() > 0) && (str.size()%2 == 0);
73 size_t starting_location = 0;
74 if (str.size() > 2 && *str.begin() ==
'0' && *(str.begin()+1) ==
'x') {
75 starting_location = 2;
77 for (
const char c : str.substr(starting_location)) {
81 return (str.size() > starting_location);
84 std::vector<unsigned char>
ParseHex(
const char* psz)
87 std::vector<unsigned char> vch;
93 if (c == (
signed char)-1)
95 auto n{uint8_t(c << 4)};
97 if (c == (
signed char)-1)
105 std::vector<unsigned char>
ParseHex(
const std::string& str)
112 size_t colon = in.find_last_of(
':');
114 bool fHaveColon = colon != in.npos;
115 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
116 bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(
':', colon - 1) != in.npos)};
117 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
120 in = in.substr(0, colon);
124 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
125 hostOut = in.substr(1, in.size() - 2);
133 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
136 str.reserve(((input.
size() + 2) / 3) * 4);
137 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
138 while (str.size() % 4) str +=
'=';
142 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pf_invalid)
144 static const int8_t decode64_table[256]{
145 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
146 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
147 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
148 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
149 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
150 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
151 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
154 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
155 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
156 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
157 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
161 std::vector<uint8_t> val;
162 val.reserve(strlen(p));
164 int x = decode64_table[(
unsigned char)*p];
166 val.push_back(uint8_t(x));
170 std::vector<unsigned char> ret;
171 ret.reserve((val.size() * 3) / 4);
172 bool valid = ConvertBits<6, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
175 while (valid && *p != 0) {
182 valid = valid && (p - e) % 4 == 0 && p - q < 4;
183 if (pf_invalid) *pf_invalid = !valid;
196 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
197 return std::string((
const char*)vchRet.data(), vchRet.size());
202 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
205 str.reserve(((input.
size() + 4) / 5) * 8);
206 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
208 while (str.size() % 8) {
220 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pf_invalid)
222 static const int8_t decode32_table[256]{
223 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
224 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
225 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
226 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
227 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
228 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
229 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
230 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
231 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
232 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
233 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
234 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
235 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
239 std::vector<uint8_t> val;
240 val.reserve(strlen(p));
242 int x = decode32_table[(
unsigned char)*p];
244 val.push_back(uint8_t(x));
248 std::vector<unsigned char> ret;
249 ret.reserve((val.size() * 5) / 8);
250 bool valid = ConvertBits<5, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
253 while (valid && *p != 0) {
260 valid = valid && (p - e) % 8 == 0 && p - q < 8;
261 if (pf_invalid) *pf_invalid = !valid;
274 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
275 return std::string((
const char*)vchRet.data(), vchRet.size());
279 template <
typename T>
280 bool ParseIntegral(
const std::string& str, T* out)
282 static_assert(std::is_integral<T>::value);
285 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
288 const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
292 if (out !=
nullptr) {
301 return ParseIntegral<int32_t>(str, out);
306 return ParseIntegral<int64_t>(str, out);
311 return ParseIntegral<uint8_t>(str, out);
316 return ParseIntegral<uint16_t>(str, out);
321 return ParseIntegral<uint32_t>(str, out);
326 return ParseIntegral<uint64_t>(str, out);
332 std::stringstream out;
335 while (ptr < in.size())
337 size_t lineend = in.find_first_of(
'\n', ptr);
338 if (lineend == std::string::npos) {
341 const size_t linelen = lineend - ptr;
342 const size_t rem_width = width - indented;
343 if (linelen <= rem_width) {
344 out << in.substr(ptr, linelen + 1);
348 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
349 if (finalspace == std::string::npos || finalspace < ptr) {
351 finalspace = in.find_first_of(
"\n ", ptr);
352 if (finalspace == std::string::npos) {
354 out << in.substr(ptr);
358 out << in.substr(ptr, finalspace - ptr) <<
"\n";
359 if (in[finalspace] ==
'\n') {
362 out << std::string(indent,
' ');
365 ptr = finalspace + 1;
387 for (
int i=0; i<=mantissa_tzeros; ++i) {
392 mantissa += ch -
'0';
400 int64_t mantissa = 0;
401 int64_t exponent = 0;
402 int mantissa_tzeros = 0;
403 bool mantissa_sign =
false;
404 bool exponent_sign =
false;
406 int end = val.size();
409 if (ptr < end && val[ptr] ==
'-') {
410 mantissa_sign =
true;
415 if (val[ptr] ==
'0') {
418 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
419 while (ptr < end &&
IsDigit(val[ptr])) {
426 if (ptr < end && val[ptr] ==
'.')
429 if (ptr < end &&
IsDigit(val[ptr]))
431 while (ptr < end &&
IsDigit(val[ptr])) {
439 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
442 if (ptr < end && val[ptr] ==
'+')
444 else if (ptr < end && val[ptr] ==
'-') {
445 exponent_sign =
true;
448 if (ptr < end &&
IsDigit(val[ptr])) {
449 while (ptr < end &&
IsDigit(val[ptr])) {
452 exponent = exponent * 10 + val[ptr] -
'0';
462 exponent = -exponent;
463 exponent = exponent - point_ofs + mantissa_tzeros;
467 mantissa = -mantissa;
470 exponent += decimals;
476 for (
int i=0; i < exponent; ++i) {
485 *amount_out = mantissa;
493 for (
auto ch : str) r +=
ToLower(ch);
500 for (
auto ch : str) r +=
ToUpper(ch);
506 if (str.empty())
return str;
513 std::string rv(s.
size() * 2,
'\0');
514 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
515 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
516 auto it = rv.begin();
517 for (uint8_t v : s) {
518 *it++ = hexmap[v >> 4];
519 *it++ = hexmap[v & 15];
530 auto multiplier = default_multiplier;
531 char unit = str.back();
562 uint64_t unit_amount =
static_cast<uint64_t
>(multiplier);
563 auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
564 if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) {
567 return *parsed_num * unit_amount;