17 static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
23 CHARS_ALPHA_NUM +
".-_",
24 CHARS_ALPHA_NUM +
"!*'();:@&=+$,/?#[]-_.~%",
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)
111 size_t colon = in.find_last_of(
':');
113 bool fHaveColon = colon != in.npos;
114 bool fBracketed = fHaveColon && (in[0]==
'[' && in[colon-1]==
']');
115 bool fMultiColon = fHaveColon && (in.find_last_of(
':',colon-1) != in.npos);
116 if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
118 if (
ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
119 in = in.substr(0, colon);
123 if (in.size()>0 && in[0] ==
'[' && in[in.size()-1] ==
']')
124 hostOut = in.substr(1, in.size()-2);
131 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
134 str.reserve(((input.
size() + 2) / 3) * 4);
135 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
136 while (str.size() % 4) str +=
'=';
145 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pf_invalid)
147 static const int decode64_table[256] =
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
151 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
152 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
153 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
154 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
155 49, 50, 51, -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, -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
165 std::vector<uint8_t> val;
166 val.reserve(strlen(p));
168 int x = decode64_table[(
unsigned char)*p];
174 std::vector<unsigned char> ret;
175 ret.reserve((val.size() * 3) / 4);
176 bool valid = ConvertBits<6, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
179 while (valid && *p != 0) {
186 valid = valid && (p - e) % 4 == 0 && p - q < 4;
187 if (pf_invalid) *pf_invalid = !valid;
200 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
201 return std::string((
const char*)vchRet.data(), vchRet.size());
206 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
209 str.reserve(((input.
size() + 4) / 5) * 8);
210 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
212 while (str.size() % 8) {
224 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pf_invalid)
226 static const int decode32_table[256] =
228 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
229 -1, -1, -1, -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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
231 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
232 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
233 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
234 23, 24, 25, -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, -1, -1,
236 -1, -1, -1, -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
244 std::vector<uint8_t> val;
245 val.reserve(strlen(p));
247 int x = decode32_table[(
unsigned char)*p];
253 std::vector<unsigned char> ret;
254 ret.reserve((val.size() * 5) / 8);
255 bool valid = ConvertBits<5, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
258 while (valid && *p != 0) {
265 valid = valid && (p - e) % 8 == 0 && p - q < 8;
266 if (pf_invalid) *pf_invalid = !valid;
279 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
280 return std::string((
const char*)vchRet.data(), vchRet.size());
287 if (str.size() >= 1 && (
IsSpace(str[0]) ||
IsSpace(str[str.size()-1])))
298 char *endp =
nullptr;
300 long int n = strtol(str.c_str(), &endp, 10);
301 if(out) *out = (int32_t)n;
305 return endp && *endp == 0 && !errno &&
306 n >= std::numeric_limits<int32_t>::min() &&
307 n <= std::numeric_limits<int32_t>::max();
314 char *endp =
nullptr;
316 long long int n = strtoll(str.c_str(), &endp, 10);
317 if(out) *out = (int64_t)n;
320 return endp && *endp == 0 && !errno &&
321 n >= std::numeric_limits<int64_t>::min() &&
322 n <= std::numeric_limits<int64_t>::max();
328 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
331 if (out !=
nullptr) {
332 *out =
static_cast<uint8_t
>(u32);
341 if (str.size() >= 1 && str[0] ==
'-')
343 char *endp =
nullptr;
345 unsigned long int n = strtoul(str.c_str(), &endp, 10);
346 if(out) *out = (uint32_t)n;
350 return endp && *endp == 0 && !errno &&
351 n <= std::numeric_limits<uint32_t>::max();
358 if (str.size() >= 1 && str[0] ==
'-')
360 char *endp =
nullptr;
362 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
363 if(out) *out = (uint64_t)n;
366 return endp && *endp == 0 && !errno &&
367 n <= std::numeric_limits<uint64_t>::max();
375 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
377 std::istringstream text(str);
378 text.imbue(std::locale::classic());
381 if(out) *out = result;
382 return text.eof() && !text.fail();
387 std::stringstream out;
390 while (ptr < in.size())
392 size_t lineend = in.find_first_of(
'\n', ptr);
393 if (lineend == std::string::npos) {
396 const size_t linelen = lineend - ptr;
397 const size_t rem_width = width - indented;
398 if (linelen <= rem_width) {
399 out << in.substr(ptr, linelen + 1);
403 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
404 if (finalspace == std::string::npos || finalspace < ptr) {
406 finalspace = in.find_first_of(
"\n ", ptr);
407 if (finalspace == std::string::npos) {
409 out << in.substr(ptr);
413 out << in.substr(ptr, finalspace - ptr) <<
"\n";
414 if (in[finalspace] ==
'\n') {
417 out << std::string(indent,
' ');
420 ptr = finalspace + 1;
429 return _atoi64(str.c_str());
431 return strtoll(str.c_str(),
nullptr, 10);
435 int atoi(
const std::string& str)
437 return atoi(str.c_str());
456 for (
int i=0; i<=mantissa_tzeros; ++i) {
461 mantissa += ch -
'0';
469 int64_t mantissa = 0;
470 int64_t exponent = 0;
471 int mantissa_tzeros = 0;
472 bool mantissa_sign =
false;
473 bool exponent_sign =
false;
475 int end = val.size();
478 if (ptr < end && val[ptr] ==
'-') {
479 mantissa_sign =
true;
484 if (val[ptr] ==
'0') {
487 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
488 while (ptr < end &&
IsDigit(val[ptr])) {
495 if (ptr < end && val[ptr] ==
'.')
498 if (ptr < end &&
IsDigit(val[ptr]))
500 while (ptr < end &&
IsDigit(val[ptr])) {
508 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
511 if (ptr < end && val[ptr] ==
'+')
513 else if (ptr < end && val[ptr] ==
'-') {
514 exponent_sign =
true;
517 if (ptr < end &&
IsDigit(val[ptr])) {
518 while (ptr < end &&
IsDigit(val[ptr])) {
521 exponent = exponent * 10 + val[ptr] -
'0';
531 exponent = -exponent;
532 exponent = exponent - point_ofs + mantissa_tzeros;
536 mantissa = -mantissa;
539 exponent += decimals;
545 for (
int i=0; i < exponent; ++i) {
554 *amount_out = mantissa;
562 for (
auto ch : str) r +=
ToLower((
unsigned char)ch);
569 for (
auto ch : str) r +=
ToUpper((
unsigned char)ch);
575 if (str.empty())
return str;
583 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
584 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
585 rv.reserve(s.
size() * 2);
587 rv.push_back(hexmap[v >> 4]);
588 rv.push_back(hexmap[v & 15]);
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::deque< CInv >::iterator it
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
constexpr C * end() const noexcept
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
signed char HexDigit(char c)
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.
std::vector< unsigned char > ParseHex(const char *psz)
constexpr std::size_t size() const noexcept
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid)
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 bool ParsePrechecks(const std::string &str)
std::string EncodeBase64(Span< const unsigned char > input)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
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)
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.
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
int64_t atoi64(const std::string &str)
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
constexpr C * begin() const noexcept
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
const signed char p_util_hexdigit[256]
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
A Span is an object that can refer to a contiguous sequence of objects.
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)