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) {
152 static const int8_t decode64_table[256] = {
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
155 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
156 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
157 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
158 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
159 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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,
166 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170 std::vector<uint8_t> val;
171 val.reserve(strlen(p));
173 int x = decode64_table[(uint8_t)*p];
177 val.push_back(uint8_t(x));
181 std::vector<uint8_t> ret;
182 ret.reserve((val.size() * 3) / 4);
183 bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
184 val.begin(), val.end());
187 while (valid && *p != 0) {
194 valid = valid && (p - e) % 4 == 0 && p - q < 4;
196 *pf_invalid = !valid;
209 std::vector<uint8_t> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
210 return std::string((
const char *)vchRet.data(), vchRet.size());
214 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
217 str.reserve(((input.
size() + 4) / 5) * 8);
218 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(),
221 while (str.size() % 8) {
233 static const int8_t 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];
258 val.push_back(uint8_t(x));
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();
351 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
354 if (out !=
nullptr) {
355 *out =
static_cast<uint8_t
>(u32);
362 if (!
ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
365 if (out !=
nullptr) {
366 *out =
static_cast<uint16_t
>(u32);
377 if (str.size() >= 1 && str[0] ==
'-') {
380 char *endp =
nullptr;
383 unsigned long int n = strtoul(str.c_str(), &endp, 10);
391 return endp && *endp == 0 && !errno &&
392 n <= std::numeric_limits<uint32_t>::max();
401 if (str.size() >= 1 && str[0] ==
'-') {
404 char *endp =
nullptr;
407 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
414 return endp && *endp == 0 && !errno &&
415 n <= std::numeric_limits<uint64_t>::max();
423 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x') {
426 std::istringstream text(str);
427 text.imbue(std::locale::classic());
433 return text.eof() && !text.fail();
438 std::stringstream out;
441 while (ptr < in.size()) {
442 size_t lineend = in.find_first_of(
'\n', ptr);
443 if (lineend == std::string::npos) {
446 const size_t linelen = lineend - ptr;
447 const size_t rem_width = width - indented;
448 if (linelen <= rem_width) {
449 out << in.substr(ptr, linelen + 1);
453 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
454 if (finalspace == std::string::npos || finalspace < ptr) {
456 finalspace = in.find_first_of(
"\n ", ptr);
457 if (finalspace == std::string::npos) {
459 out << in.substr(ptr);
463 out << in.substr(ptr, finalspace - ptr) <<
"\n";
464 if (in[finalspace] ==
'\n') {
467 out << std::string(indent,
' ');
470 ptr = finalspace + 1;
478 return _atoi64(str.c_str());
480 return strtoll(str.c_str(),
nullptr, 10);
484 int atoi(
const std::string &str) {
485 return atoi(str.c_str());
501 int &mantissa_tzeros) {
505 for (
int i = 0; i <= mantissa_tzeros; ++i) {
512 mantissa += ch -
'0';
519 int64_t *amount_out) {
520 int64_t mantissa = 0;
521 int64_t exponent = 0;
522 int mantissa_tzeros = 0;
523 bool mantissa_sign =
false;
524 bool exponent_sign =
false;
526 int end = val.size();
529 if (ptr < end && val[ptr] ==
'-') {
530 mantissa_sign =
true;
534 if (val[ptr] ==
'0') {
537 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
538 while (ptr < end &&
IsDigit(val[ptr])) {
554 if (ptr < end && val[ptr] ==
'.') {
556 if (ptr < end &&
IsDigit(val[ptr])) {
557 while (ptr < end &&
IsDigit(val[ptr])) {
571 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E')) {
573 if (ptr < end && val[ptr] ==
'+') {
575 }
else if (ptr < end && val[ptr] ==
'-') {
576 exponent_sign =
true;
579 if (ptr < end &&
IsDigit(val[ptr])) {
580 while (ptr < end &&
IsDigit(val[ptr])) {
585 exponent = exponent * 10 + val[ptr] -
'0';
599 exponent = -exponent;
601 exponent = exponent - point_ofs + mantissa_tzeros;
605 mantissa = -mantissa;
609 exponent += decimals;
614 if (exponent >= 18) {
619 for (
int i = 0; i < exponent; ++i) {
633 *amount_out = mantissa;
641 for (
auto ch : str) {
649 for (
auto ch : str) {
664 std::string rv(s.
size() * 2,
'\0');
665 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
666 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
667 auto it = rv.begin();
668 for (uint8_t v : s) {
669 *it++ = hexmap[v >> 4];
670 *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.