Bitcoin ABC  0.22.13
P2P Digital Currency
strencodings.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <util/strencodings.h>
7 #include <util/string.h>
8 
9 #include <tinyformat.h>
10 
11 #include <algorithm>
12 #include <cerrno>
13 #include <cstdlib>
14 #include <cstring>
15 #include <limits>
16 
17 static const std::string CHARS_ALPHA_NUM =
18  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
19 
20 static const std::string SAFE_CHARS[] = {
21  // SAFE_CHARS_DEFAULT
22  CHARS_ALPHA_NUM + " .,;-_/:[email protected]()",
23  // SAFE_CHARS_UA_COMMENT
25  // SAFE_CHARS_FILENAME
26  CHARS_ALPHA_NUM + ".-_",
27  // SAFE_CHARS_URI
28  CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%",
29 };
30 
31 std::string SanitizeString(const std::string &str, int rule) {
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]);
36  }
37  }
38  return strResult;
39 }
40 
41 const signed char p_util_hexdigit[256] = {
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,
58 };
59 
60 signed char HexDigit(char c) {
61  return p_util_hexdigit[(uint8_t)c];
62 }
63 
64 bool IsHex(const std::string &str) {
65  for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
66  if (HexDigit(*it) < 0) {
67  return false;
68  }
69  }
70  return (str.size() > 0) && (str.size() % 2 == 0);
71 }
72 
73 bool IsHexNumber(const std::string &str) {
74  size_t starting_location = 0;
75  if (str.size() > 2 && *str.begin() == '0' && *(str.begin() + 1) == 'x') {
76  starting_location = 2;
77  }
78  for (auto c : str.substr(starting_location)) {
79  if (HexDigit(c) < 0) {
80  return false;
81  }
82  }
83  // Return false for empty string or "0x".
84  return (str.size() > starting_location);
85 }
86 
87 std::vector<uint8_t> ParseHex(const char *psz) {
88  // convert hex dump to vector
89  std::vector<uint8_t> vch;
90  while (true) {
91  while (IsSpace(*psz)) {
92  psz++;
93  }
94  signed char c = HexDigit(*psz++);
95  if (c == (signed char)-1) {
96  break;
97  }
98  uint8_t n = (c << 4);
99  c = HexDigit(*psz++);
100  if (c == (signed char)-1) {
101  break;
102  }
103  n |= c;
104  vch.push_back(n);
105  }
106  return vch;
107 }
108 
109 std::vector<uint8_t> ParseHex(const std::string &str) {
110  return ParseHex(str.c_str());
111 }
112 
113 void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
114  size_t colon = in.find_last_of(':');
115  // if a : is found, and it either follows a [...], or no other : is in the
116  // string, treat it as port separator
117  bool fHaveColon = colon != in.npos;
118  // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is
119  // safe
120  bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']');
121  bool fMultiColon =
122  fHaveColon && (in.find_last_of(':', colon - 1) != in.npos);
123  if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
124  int32_t n;
125  if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
126  in = in.substr(0, colon);
127  portOut = n;
128  }
129  }
130  if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') {
131  hostOut = in.substr(1, in.size() - 2);
132  } else {
133  hostOut = in;
134  }
135 }
136 
137 std::string EncodeBase64(const uint8_t *pch, size_t len) {
138  static const char *pbase64 =
139  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
140 
141  std::string str;
142  str.reserve(((len + 2) / 3) * 4);
143  ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, pch, pch + len);
144  while (str.size() % 4) {
145  str += '=';
146  }
147  return str;
148 }
149 
150 std::string EncodeBase64(const std::string &str) {
151  return EncodeBase64((const uint8_t *)str.data(), str.size());
152 }
153 
154 std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid) {
155  static const int decode64_table[256] = {
156  -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,
158  -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
159  58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
160  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
161  25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
162  37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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,
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};
171 
172  const char *e = p;
173  std::vector<uint8_t> val;
174  val.reserve(strlen(p));
175  while (*p != 0) {
176  int x = decode64_table[(uint8_t)*p];
177  if (x == -1) {
178  break;
179  }
180  val.push_back(x);
181  ++p;
182  }
183 
184  std::vector<uint8_t> ret;
185  ret.reserve((val.size() * 3) / 4);
186  bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
187  val.begin(), val.end());
188 
189  const char *q = p;
190  while (valid && *p != 0) {
191  if (*p != '=') {
192  valid = false;
193  break;
194  }
195  ++p;
196  }
197  valid = valid && (p - e) % 4 == 0 && p - q < 4;
198  if (pf_invalid) {
199  *pf_invalid = !valid;
200  }
201 
202  return ret;
203 }
204 
205 std::string DecodeBase64(const std::string &str, bool *pf_invalid) {
206  if (!ValidAsCString(str)) {
207  if (pf_invalid) {
208  *pf_invalid = true;
209  }
210  return {};
211  }
212  std::vector<uint8_t> vchRet = DecodeBase64(str.c_str(), pf_invalid);
213  return std::string((const char *)vchRet.data(), vchRet.size());
214 }
215 
216 std::string EncodeBase32(const uint8_t *pch, size_t len) {
217  static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
218 
219  std::string str;
220  str.reserve(((len + 4) / 5) * 8);
221  ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, pch, pch + len);
222  while (str.size() % 8) {
223  str += '=';
224  }
225  return str;
226 }
227 
228 std::string EncodeBase32(const std::string &str) {
229  return EncodeBase32((const uint8_t *)str.data(), str.size());
230 }
231 
232 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid) {
233  static const int 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,
248  -1, -1, -1, -1};
249 
250  const char *e = p;
251  std::vector<uint8_t> val;
252  val.reserve(strlen(p));
253  while (*p != 0) {
254  int x = decode32_table[(uint8_t)*p];
255  if (x == -1) {
256  break;
257  }
258  val.push_back(x);
259  ++p;
260  }
261 
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());
266 
267  const char *q = p;
268  while (valid && *p != 0) {
269  if (*p != '=') {
270  valid = false;
271  break;
272  }
273  ++p;
274  }
275  valid = valid && (p - e) % 8 == 0 && p - q < 8;
276  if (pf_invalid) {
277  *pf_invalid = !valid;
278  }
279 
280  return ret;
281 }
282 
283 std::string DecodeBase32(const std::string &str, bool *pf_invalid) {
284  if (!ValidAsCString(str)) {
285  if (pf_invalid) {
286  *pf_invalid = true;
287  }
288  return {};
289  }
290  std::vector<uint8_t> vchRet = DecodeBase32(str.c_str(), pf_invalid);
291  return std::string((const char *)vchRet.data(), vchRet.size());
292 }
293 
294 NODISCARD static bool ParsePrechecks(const std::string &str) {
295  // No empty string allowed
296  if (str.empty()) {
297  return false;
298  }
299  // No padding allowed
300  if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size() - 1]))) {
301  return false;
302  }
303  // No embedded NUL characters allowed
304  if (!ValidAsCString(str)) {
305  return false;
306  }
307  return true;
308 }
309 
310 bool ParseInt32(const std::string &str, int32_t *out) {
311  if (!ParsePrechecks(str)) {
312  return false;
313  }
314  char *endp = nullptr;
315  // strtol will not set errno if valid
316  errno = 0;
317  long int n = strtol(str.c_str(), &endp, 10);
318  if (out) {
319  *out = (int32_t)n;
320  }
321  // Note that strtol returns a *long int*, so even if strtol doesn't report
322  // an over/underflow we still have to check that the returned value is
323  // within the range of an *int32_t*. On 64-bit platforms the size of these
324  // types may be different.
325  return endp && *endp == 0 && !errno &&
326  n >= std::numeric_limits<int32_t>::min() &&
327  n <= std::numeric_limits<int32_t>::max();
328 }
329 
330 bool ParseInt64(const std::string &str, int64_t *out) {
331  if (!ParsePrechecks(str)) {
332  return false;
333  }
334  char *endp = nullptr;
335  // strtoll will not set errno if valid
336  errno = 0;
337  long long int n = strtoll(str.c_str(), &endp, 10);
338  if (out) {
339  *out = (int64_t)n;
340  }
341  // Note that strtoll returns a *long long int*, so even if strtol doesn't
342  // report an over/underflow we still have to check that the returned value
343  // is within the range of an *int64_t*.
344  return endp && *endp == 0 && !errno &&
345  n >= std::numeric_limits<int64_t>::min() &&
346  n <= std::numeric_limits<int64_t>::max();
347 }
348 
349 bool ParseUInt32(const std::string &str, uint32_t *out) {
350  if (!ParsePrechecks(str)) {
351  return false;
352  }
353  // Reject negative values, unfortunately strtoul accepts these by default if
354  // they fit in the range
355  if (str.size() >= 1 && str[0] == '-') {
356  return false;
357  }
358  char *endp = nullptr;
359  // strtoul will not set errno if valid
360  errno = 0;
361  unsigned long int n = strtoul(str.c_str(), &endp, 10);
362  if (out) {
363  *out = (uint32_t)n;
364  }
365  // Note that strtoul returns a *unsigned long int*, so even if it doesn't
366  // report an over/underflow we still have to check that the returned value
367  // is within the range of an *uint32_t*. On 64-bit platforms the size of
368  // these types may be different.
369  return endp && *endp == 0 && !errno &&
370  n <= std::numeric_limits<uint32_t>::max();
371 }
372 
373 bool ParseUInt64(const std::string &str, uint64_t *out) {
374  if (!ParsePrechecks(str)) {
375  return false;
376  }
377  // Reject negative values, unfortunately strtoull accepts these by default
378  // if they fit in the range
379  if (str.size() >= 1 && str[0] == '-') {
380  return false;
381  }
382  char *endp = nullptr;
383  // strtoull will not set errno if valid
384  errno = 0;
385  unsigned long long int n = strtoull(str.c_str(), &endp, 10);
386  if (out) {
387  *out = (uint64_t)n;
388  }
389  // Note that strtoull returns a *unsigned long long int*, so even if it
390  // doesn't report an over/underflow we still have to check that the returned
391  // value is within the range of an *uint64_t*.
392  return endp && *endp == 0 && !errno &&
393  n <= std::numeric_limits<uint64_t>::max();
394 }
395 
396 bool ParseDouble(const std::string &str, double *out) {
397  if (!ParsePrechecks(str)) {
398  return false;
399  }
400  // No hexadecimal floats allowed
401  if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') {
402  return false;
403  }
404  std::istringstream text(str);
405  text.imbue(std::locale::classic());
406  double result;
407  text >> result;
408  if (out) {
409  *out = result;
410  }
411  return text.eof() && !text.fail();
412 }
413 
414 std::string FormatParagraph(const std::string &in, size_t width,
415  size_t indent) {
416  std::stringstream out;
417  size_t ptr = 0;
418  size_t indented = 0;
419  while (ptr < in.size()) {
420  size_t lineend = in.find_first_of('\n', ptr);
421  if (lineend == std::string::npos) {
422  lineend = in.size();
423  }
424  const size_t linelen = lineend - ptr;
425  const size_t rem_width = width - indented;
426  if (linelen <= rem_width) {
427  out << in.substr(ptr, linelen + 1);
428  ptr = lineend + 1;
429  indented = 0;
430  } else {
431  size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
432  if (finalspace == std::string::npos || finalspace < ptr) {
433  // No place to break; just include the entire word and move on
434  finalspace = in.find_first_of("\n ", ptr);
435  if (finalspace == std::string::npos) {
436  // End of the string, just add it and break
437  out << in.substr(ptr);
438  break;
439  }
440  }
441  out << in.substr(ptr, finalspace - ptr) << "\n";
442  if (in[finalspace] == '\n') {
443  indented = 0;
444  } else if (indent) {
445  out << std::string(indent, ' ');
446  indented = indent;
447  }
448  ptr = finalspace + 1;
449  }
450  }
451  return out.str();
452 }
453 
454 int64_t atoi64(const char *psz) {
455 #ifdef _MSC_VER
456  return _atoi64(psz);
457 #else
458  return strtoll(psz, nullptr, 10);
459 #endif
460 }
461 
462 int64_t atoi64(const std::string &str) {
463 #ifdef _MSC_VER
464  return _atoi64(str.c_str());
465 #else
466  return strtoll(str.c_str(), nullptr, 10);
467 #endif
468 }
469 
470 int atoi(const std::string &str) {
471  return atoi(str.c_str());
472 }
473 
483 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
484 
486 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa,
487  int &mantissa_tzeros) {
488  if (ch == '0') {
489  ++mantissa_tzeros;
490  } else {
491  for (int i = 0; i <= mantissa_tzeros; ++i) {
492  // overflow
493  if (mantissa > (UPPER_BOUND / 10LL)) {
494  return false;
495  }
496  mantissa *= 10;
497  }
498  mantissa += ch - '0';
499  mantissa_tzeros = 0;
500  }
501  return true;
502 }
503 
504 bool ParseFixedPoint(const std::string &val, int decimals,
505  int64_t *amount_out) {
506  int64_t mantissa = 0;
507  int64_t exponent = 0;
508  int mantissa_tzeros = 0;
509  bool mantissa_sign = false;
510  bool exponent_sign = false;
511  int ptr = 0;
512  int end = val.size();
513  int point_ofs = 0;
514 
515  if (ptr < end && val[ptr] == '-') {
516  mantissa_sign = true;
517  ++ptr;
518  }
519  if (ptr < end) {
520  if (val[ptr] == '0') {
521  // pass single 0
522  ++ptr;
523  } else if (val[ptr] >= '1' && val[ptr] <= '9') {
524  while (ptr < end && IsDigit(val[ptr])) {
525  if (!ProcessMantissaDigit(val[ptr], mantissa,
526  mantissa_tzeros)) {
527  // overflow
528  return false;
529  }
530  ++ptr;
531  }
532  } else {
533  // missing expected digit
534  return false;
535  }
536  } else {
537  // empty string or loose '-'
538  return false;
539  }
540  if (ptr < end && val[ptr] == '.') {
541  ++ptr;
542  if (ptr < end && IsDigit(val[ptr])) {
543  while (ptr < end && IsDigit(val[ptr])) {
544  if (!ProcessMantissaDigit(val[ptr], mantissa,
545  mantissa_tzeros)) {
546  // overflow
547  return false;
548  }
549  ++ptr;
550  ++point_ofs;
551  }
552  } else {
553  // missing expected digit
554  return false;
555  }
556  }
557  if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E')) {
558  ++ptr;
559  if (ptr < end && val[ptr] == '+') {
560  ++ptr;
561  } else if (ptr < end && val[ptr] == '-') {
562  exponent_sign = true;
563  ++ptr;
564  }
565  if (ptr < end && IsDigit(val[ptr])) {
566  while (ptr < end && IsDigit(val[ptr])) {
567  if (exponent > (UPPER_BOUND / 10LL)) {
568  // overflow
569  return false;
570  }
571  exponent = exponent * 10 + val[ptr] - '0';
572  ++ptr;
573  }
574  } else {
575  // missing expected digit
576  return false;
577  }
578  }
579  if (ptr != end) {
580  // trailing garbage
581  return false;
582  }
583  // finalize exponent
584  if (exponent_sign) {
585  exponent = -exponent;
586  }
587  exponent = exponent - point_ofs + mantissa_tzeros;
588 
589  // finalize mantissa
590  if (mantissa_sign) {
591  mantissa = -mantissa;
592  }
593 
594  // convert to one 64-bit fixed-point value
595  exponent += decimals;
596  if (exponent < 0) {
597  // cannot represent values smaller than 10^-decimals
598  return false;
599  }
600  if (exponent >= 18) {
601  // cannot represent values larger than or equal to 10^(18-decimals)
602  return false;
603  }
604 
605  for (int i = 0; i < exponent; ++i) {
606  if (mantissa > (UPPER_BOUND / 10LL) ||
607  mantissa < -(UPPER_BOUND / 10LL)) {
608  // overflow
609  return false;
610  }
611  mantissa *= 10;
612  }
613  if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND) {
614  // overflow
615  return false;
616  }
617 
618  if (amount_out) {
619  *amount_out = mantissa;
620  }
621 
622  return true;
623 }
624 
625 std::string ToLower(const std::string &str) {
626  std::string r;
627  for (auto ch : str) {
628  r += ToLower(ch);
629  }
630  return r;
631 }
632 
633 std::string ToUpper(const std::string &str) {
634  std::string r;
635  for (auto ch : str) {
636  r += ToUpper(ch);
637  }
638  return r;
639 }
640 
641 std::string Capitalize(std::string str) {
642  if (str.empty()) {
643  return str;
644  }
645  str[0] = ToUpper(str.front());
646  return str;
647 }
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::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
signed char HexDigit(char c)
NODISCARD bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:60
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.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:74
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid)
std::string EncodeBase64(const uint8_t *pch, size_t len)
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 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.
#define NODISCARD
Definition: attributes.h:18
int atoi(const std::string &str)
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...
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.
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
std::string EncodeBase32(const uint8_t *pch, size_t len)
int64_t atoi64(const char *psz)
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::vector< uint8_t > ParseHex(const char *psz)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:90
static NODISCARD bool ParsePrechecks(const std::string &str)
const signed char p_util_hexdigit[256]
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)