17 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
31 std::string strResult;
32 for (std::string::size_type i = 0; i < str.size(); i++) {
33 if (
SAFE_CHARS[rule].find(str[i]) != std::string::npos) {
34 strResult.push_back(str[i]);
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -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, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -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,
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,
63 bool IsHex(
const std::string &str) {
64 for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
69 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 (
auto c : str.substr(starting_location)) {
83 return (str.size() > starting_location);
86 std::vector<uint8_t>
ParseHex(
const char *psz) {
88 std::vector<uint8_t> vch;
94 if (c == (
signed char)-1) {
97 auto n{uint8_t(c << 4)};
99 if (c == (
signed char)-1) {
108 std::vector<uint8_t>
ParseHex(
const std::string &str) {
112 void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut) {
113 size_t colon = in.find_last_of(
':');
116 bool fHaveColon = colon != in.npos;
119 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
121 fHaveColon && (in.find_last_of(
':', colon - 1) != in.npos);
122 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
125 in = in.substr(0, colon);
129 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
130 hostOut = in.substr(1, in.size() - 2);
137 static const char *pbase64 =
138 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
141 str.reserve(((input.
size() + 2) / 3) * 4);
142 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(),
144 while (str.size() % 4) {
151 static const int8_t decode64_table[256] = {
152 -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,
154 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
155 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
156 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
157 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
158 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
159 -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,
161 -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,
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,
169 std::vector<uint8_t> val;
170 val.reserve(strlen(p));
172 int x = decode64_table[(uint8_t)*p];
176 val.push_back(uint8_t(x));
180 std::vector<uint8_t> ret;
181 ret.reserve((val.size() * 3) / 4);
182 bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
183 val.begin(), val.end());
186 while (valid && *p != 0) {
193 valid = valid && (p - e) % 4 == 0 && p - q < 4;
195 *pf_invalid = !valid;
208 std::vector<uint8_t> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
209 return std::string((
const char *)vchRet.data(), vchRet.size());
213 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
216 str.reserve(((input.
size() + 4) / 5) * 8);
217 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(),
220 while (str.size() % 8) {
232 static const int8_t decode32_table[256] = {
233 -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,
235 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
236 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
237 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
238 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
239 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1,
240 -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,
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,
250 std::vector<uint8_t> val;
251 val.reserve(strlen(p));
253 int x = decode32_table[(uint8_t)*p];
257 val.push_back(uint8_t(x));
261 std::vector<uint8_t> ret;
262 ret.reserve((val.size() * 5) / 8);
263 bool valid = ConvertBits<5, 8, false>([&](uint8_t c) { ret.push_back(c); },
264 val.begin(), val.end());
267 while (valid && *p != 0) {
274 valid = valid && (p - e) % 8 == 0 && p - q < 8;
276 *pf_invalid = !valid;
289 std::vector<uint8_t> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
290 return std::string((
const char *)vchRet.data(), vchRet.size());
299 if (str.size() >= 1 && (
IsSpace(str[0]) ||
IsSpace(str[str.size() - 1]))) {
313 char *endp =
nullptr;
316 long int n = strtol(str.c_str(), &endp, 10);
324 return endp && *endp == 0 && !errno &&
325 n >= std::numeric_limits<int32_t>::min() &&
326 n <= std::numeric_limits<int32_t>::max();
333 char *endp =
nullptr;
336 long long int n = strtoll(str.c_str(), &endp, 10);
343 return endp && *endp == 0 && !errno &&
344 n >= std::numeric_limits<int64_t>::min() &&
345 n <= std::numeric_limits<int64_t>::max();
350 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
353 if (out !=
nullptr) {
354 *out =
static_cast<uint8_t
>(u32);
361 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
364 if (out !=
nullptr) {
365 *out =
static_cast<uint16_t
>(u32);
376 if (str.size() >= 1 && str[0] ==
'-') {
379 char *endp =
nullptr;
382 unsigned long int n = strtoul(str.c_str(), &endp, 10);
390 return endp && *endp == 0 && !errno &&
391 n <= std::numeric_limits<uint32_t>::max();
400 if (str.size() >= 1 && str[0] ==
'-') {
403 char *endp =
nullptr;
406 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
413 return endp && *endp == 0 && !errno &&
414 n <= std::numeric_limits<uint64_t>::max();
422 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x') {
425 std::istringstream text(str);
426 text.imbue(std::locale::classic());
432 return text.eof() && !text.fail();
437 std::stringstream out;
440 while (ptr < in.size()) {
441 size_t lineend = in.find_first_of(
'\n', ptr);
442 if (lineend == std::string::npos) {
445 const size_t linelen = lineend - ptr;
446 const size_t rem_width = width - indented;
447 if (linelen <= rem_width) {
448 out << in.substr(ptr, linelen + 1);
452 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
453 if (finalspace == std::string::npos || finalspace < ptr) {
455 finalspace = in.find_first_of(
"\n ", ptr);
456 if (finalspace == std::string::npos) {
458 out << in.substr(ptr);
462 out << in.substr(ptr, finalspace - ptr) <<
"\n";
463 if (in[finalspace] ==
'\n') {
466 out << std::string(indent,
' ');
469 ptr = finalspace + 1;
477 return _atoi64(str.c_str());
479 return strtoll(str.c_str(),
nullptr, 10);
483 int atoi(
const std::string &str) {
484 return atoi(str.c_str());
500 int &mantissa_tzeros) {
504 for (
int i = 0; i <= mantissa_tzeros; ++i) {
511 mantissa += ch -
'0';
518 int64_t *amount_out) {
519 int64_t mantissa = 0;
520 int64_t exponent = 0;
521 int mantissa_tzeros = 0;
522 bool mantissa_sign =
false;
523 bool exponent_sign =
false;
525 int end = val.size();
528 if (ptr < end && val[ptr] ==
'-') {
529 mantissa_sign =
true;
533 if (val[ptr] ==
'0') {
536 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
537 while (ptr < end &&
IsDigit(val[ptr])) {
553 if (ptr < end && val[ptr] ==
'.') {
555 if (ptr < end &&
IsDigit(val[ptr])) {
556 while (ptr < end &&
IsDigit(val[ptr])) {
570 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E')) {
572 if (ptr < end && val[ptr] ==
'+') {
574 }
else if (ptr < end && val[ptr] ==
'-') {
575 exponent_sign =
true;
578 if (ptr < end &&
IsDigit(val[ptr])) {
579 while (ptr < end &&
IsDigit(val[ptr])) {
584 exponent = exponent * 10 + val[ptr] -
'0';
598 exponent = -exponent;
600 exponent = exponent - point_ofs + mantissa_tzeros;
604 mantissa = -mantissa;
608 exponent += decimals;
613 if (exponent >= 18) {
618 for (
int i = 0; i < exponent; ++i) {
632 *amount_out = mantissa;
640 for (
auto ch : str) {
648 for (
auto ch : str) {
663 std::string rv(s.
size() * 2,
'\0');
664 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
665 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
666 auto it = rv.begin();
667 for (uint8_t v : s) {
668 *it++ = hexmap[v >> 4];
669 *it++ = hexmap[v & 15];
constexpr std::size_t size() const noexcept
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, 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.