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 unsigned char n = (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 && (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 +=
'=';
147 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pf_invalid)
149 static const int decode64_table[256] =
151 -1, -1, -1, -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, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
154 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
155 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
156 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
157 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
158 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
160 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
161 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
162 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
163 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
167 std::vector<uint8_t> val;
168 val.reserve(strlen(p));
170 int x = decode64_table[(
unsigned char)*p];
176 std::vector<unsigned char> ret;
177 ret.reserve((val.size() * 3) / 4);
178 bool valid = ConvertBits<6, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
181 while (valid && *p != 0) {
188 valid = valid && (p - e) % 4 == 0 && p - q < 4;
189 if (pf_invalid) *pf_invalid = !valid;
202 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
203 return std::string((
const char*)vchRet.data(), vchRet.size());
208 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
211 str.reserve(((input.
size() + 4) / 5) * 8);
212 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
214 while (str.size() % 8) {
226 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pf_invalid)
228 static const int decode32_table[256] =
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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
233 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
234 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
235 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
236 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
237 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
238 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
239 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
240 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
241 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
242 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
246 std::vector<uint8_t> val;
247 val.reserve(strlen(p));
249 int x = decode32_table[(
unsigned char)*p];
255 std::vector<unsigned char> ret;
256 ret.reserve((val.size() * 5) / 8);
257 bool valid = ConvertBits<5, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
260 while (valid && *p != 0) {
267 valid = valid && (p - e) % 8 == 0 && p - q < 8;
268 if (pf_invalid) *pf_invalid = !valid;
281 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
282 return std::string((
const char*)vchRet.data(), vchRet.size());
289 if (str.size() >= 1 && (
IsSpace(str[0]) ||
IsSpace(str[str.size()-1])))
300 char *endp =
nullptr;
302 long int n = strtol(str.c_str(), &endp, 10);
303 if(out) *out = (int32_t)n;
307 return endp && *endp == 0 && !errno &&
308 n >= std::numeric_limits<int32_t>::min() &&
309 n <= std::numeric_limits<int32_t>::max();
316 char *endp =
nullptr;
318 long long int n = strtoll(str.c_str(), &endp, 10);
319 if(out) *out = (int64_t)n;
322 return endp && *endp == 0 && !errno &&
323 n >= std::numeric_limits<int64_t>::min() &&
324 n <= std::numeric_limits<int64_t>::max();
330 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
333 if (out !=
nullptr) {
334 *out =
static_cast<uint8_t
>(u32);
342 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
345 if (out !=
nullptr) {
346 *out =
static_cast<uint16_t
>(u32);
355 if (str.size() >= 1 && str[0] ==
'-')
357 char *endp =
nullptr;
359 unsigned long int n = strtoul(str.c_str(), &endp, 10);
360 if(out) *out = (uint32_t)n;
364 return endp && *endp == 0 && !errno &&
365 n <= std::numeric_limits<uint32_t>::max();
372 if (str.size() >= 1 && str[0] ==
'-')
374 char *endp =
nullptr;
376 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
377 if(out) *out = (uint64_t)n;
380 return endp && *endp == 0 && !errno &&
381 n <= std::numeric_limits<uint64_t>::max();
389 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
391 std::istringstream text(str);
392 text.imbue(std::locale::classic());
395 if(out) *out = result;
396 return text.eof() && !text.fail();
401 std::stringstream out;
404 while (ptr < in.size())
406 size_t lineend = in.find_first_of(
'\n', ptr);
407 if (lineend == std::string::npos) {
410 const size_t linelen = lineend - ptr;
411 const size_t rem_width = width - indented;
412 if (linelen <= rem_width) {
413 out << in.substr(ptr, linelen + 1);
417 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
418 if (finalspace == std::string::npos || finalspace < ptr) {
420 finalspace = in.find_first_of(
"\n ", ptr);
421 if (finalspace == std::string::npos) {
423 out << in.substr(ptr);
427 out << in.substr(ptr, finalspace - ptr) <<
"\n";
428 if (in[finalspace] ==
'\n') {
431 out << std::string(indent,
' ');
434 ptr = finalspace + 1;
443 return _atoi64(str.c_str());
445 return strtoll(str.c_str(),
nullptr, 10);
449 int atoi(
const std::string& str)
451 return atoi(str.c_str());
470 for (
int i=0; i<=mantissa_tzeros; ++i) {
475 mantissa += ch -
'0';
483 int64_t mantissa = 0;
484 int64_t exponent = 0;
485 int mantissa_tzeros = 0;
486 bool mantissa_sign =
false;
487 bool exponent_sign =
false;
489 int end = val.size();
492 if (ptr < end && val[ptr] ==
'-') {
493 mantissa_sign =
true;
498 if (val[ptr] ==
'0') {
501 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
502 while (ptr < end &&
IsDigit(val[ptr])) {
509 if (ptr < end && val[ptr] ==
'.')
512 if (ptr < end &&
IsDigit(val[ptr]))
514 while (ptr < end &&
IsDigit(val[ptr])) {
522 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
525 if (ptr < end && val[ptr] ==
'+')
527 else if (ptr < end && val[ptr] ==
'-') {
528 exponent_sign =
true;
531 if (ptr < end &&
IsDigit(val[ptr])) {
532 while (ptr < end &&
IsDigit(val[ptr])) {
535 exponent = exponent * 10 + val[ptr] -
'0';
545 exponent = -exponent;
546 exponent = exponent - point_ofs + mantissa_tzeros;
550 mantissa = -mantissa;
553 exponent += decimals;
559 for (
int i=0; i < exponent; ++i) {
568 *amount_out = mantissa;
576 for (
auto ch : str) r +=
ToLower((
unsigned char)ch);
583 for (
auto ch : str) r +=
ToUpper((
unsigned char)ch);
589 if (str.empty())
return str;
597 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
598 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
599 rv.reserve(s.
size() * 2);
601 rv.push_back(hexmap[v >> 4]);
602 rv.push_back(hexmap[v & 15]);