18 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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) {
98 auto n{uint8_t(c << 4)};
100 if (c == (
signed char)-1) {
109 std::vector<uint8_t>
ParseHex(
const std::string &str) {
113 void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut) {
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)) {
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(((input.
size() + 2) / 3) * 4);
143 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(),
145 while (str.size() % 4) {
156 static const int8_t decode64_table[256] = {
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
160 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
161 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
162 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
163 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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,
170 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174 std::vector<uint8_t> val;
175 val.reserve(strlen(p));
177 int x = decode64_table[(uint8_t)*p];
181 val.push_back(uint8_t(x));
185 std::vector<uint8_t> ret;
186 ret.reserve((val.size() * 3) / 4);
187 bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
188 val.begin(), val.end());
191 while (valid && *p != 0) {
198 valid = valid && (p - e) % 4 == 0 && p - q < 4;
200 *pf_invalid = !valid;
213 std::vector<uint8_t> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
214 return std::string((
const char *)vchRet.data(), vchRet.size());
218 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
221 str.reserve(((input.
size() + 4) / 5) * 8);
222 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(),
225 while (str.size() % 8) {
237 static const int8_t decode32_table[256] = {
238 -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,
240 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
241 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
242 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
243 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
244 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -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,
248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
249 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
250 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
251 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
255 std::vector<uint8_t> val;
256 val.reserve(strlen(p));
258 int x = decode32_table[(uint8_t)*p];
262 val.push_back(uint8_t(x));
266 std::vector<uint8_t> ret;
267 ret.reserve((val.size() * 5) / 8);
268 bool valid = ConvertBits<5, 8, false>([&](uint8_t c) { ret.push_back(c); },
269 val.begin(), val.end());
272 while (valid && *p != 0) {
279 valid = valid && (p - e) % 8 == 0 && p - q < 8;
281 *pf_invalid = !valid;
294 std::vector<uint8_t> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
295 return std::string((
const char *)vchRet.data(), vchRet.size());
304 if (str.size() >= 1 && (
IsSpace(str[0]) ||
IsSpace(str[str.size() - 1]))) {
318 char *endp =
nullptr;
321 long int n = strtol(str.c_str(), &endp, 10);
329 return endp && *endp == 0 && !errno &&
330 n >= std::numeric_limits<int32_t>::min() &&
331 n <= std::numeric_limits<int32_t>::max();
338 char *endp =
nullptr;
341 long long int n = strtoll(str.c_str(), &endp, 10);
348 return endp && *endp == 0 && !errno &&
349 n >= std::numeric_limits<int64_t>::min() &&
350 n <= std::numeric_limits<int64_t>::max();
355 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
358 if (out !=
nullptr) {
359 *out =
static_cast<uint8_t
>(u32);
366 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
369 if (out !=
nullptr) {
370 *out =
static_cast<uint16_t
>(u32);
381 if (str.size() >= 1 && str[0] ==
'-') {
384 char *endp =
nullptr;
387 unsigned long int n = strtoul(str.c_str(), &endp, 10);
395 return endp && *endp == 0 && !errno &&
396 n <= std::numeric_limits<uint32_t>::max();
405 if (str.size() >= 1 && str[0] ==
'-') {
408 char *endp =
nullptr;
411 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
418 return endp && *endp == 0 && !errno &&
419 n <= std::numeric_limits<uint64_t>::max();
427 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x') {
430 std::istringstream text(str);
431 text.imbue(std::locale::classic());
437 return text.eof() && !text.fail();
442 std::stringstream out;
445 while (ptr < in.size()) {
446 size_t lineend = in.find_first_of(
'\n', ptr);
447 if (lineend == std::string::npos) {
450 const size_t linelen = lineend - ptr;
451 const size_t rem_width = width - indented;
452 if (linelen <= rem_width) {
453 out << in.substr(ptr, linelen + 1);
457 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
458 if (finalspace == std::string::npos || finalspace < ptr) {
460 finalspace = in.find_first_of(
"\n ", ptr);
461 if (finalspace == std::string::npos) {
463 out << in.substr(ptr);
467 out << in.substr(ptr, finalspace - ptr) <<
"\n";
468 if (in[finalspace] ==
'\n') {
471 out << std::string(indent,
' ');
474 ptr = finalspace + 1;
482 return _atoi64(str.c_str());
484 return strtoll(str.c_str(),
nullptr, 10);
488 int atoi(
const std::string &str) {
489 return atoi(str.c_str());
505 int &mantissa_tzeros) {
509 for (
int i = 0; i <= mantissa_tzeros; ++i) {
516 mantissa += ch -
'0';
523 int64_t *amount_out) {
524 int64_t mantissa = 0;
525 int64_t exponent = 0;
526 int mantissa_tzeros = 0;
527 bool mantissa_sign =
false;
528 bool exponent_sign =
false;
530 int end = val.size();
533 if (ptr < end && val[ptr] ==
'-') {
534 mantissa_sign =
true;
538 if (val[ptr] ==
'0') {
541 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
542 while (ptr < end &&
IsDigit(val[ptr])) {
558 if (ptr < end && val[ptr] ==
'.') {
560 if (ptr < end &&
IsDigit(val[ptr])) {
561 while (ptr < end &&
IsDigit(val[ptr])) {
575 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E')) {
577 if (ptr < end && val[ptr] ==
'+') {
579 }
else if (ptr < end && val[ptr] ==
'-') {
580 exponent_sign =
true;
583 if (ptr < end &&
IsDigit(val[ptr])) {
584 while (ptr < end &&
IsDigit(val[ptr])) {
589 exponent = exponent * 10 + val[ptr] -
'0';
603 exponent = -exponent;
605 exponent = exponent - point_ofs + mantissa_tzeros;
609 mantissa = -mantissa;
613 exponent += decimals;
618 if (exponent >= 18) {
623 for (
int i = 0; i < exponent; ++i) {
637 *amount_out = mantissa;
645 for (
auto ch : str) {
653 for (
auto ch : str) {
669 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
670 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
671 rv.reserve(s.
size() * 2);
672 for (uint8_t v : s) {
673 rv.push_back(hexmap[v >> 4]);
674 rv.push_back(hexmap[v & 15]);
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) uint8_t member types only.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
static const std::string SAFE_CHARS[]
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string EncodeBase64(Span< const uint8_t > input)
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.
static bool ParsePrechecks(const std::string &str)
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
const signed char p_util_hexdigit[256]
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
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.
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid)
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
signed char HexDigit(char c)
int atoi(const std::string &str)
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
static const int64_t UPPER_BOUND
Upper bound for mantissa.
int64_t atoi64(const std::string &str)
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
static const std::string CHARS_ALPHA_NUM
std::vector< uint8_t > ParseHex(const char *psz)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
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.