Bitcoin ABC  0.24.7
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 + " .,;-_/:?@()",
23  // SAFE_CHARS_UA_COMMENT
24  CHARS_ALPHA_NUM + " .,;-_?@",
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(Span<const uint8_t> input) {
138  static const char *pbase64 =
139  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
140 
141  std::string str;
142  str.reserve(((input.size() + 2) / 3) * 4);
143  ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(),
144  input.end());
145  while (str.size() % 4) {
146  str += '=';
147  }
148  return str;
149 }
150 
151 std::string EncodeBase64(const std::string &str) {
152  return EncodeBase64(MakeUCharSpan(str));
153 }
154 
155 std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid) {
156  static const int 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,
171  -1, -1, -1, -1};
172 
173  const char *e = p;
174  std::vector<uint8_t> val;
175  val.reserve(strlen(p));
176  while (*p != 0) {
177  int x = decode64_table[(uint8_t)*p];
178  if (x == -1) {
179  break;
180  }
181  val.push_back(x);
182  ++p;
183  }
184 
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());
189 
190  const char *q = p;
191  while (valid && *p != 0) {
192  if (*p != '=') {
193  valid = false;
194  break;
195  }
196  ++p;
197  }
198  valid = valid && (p - e) % 4 == 0 && p - q < 4;
199  if (pf_invalid) {
200  *pf_invalid = !valid;
201  }
202 
203  return ret;
204 }
205 
206 std::string DecodeBase64(const std::string &str, bool *pf_invalid) {
207  if (!ValidAsCString(str)) {
208  if (pf_invalid) {
209  *pf_invalid = true;
210  }
211  return {};
212  }
213  std::vector<uint8_t> vchRet = DecodeBase64(str.c_str(), pf_invalid);
214  return std::string((const char *)vchRet.data(), vchRet.size());
215 }
216 
217 std::string EncodeBase32(Span<const uint8_t> input, bool pad) {
218  static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
219 
220  std::string str;
221  str.reserve(((input.size() + 4) / 5) * 8);
222  ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(),
223  input.end());
224  if (pad) {
225  while (str.size() % 8) {
226  str += '=';
227  }
228  }
229  return str;
230 }
231 
232 std::string EncodeBase32(const std::string &str, bool pad) {
233  return EncodeBase32(MakeUCharSpan(str), pad);
234 }
235 
236 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid) {
237  static const int 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,
252  -1, -1, -1, -1};
253 
254  const char *e = p;
255  std::vector<uint8_t> val;
256  val.reserve(strlen(p));
257  while (*p != 0) {
258  int x = decode32_table[(uint8_t)*p];
259  if (x == -1) {
260  break;
261  }
262  val.push_back(x);
263  ++p;
264  }
265 
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());
270 
271  const char *q = p;
272  while (valid && *p != 0) {
273  if (*p != '=') {
274  valid = false;
275  break;
276  }
277  ++p;
278  }
279  valid = valid && (p - e) % 8 == 0 && p - q < 8;
280  if (pf_invalid) {
281  *pf_invalid = !valid;
282  }
283 
284  return ret;
285 }
286 
287 std::string DecodeBase32(const std::string &str, bool *pf_invalid) {
288  if (!ValidAsCString(str)) {
289  if (pf_invalid) {
290  *pf_invalid = true;
291  }
292  return {};
293  }
294  std::vector<uint8_t> vchRet = DecodeBase32(str.c_str(), pf_invalid);
295  return std::string((const char *)vchRet.data(), vchRet.size());
296 }
297 
298 NODISCARD static bool ParsePrechecks(const std::string &str) {
299  // No empty string allowed
300  if (str.empty()) {
301  return false;
302  }
303  // No padding allowed
304  if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size() - 1]))) {
305  return false;
306  }
307  // No embedded NUL characters allowed
308  if (!ValidAsCString(str)) {
309  return false;
310  }
311  return true;
312 }
313 
314 bool ParseInt32(const std::string &str, int32_t *out) {
315  if (!ParsePrechecks(str)) {
316  return false;
317  }
318  char *endp = nullptr;
319  // strtol will not set errno if valid
320  errno = 0;
321  long int n = strtol(str.c_str(), &endp, 10);
322  if (out) {
323  *out = (int32_t)n;
324  }
325  // Note that strtol returns a *long int*, so even if strtol doesn't report
326  // an over/underflow we still have to check that the returned value is
327  // within the range of an *int32_t*. On 64-bit platforms the size of these
328  // types may be different.
329  return endp && *endp == 0 && !errno &&
330  n >= std::numeric_limits<int32_t>::min() &&
331  n <= std::numeric_limits<int32_t>::max();
332 }
333 
334 bool ParseInt64(const std::string &str, int64_t *out) {
335  if (!ParsePrechecks(str)) {
336  return false;
337  }
338  char *endp = nullptr;
339  // strtoll will not set errno if valid
340  errno = 0;
341  long long int n = strtoll(str.c_str(), &endp, 10);
342  if (out) {
343  *out = (int64_t)n;
344  }
345  // Note that strtoll returns a *long long int*, so even if strtol doesn't
346  // report an over/underflow we still have to check that the returned value
347  // is within the range of an *int64_t*.
348  return endp && *endp == 0 && !errno &&
349  n >= std::numeric_limits<int64_t>::min() &&
350  n <= std::numeric_limits<int64_t>::max();
351 }
352 
353 bool ParseUInt8(const std::string &str, uint8_t *out) {
354  uint32_t u32;
355  if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
356  return false;
357  }
358  if (out != nullptr) {
359  *out = static_cast<uint8_t>(u32);
360  }
361  return true;
362 }
363 
364 bool ParseUInt32(const std::string &str, uint32_t *out) {
365  if (!ParsePrechecks(str)) {
366  return false;
367  }
368  // Reject negative values, unfortunately strtoul accepts these by default if
369  // they fit in the range
370  if (str.size() >= 1 && str[0] == '-') {
371  return false;
372  }
373  char *endp = nullptr;
374  // strtoul will not set errno if valid
375  errno = 0;
376  unsigned long int n = strtoul(str.c_str(), &endp, 10);
377  if (out) {
378  *out = (uint32_t)n;
379  }
380  // Note that strtoul returns a *unsigned long int*, so even if it doesn't
381  // report an over/underflow we still have to check that the returned value
382  // is within the range of an *uint32_t*. On 64-bit platforms the size of
383  // these types may be different.
384  return endp && *endp == 0 && !errno &&
385  n <= std::numeric_limits<uint32_t>::max();
386 }
387 
388 bool ParseUInt64(const std::string &str, uint64_t *out) {
389  if (!ParsePrechecks(str)) {
390  return false;
391  }
392  // Reject negative values, unfortunately strtoull accepts these by default
393  // if they fit in the range
394  if (str.size() >= 1 && str[0] == '-') {
395  return false;
396  }
397  char *endp = nullptr;
398  // strtoull will not set errno if valid
399  errno = 0;
400  unsigned long long int n = strtoull(str.c_str(), &endp, 10);
401  if (out) {
402  *out = (uint64_t)n;
403  }
404  // Note that strtoull returns a *unsigned long long int*, so even if it
405  // doesn't report an over/underflow we still have to check that the returned
406  // value is within the range of an *uint64_t*.
407  return endp && *endp == 0 && !errno &&
408  n <= std::numeric_limits<uint64_t>::max();
409 }
410 
411 bool ParseDouble(const std::string &str, double *out) {
412  if (!ParsePrechecks(str)) {
413  return false;
414  }
415  // No hexadecimal floats allowed
416  if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') {
417  return false;
418  }
419  std::istringstream text(str);
420  text.imbue(std::locale::classic());
421  double result;
422  text >> result;
423  if (out) {
424  *out = result;
425  }
426  return text.eof() && !text.fail();
427 }
428 
429 std::string FormatParagraph(const std::string &in, size_t width,
430  size_t indent) {
431  std::stringstream out;
432  size_t ptr = 0;
433  size_t indented = 0;
434  while (ptr < in.size()) {
435  size_t lineend = in.find_first_of('\n', ptr);
436  if (lineend == std::string::npos) {
437  lineend = in.size();
438  }
439  const size_t linelen = lineend - ptr;
440  const size_t rem_width = width - indented;
441  if (linelen <= rem_width) {
442  out << in.substr(ptr, linelen + 1);
443  ptr = lineend + 1;
444  indented = 0;
445  } else {
446  size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
447  if (finalspace == std::string::npos || finalspace < ptr) {
448  // No place to break; just include the entire word and move on
449  finalspace = in.find_first_of("\n ", ptr);
450  if (finalspace == std::string::npos) {
451  // End of the string, just add it and break
452  out << in.substr(ptr);
453  break;
454  }
455  }
456  out << in.substr(ptr, finalspace - ptr) << "\n";
457  if (in[finalspace] == '\n') {
458  indented = 0;
459  } else if (indent) {
460  out << std::string(indent, ' ');
461  indented = indent;
462  }
463  ptr = finalspace + 1;
464  }
465  }
466  return out.str();
467 }
468 
469 int64_t atoi64(const std::string &str) {
470 #ifdef _MSC_VER
471  return _atoi64(str.c_str());
472 #else
473  return strtoll(str.c_str(), nullptr, 10);
474 #endif
475 }
476 
477 int atoi(const std::string &str) {
478  return atoi(str.c_str());
479 }
480 
490 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
491 
493 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa,
494  int &mantissa_tzeros) {
495  if (ch == '0') {
496  ++mantissa_tzeros;
497  } else {
498  for (int i = 0; i <= mantissa_tzeros; ++i) {
499  // overflow
500  if (mantissa > (UPPER_BOUND / 10LL)) {
501  return false;
502  }
503  mantissa *= 10;
504  }
505  mantissa += ch - '0';
506  mantissa_tzeros = 0;
507  }
508  return true;
509 }
510 
511 bool ParseFixedPoint(const std::string &val, int decimals,
512  int64_t *amount_out) {
513  int64_t mantissa = 0;
514  int64_t exponent = 0;
515  int mantissa_tzeros = 0;
516  bool mantissa_sign = false;
517  bool exponent_sign = false;
518  int ptr = 0;
519  int end = val.size();
520  int point_ofs = 0;
521 
522  if (ptr < end && val[ptr] == '-') {
523  mantissa_sign = true;
524  ++ptr;
525  }
526  if (ptr < end) {
527  if (val[ptr] == '0') {
528  // pass single 0
529  ++ptr;
530  } else if (val[ptr] >= '1' && val[ptr] <= '9') {
531  while (ptr < end && IsDigit(val[ptr])) {
532  if (!ProcessMantissaDigit(val[ptr], mantissa,
533  mantissa_tzeros)) {
534  // overflow
535  return false;
536  }
537  ++ptr;
538  }
539  } else {
540  // missing expected digit
541  return false;
542  }
543  } else {
544  // empty string or loose '-'
545  return false;
546  }
547  if (ptr < end && val[ptr] == '.') {
548  ++ptr;
549  if (ptr < end && IsDigit(val[ptr])) {
550  while (ptr < end && IsDigit(val[ptr])) {
551  if (!ProcessMantissaDigit(val[ptr], mantissa,
552  mantissa_tzeros)) {
553  // overflow
554  return false;
555  }
556  ++ptr;
557  ++point_ofs;
558  }
559  } else {
560  // missing expected digit
561  return false;
562  }
563  }
564  if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E')) {
565  ++ptr;
566  if (ptr < end && val[ptr] == '+') {
567  ++ptr;
568  } else if (ptr < end && val[ptr] == '-') {
569  exponent_sign = true;
570  ++ptr;
571  }
572  if (ptr < end && IsDigit(val[ptr])) {
573  while (ptr < end && IsDigit(val[ptr])) {
574  if (exponent > (UPPER_BOUND / 10LL)) {
575  // overflow
576  return false;
577  }
578  exponent = exponent * 10 + val[ptr] - '0';
579  ++ptr;
580  }
581  } else {
582  // missing expected digit
583  return false;
584  }
585  }
586  if (ptr != end) {
587  // trailing garbage
588  return false;
589  }
590  // finalize exponent
591  if (exponent_sign) {
592  exponent = -exponent;
593  }
594  exponent = exponent - point_ofs + mantissa_tzeros;
595 
596  // finalize mantissa
597  if (mantissa_sign) {
598  mantissa = -mantissa;
599  }
600 
601  // convert to one 64-bit fixed-point value
602  exponent += decimals;
603  if (exponent < 0) {
604  // cannot represent values smaller than 10^-decimals
605  return false;
606  }
607  if (exponent >= 18) {
608  // cannot represent values larger than or equal to 10^(18-decimals)
609  return false;
610  }
611 
612  for (int i = 0; i < exponent; ++i) {
613  if (mantissa > (UPPER_BOUND / 10LL) ||
614  mantissa < -(UPPER_BOUND / 10LL)) {
615  // overflow
616  return false;
617  }
618  mantissa *= 10;
619  }
620  if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND) {
621  // overflow
622  return false;
623  }
624 
625  if (amount_out) {
626  *amount_out = mantissa;
627  }
628 
629  return true;
630 }
631 
632 std::string ToLower(const std::string &str) {
633  std::string r;
634  for (auto ch : str) {
635  r += ToLower(ch);
636  }
637  return r;
638 }
639 
640 std::string ToUpper(const std::string &str) {
641  std::string r;
642  for (auto ch : str) {
643  r += ToUpper(ch);
644  }
645  return r;
646 }
647 
648 std::string Capitalize(std::string str) {
649  if (str.empty()) {
650  return str;
651  }
652  str[0] = ToUpper(str.front());
653  return str;
654 }
655 
656 std::string HexStr(const Span<const uint8_t> s) {
657  std::string rv;
658  static constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
659  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
660  rv.reserve(s.size() * 2);
661  for (uint8_t v : s) {
662  rv.push_back(hexmap[v >> 4]);
663  rv.push_back(hexmap[v & 15]);
664  }
665  return rv;
666 }
atoi64
int64_t atoi64(const std::string &str)
Definition: strencodings.cpp:469
ParseInt64
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:334
ParseHex
std::vector< uint8_t > ParseHex(const char *psz)
Definition: strencodings.cpp:87
ParseFixedPoint
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
Definition: strencodings.cpp:511
ParseDouble
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
Definition: strencodings.cpp:411
IsHexNumber
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
Definition: strencodings.cpp:73
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) uint8_t member types only.
Definition: span.h:280
string.h
atoi
int atoi(const std::string &str)
Definition: strencodings.cpp:477
IsHex
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.
Definition: strencodings.cpp:64
DecodeBase64
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid)
Definition: strencodings.cpp:155
NODISCARD
#define NODISCARD
Definition: attributes.h:18
SplitHostPort
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: strencodings.cpp:113
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:102
tinyformat.h
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:179
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:83
strencodings.h
p_util_hexdigit
const signed char p_util_hexdigit[256]
Definition: strencodings.cpp:41
UPPER_BOUND
static const int64_t UPPER_BOUND
Upper bound for mantissa.
Definition: strencodings.cpp:490
HexDigit
signed char HexDigit(char c)
Definition: strencodings.cpp:60
DecodeBase32
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
Definition: strencodings.cpp:236
ParsePrechecks
static NODISCARD bool ParsePrechecks(const std::string &str)
Definition: strencodings.cpp:298
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:169
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:648
ToUpper
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
Definition: strencodings.cpp:640
CHARS_ALPHA_NUM
static const std::string CHARS_ALPHA_NUM
Definition: strencodings.cpp:17
ValidAsCString
NODISCARD bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:62
IsDigit
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:86
EncodeBase32
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
Definition: strencodings.cpp:217
ParseUInt64
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:388
SAFE_CHARS
static const std::string SAFE_CHARS[]
Definition: strencodings.cpp:20
ParseInt32
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:314
SanitizeString
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
Definition: strencodings.cpp:31
EncodeBase64
std::string EncodeBase64(Span< const uint8_t > input)
Definition: strencodings.cpp:137
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:656
ParseUInt8
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
Definition: strencodings.cpp:353
ProcessMantissaDigit
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
Definition: strencodings.cpp:493
Span::end
constexpr C * end() const noexcept
Definition: span.h:170
ParseUInt32
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:364
FormatParagraph
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.
Definition: strencodings.cpp:429
ToLower
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
Definition: strencodings.cpp:632