16static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
28 std::string strResult;
29 for (std::string::size_type i = 0; i < str.size(); i++)
31 if (
SAFE_CHARS[rule].find(str[i]) != std::string::npos)
32 strResult.push_back(str[i]);
38{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
42 -1,0xa,0xb,0xc,0xd,0xe,0xf,-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,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
45 -1,-1,-1,-1,-1,-1,-1,-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, };
60bool IsHex(
const std::string& str)
62 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
67 return (str.size() > 0) && (str.size()%2 == 0);
72 size_t starting_location = 0;
73 if (str.size() > 2 && *str.begin() ==
'0' && *(str.begin()+1) ==
'x') {
74 starting_location = 2;
76 for (
const char c : str.substr(starting_location)) {
80 return (str.size() > starting_location);
83std::vector<unsigned char>
ParseHex(
const char* psz)
86 std::vector<unsigned char> vch;
92 if (c == (
signed char)-1)
94 unsigned char n = (c << 4);
96 if (c == (
signed char)-1)
104std::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)) {
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);
132 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
135 str.reserve(((input.
size() + 2) / 3) * 4);
136 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
137 while (str.size() % 4) str +=
'=';
146std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pf_invalid)
148 static const int decode64_table[256] =
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
153 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
154 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
155 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
156 49, 50, 51, -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, -1, -1, -1, -1,
162 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
166 std::vector<uint8_t> val;
167 val.reserve(strlen(p));
169 int x = decode64_table[(
unsigned char)*p];
175 std::vector<unsigned char> ret;
176 ret.reserve((val.size() * 3) / 4);
177 bool valid = ConvertBits<6, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
180 while (valid && *p != 0) {
187 valid = valid && (p - e) % 4 == 0 && p - q < 4;
188 if (pf_invalid) *pf_invalid = !valid;
201 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str(), pf_invalid);
202 return std::string((
const char*)vchRet.data(), vchRet.size());
207 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
210 str.reserve(((input.
size() + 4) / 5) * 8);
211 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
213 while (str.size() % 8) {
225std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pf_invalid)
227 static const int decode32_table[256] =
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
231 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
232 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
233 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
234 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
235 23, 24, 25, -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, -1, -1, -1, -1,
241 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
245 std::vector<uint8_t> val;
246 val.reserve(strlen(p));
248 int x = decode32_table[(
unsigned char)*p];
254 std::vector<unsigned char> ret;
255 ret.reserve((val.size() * 5) / 8);
256 bool valid = ConvertBits<5, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
259 while (valid && *p != 0) {
266 valid = valid && (p - e) % 8 == 0 && p - q < 8;
267 if (pf_invalid) *pf_invalid = !valid;
280 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str(), pf_invalid);
281 return std::string((
const char*)vchRet.data(), vchRet.size());
286bool ParseIntegral(
const std::string& str,
T* out)
288 static_assert(std::is_integral<T>::value);
291 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
294 const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
298 if (out !=
nullptr) {
307 return ParseIntegral<int32_t>(str, out);
312 return ParseIntegral<int64_t>(str, out);
317 return ParseIntegral<uint8_t>(str, out);
322 return ParseIntegral<uint16_t>(str, out);
327 return ParseIntegral<uint32_t>(str, out);
332 return ParseIntegral<uint64_t>(str, out);
337 std::stringstream out;
340 while (ptr < in.size())
342 size_t lineend = in.find_first_of(
'\n', ptr);
343 if (lineend == std::string::npos) {
346 const size_t linelen = lineend - ptr;
347 const size_t rem_width = width - indented;
348 if (linelen <= rem_width) {
349 out << in.substr(ptr, linelen + 1);
353 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
354 if (finalspace == std::string::npos || finalspace < ptr) {
356 finalspace = in.find_first_of(
"\n ", ptr);
357 if (finalspace == std::string::npos) {
359 out << in.substr(ptr);
363 out << in.substr(ptr, finalspace - ptr) <<
"\n";
364 if (in[finalspace] ==
'\n') {
367 out << std::string(indent,
' ');
370 ptr = finalspace + 1;
392 for (
int i=0; i<=mantissa_tzeros; ++i) {
397 mantissa += ch -
'0';
405 int64_t mantissa = 0;
406 int64_t exponent = 0;
407 int mantissa_tzeros = 0;
408 bool mantissa_sign =
false;
409 bool exponent_sign =
false;
411 int end = val.size();
414 if (ptr < end && val[ptr] ==
'-') {
415 mantissa_sign =
true;
420 if (val[ptr] ==
'0') {
423 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
424 while (ptr < end &&
IsDigit(val[ptr])) {
431 if (ptr < end && val[ptr] ==
'.')
434 if (ptr < end &&
IsDigit(val[ptr]))
436 while (ptr < end &&
IsDigit(val[ptr])) {
444 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
447 if (ptr < end && val[ptr] ==
'+')
449 else if (ptr < end && val[ptr] ==
'-') {
450 exponent_sign =
true;
453 if (ptr < end &&
IsDigit(val[ptr])) {
454 while (ptr < end &&
IsDigit(val[ptr])) {
457 exponent = exponent * 10 + val[ptr] -
'0';
467 exponent = -exponent;
468 exponent = exponent - point_ofs + mantissa_tzeros;
472 mantissa = -mantissa;
475 exponent += decimals;
481 for (
int i=0; i < exponent; ++i) {
490 *amount_out = mantissa;
498 for (
auto ch : str) r +=
ToLower((
unsigned char)ch);
505 for (
auto ch : str) r +=
ToUpper((
unsigned char)ch);
511 if (str.empty())
return str;
518 std::string rv(s.
size() * 2,
'\0');
519 static constexpr char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
520 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
521 auto it = rv.begin();
522 for (uint8_t v : s) {
523 *it++ = hexmap[v >> 4];
524 *it++ = 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 * begin() const noexcept
constexpr C * end() const noexcept
#define T(expected, seed, data)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
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 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.
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< unsigned char > DecodeBase64(const char *p, bool *pf_invalid)
std::string EncodeBase64(Span< const unsigned char > input)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
std::vector< unsigned char > ParseHex(const char *psz)
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
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)
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
signed char HexDigit(char c)
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.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer 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
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.