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