18 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
26 CHARS_ALPHA_NUM +
".-_",
28 CHARS_ALPHA_NUM +
"!*'();:@&=+$,/?#[]-_.~%",
32 std::string strResult;
33 for (std::string::size_type i = 0; i < str.size(); i++) {
34 if (
SAFE_CHARS[rule].find(str[i]) != std::string::npos) {
35 strResult.push_back(str[i]);
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
46 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -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, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -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,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 bool IsHex(
const std::string &str) {
65 for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
70 return (str.size() > 0) && (str.size() % 2 == 0);
74 size_t starting_location = 0;
75 if (str.size() > 2 && *str.begin() ==
'0' && *(str.begin() + 1) ==
'x') {
76 starting_location = 2;
78 for (
auto c : str.substr(starting_location)) {
84 return (str.size() > starting_location);
87 std::vector<uint8_t>
ParseHex(
const char *psz) {
89 std::vector<uint8_t> vch;
95 if (c == (
signed char)-1) {
100 if (c == (
signed char)-1) {
109 std::vector<uint8_t>
ParseHex(
const std::string &str) {
114 size_t colon = in.find_last_of(
':');
117 bool fHaveColon = colon != in.npos;
120 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
122 fHaveColon && (in.find_last_of(
':', colon - 1) != in.npos);
123 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
125 if (
ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
126 in = in.substr(0, colon);
130 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
131 hostOut = in.substr(1, in.size() - 2);
138 static const char *pbase64 =
139 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
142 str.reserve(((len + 2) / 3) * 4);
143 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, pch, pch + len);
144 while (str.size() % 4) {
151 return EncodeBase64((
const uint8_t *)str.data(), str.size());
155 static const int decode64_table[256] = {
156 -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, -1, -1,
158 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
159 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
160 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
161 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
162 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
163 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
164 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
165 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
166 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
167 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
168 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173 std::vector<uint8_t> val;
174 val.reserve(strlen(p));
176 int x = decode64_table[(uint8_t)*p];
184 std::vector<uint8_t> ret;
185 ret.reserve((val.size() * 3) / 4);
186 bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
187 val.begin(), val.end());
190 while (valid && *p != 0) {
197 valid = valid && (p - e) % 4 == 0 && p - q < 4;
199 *pf_invalid = !valid;
212 std::vector<uint8_t> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
213 return std::string((
const char *)vchRet.data(), vchRet.size());
217 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
220 str.reserve(((len + 4) / 5) * 8);
221 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, pch, pch + len);
222 while (str.size() % 8) {
229 return EncodeBase32((
const uint8_t *)str.data(), str.size());
233 static const int decode32_table[256] = {
234 -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, -1, -1,
236 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
237 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
238 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
239 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
240 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1,
241 -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, -1, -1,
243 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
244 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
245 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
251 std::vector<uint8_t> val;
252 val.reserve(strlen(p));
254 int x = decode32_table[(uint8_t)*p];
262 std::vector<uint8_t> ret;
263 ret.reserve((val.size() * 5) / 8);
264 bool valid = ConvertBits<5, 8, false>([&](uint8_t c) { ret.push_back(c); },
265 val.begin(), val.end());
268 while (valid && *p != 0) {
275 valid = valid && (p - e) % 8 == 0 && p - q < 8;
277 *pf_invalid = !valid;
290 std::vector<uint8_t> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
291 return std::string((
const char *)vchRet.data(), vchRet.size());
300 if (str.size() >= 1 && (
IsSpace(str[0]) ||
IsSpace(str[str.size() - 1]))) {
314 char *endp =
nullptr;
317 long int n = strtol(str.c_str(), &endp, 10);
325 return endp && *endp == 0 && !errno &&
326 n >= std::numeric_limits<int32_t>::min() &&
327 n <= std::numeric_limits<int32_t>::max();
334 char *endp =
nullptr;
337 long long int n = strtoll(str.c_str(), &endp, 10);
344 return endp && *endp == 0 && !errno &&
345 n >= std::numeric_limits<int64_t>::min() &&
346 n <= std::numeric_limits<int64_t>::max();
355 if (str.size() >= 1 && str[0] ==
'-') {
358 char *endp =
nullptr;
361 unsigned long int n = strtoul(str.c_str(), &endp, 10);
369 return endp && *endp == 0 && !errno &&
370 n <= std::numeric_limits<uint32_t>::max();
379 if (str.size() >= 1 && str[0] ==
'-') {
382 char *endp =
nullptr;
385 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
392 return endp && *endp == 0 && !errno &&
393 n <= std::numeric_limits<uint64_t>::max();
401 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x') {
404 std::istringstream text(str);
405 text.imbue(std::locale::classic());
411 return text.eof() && !text.fail();
416 std::stringstream out;
419 while (ptr < in.size()) {
420 size_t lineend = in.find_first_of(
'\n', ptr);
421 if (lineend == std::string::npos) {
424 const size_t linelen = lineend - ptr;
425 const size_t rem_width = width - indented;
426 if (linelen <= rem_width) {
427 out << in.substr(ptr, linelen + 1);
431 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
432 if (finalspace == std::string::npos || finalspace < ptr) {
434 finalspace = in.find_first_of(
"\n ", ptr);
435 if (finalspace == std::string::npos) {
437 out << in.substr(ptr);
441 out << in.substr(ptr, finalspace - ptr) <<
"\n";
442 if (in[finalspace] ==
'\n') {
445 out << std::string(indent,
' ');
448 ptr = finalspace + 1;
458 return strtoll(psz,
nullptr, 10);
464 return _atoi64(str.c_str());
466 return strtoll(str.c_str(),
nullptr, 10);
470 int atoi(
const std::string &str) {
471 return atoi(str.c_str());
487 int &mantissa_tzeros) {
491 for (
int i = 0; i <= mantissa_tzeros; ++i) {
498 mantissa += ch -
'0';
505 int64_t *amount_out) {
506 int64_t mantissa = 0;
507 int64_t exponent = 0;
508 int mantissa_tzeros = 0;
509 bool mantissa_sign =
false;
510 bool exponent_sign =
false;
512 int end = val.size();
515 if (ptr < end && val[ptr] ==
'-') {
516 mantissa_sign =
true;
520 if (val[ptr] ==
'0') {
523 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
524 while (ptr < end &&
IsDigit(val[ptr])) {
540 if (ptr < end && val[ptr] ==
'.') {
542 if (ptr < end &&
IsDigit(val[ptr])) {
543 while (ptr < end &&
IsDigit(val[ptr])) {
557 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E')) {
559 if (ptr < end && val[ptr] ==
'+') {
561 }
else if (ptr < end && val[ptr] ==
'-') {
562 exponent_sign =
true;
565 if (ptr < end &&
IsDigit(val[ptr])) {
566 while (ptr < end &&
IsDigit(val[ptr])) {
571 exponent = exponent * 10 + val[ptr] -
'0';
585 exponent = -exponent;
587 exponent = exponent - point_ofs + mantissa_tzeros;
591 mantissa = -mantissa;
595 exponent += decimals;
600 if (exponent >= 18) {
605 for (
int i = 0; i < exponent; ++i) {
619 *amount_out = mantissa;
627 for (
auto ch : str) {
635 for (
auto ch : str) {
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string FormatParagraph(const std::string &in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line...
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
signed char HexDigit(char c)
NODISCARD bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
static const std::string CHARS_ALPHA_NUM
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid)
std::string EncodeBase64(const uint8_t *pch, size_t len)
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
static const std::string SAFE_CHARS[]
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
int atoi(const std::string &str)
bool IsHex(const std::string &str)
Returns true if each character in str is a hex character, and has an even number of hex digits...
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
static const int64_t UPPER_BOUND
Upper bound for mantissa.
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
std::string EncodeBase32(const uint8_t *pch, size_t len)
int64_t atoi64(const char *psz)
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::vector< uint8_t > ParseHex(const char *psz)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
static NODISCARD bool ParsePrechecks(const std::string &str)
const signed char p_util_hexdigit[256]
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)