1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
8 #include <tinyformat.h>
9 
10 #include <algorithm>
11 #include <cstdlib>
12 #include <cstring>
13 #include <errno.h>
14 #include <limits>
15 
16 static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
17 
18 static const std::string SAFE_CHARS[] =
19 {
20     CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
21     CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
22     CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
23     CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
24 };
25 
SanitizeString(const std::string & str,int rule)26 std::string SanitizeString(const std::string& str, int rule)
27 {
28     std::string strResult;
29     for (std::string::size_type i = 0; i < str.size(); i++)
30     {
31         if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
32             strResult.push_back(str[i]);
33     }
34     return strResult;
35 }
36 
37 const signed char p_util_hexdigit[256] =
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, };
54 
HexDigit(char c)55 signed char HexDigit(char c)
56 {
57     return p_util_hexdigit[(unsigned char)c];
58 }
59 
IsHex(const std::string & str)60 bool IsHex(const std::string& str)
61 {
62     for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
63     {
64         if (HexDigit(*it) < 0)
65             return false;
66     }
67     return (str.size() > 0) && (str.size()%2 == 0);
68 }
69 
IsHexNumber(const std::string & str)70 bool IsHexNumber(const std::string& str)
71 {
72     size_t starting_location = 0;
73     if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') {
74         starting_location = 2;
75     }
76     for (const char c : str.substr(starting_location)) {
77         if (HexDigit(c) < 0) return false;
78     }
79     // Return false for empty string or "0x".
80     return (str.size() > starting_location);
81 }
82 
ParseHex(const char * psz)83 std::vector<unsigned char> ParseHex(const char* psz)
84 {
85     // convert hex dump to vector
86     std::vector<unsigned char> vch;
87     while (true)
88     {
89         while (IsSpace(*psz))
90             psz++;
91         signed char c = HexDigit(*psz++);
92         if (c == (signed char)-1)
93             break;
94         unsigned char n = (c << 4);
95         c = HexDigit(*psz++);
96         if (c == (signed char)-1)
97             break;
98         n |= c;
99         vch.push_back(n);
100     }
101     return vch;
102 }
103 
ParseHex(const std::string & str)104 std::vector<unsigned char> ParseHex(const std::string& str)
105 {
106     return ParseHex(str.c_str());
107 }
108 
SplitHostPort(std::string in,int & portOut,std::string & hostOut)109 void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
110     size_t colon = in.find_last_of(':');
111     // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
112     bool fHaveColon = colon != in.npos;
113     bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
114     bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
115     if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
116         int32_t n;
117         if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
118             in = in.substr(0, colon);
119             portOut = n;
120         }
121     }
122     if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
123         hostOut = in.substr(1, in.size()-2);
124     else
125         hostOut = in;
126 }
127 
EncodeBase64(const unsigned char * pch,size_t len)128 std::string EncodeBase64(const unsigned char* pch, size_t len)
129 {
130     static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
131 
132     std::string str;
133     str.reserve(((len + 2) / 3) * 4);
134     ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, pch, pch + len);
135     while (str.size() % 4) str += '=';
136     return str;
137 }
138 
EncodeBase64(const std::string & str)139 std::string EncodeBase64(const std::string& str)
140 {
141     return EncodeBase64((const unsigned char*)str.c_str(), str.size());
142 }
143 
DecodeBase64(const char * p,bool * pf_invalid)144 std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
145 {
146     static const int decode64_table[256] =
147     {
148         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150         -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
151         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
152         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
153         29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
154         49, 50, 51, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
156         -1, -1, -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, -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
161     };
162 
163     const char* e = p;
164     std::vector<uint8_t> val;
165     val.reserve(strlen(p));
166     while (*p != 0) {
167         int x = decode64_table[(unsigned char)*p];
168         if (x == -1) break;
169         val.push_back(x);
170         ++p;
171     }
172 
173     std::vector<unsigned char> ret;
174     ret.reserve((val.size() * 3) / 4);
175     bool valid = ConvertBits<6, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
176 
177     const char* q = p;
178     while (valid && *p != 0) {
179         if (*p != '=') {
180             valid = false;
181             break;
182         }
183         ++p;
184     }
185     valid = valid && (p - e) % 4 == 0 && p - q < 4;
186     if (pf_invalid) *pf_invalid = !valid;
187 
188     return ret;
189 }
190 
DecodeBase64(const std::string & str,bool * pf_invalid)191 std::string DecodeBase64(const std::string& str, bool* pf_invalid)
192 {
193     std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
194     return std::string((const char*)vchRet.data(), vchRet.size());
195 }
196 
EncodeBase32(const unsigned char * pch,size_t len)197 std::string EncodeBase32(const unsigned char* pch, size_t len)
198 {
199     static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
200 
201     std::string str;
202     str.reserve(((len + 4) / 5) * 8);
203     ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, pch, pch + len);
204     while (str.size() % 8) str += '=';
205     return str;
206 }
207 
EncodeBase32(const std::string & str)208 std::string EncodeBase32(const std::string& str)
209 {
210     return EncodeBase32((const unsigned char*)str.c_str(), str.size());
211 }
212 
DecodeBase32(const char * p,bool * pf_invalid)213 std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
214 {
215     static const int decode32_table[256] =
216     {
217         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
218         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
220         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
221         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,  0,  1,  2,
222          3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
223         23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
224         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
225         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
226         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
227         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
228         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
229         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
230     };
231 
232     const char* e = p;
233     std::vector<uint8_t> val;
234     val.reserve(strlen(p));
235     while (*p != 0) {
236         int x = decode32_table[(unsigned char)*p];
237         if (x == -1) break;
238         val.push_back(x);
239         ++p;
240     }
241 
242     std::vector<unsigned char> ret;
243     ret.reserve((val.size() * 5) / 8);
244     bool valid = ConvertBits<5, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
245 
246     const char* q = p;
247     while (valid && *p != 0) {
248         if (*p != '=') {
249             valid = false;
250             break;
251         }
252         ++p;
253     }
254     valid = valid && (p - e) % 8 == 0 && p - q < 8;
255     if (pf_invalid) *pf_invalid = !valid;
256 
257     return ret;
258 }
259 
DecodeBase32(const std::string & str,bool * pf_invalid)260 std::string DecodeBase32(const std::string& str, bool* pf_invalid)
261 {
262     std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);
263     return std::string((const char*)vchRet.data(), vchRet.size());
264 }
265 
ParsePrechecks(const std::string & str)266 NODISCARD static bool ParsePrechecks(const std::string& str)
267 {
268     if (str.empty()) // No empty string allowed
269         return false;
270     if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
271         return false;
272     if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
273         return false;
274     return true;
275 }
276 
ParseInt32(const std::string & str,int32_t * out)277 bool ParseInt32(const std::string& str, int32_t *out)
278 {
279     if (!ParsePrechecks(str))
280         return false;
281     char *endp = nullptr;
282     errno = 0; // strtol will not set errno if valid
283     long int n = strtol(str.c_str(), &endp, 10);
284     if(out) *out = (int32_t)n;
285     // Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
286     // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
287     // platforms the size of these types may be different.
288     return endp && *endp == 0 && !errno &&
289         n >= std::numeric_limits<int32_t>::min() &&
290         n <= std::numeric_limits<int32_t>::max();
291 }
292 
ParseInt64(const std::string & str,int64_t * out)293 bool ParseInt64(const std::string& str, int64_t *out)
294 {
295     if (!ParsePrechecks(str))
296         return false;
297     char *endp = nullptr;
298     errno = 0; // strtoll will not set errno if valid
299     long long int n = strtoll(str.c_str(), &endp, 10);
300     if(out) *out = (int64_t)n;
301     // Note that strtoll returns a *long long int*, so even if strtol doesn't report an over/underflow
302     // we still have to check that the returned value is within the range of an *int64_t*.
303     return endp && *endp == 0 && !errno &&
304         n >= std::numeric_limits<int64_t>::min() &&
305         n <= std::numeric_limits<int64_t>::max();
306 }
307 
ParseUInt32(const std::string & str,uint32_t * out)308 bool ParseUInt32(const std::string& str, uint32_t *out)
309 {
310     if (!ParsePrechecks(str))
311         return false;
312     if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
313         return false;
314     char *endp = nullptr;
315     errno = 0; // strtoul will not set errno if valid
316     unsigned long int n = strtoul(str.c_str(), &endp, 10);
317     if(out) *out = (uint32_t)n;
318     // Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow
319     // we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
320     // platforms the size of these types may be different.
321     return endp && *endp == 0 && !errno &&
322         n <= std::numeric_limits<uint32_t>::max();
323 }
324 
ParseUInt64(const std::string & str,uint64_t * out)325 bool ParseUInt64(const std::string& str, uint64_t *out)
326 {
327     if (!ParsePrechecks(str))
328         return false;
329     if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoull accepts these by default if they fit in the range
330         return false;
331     char *endp = nullptr;
332     errno = 0; // strtoull will not set errno if valid
333     unsigned long long int n = strtoull(str.c_str(), &endp, 10);
334     if(out) *out = (uint64_t)n;
335     // Note that strtoull returns a *unsigned long long int*, so even if it doesn't report an over/underflow
336     // we still have to check that the returned value is within the range of an *uint64_t*.
337     return endp && *endp == 0 && !errno &&
338         n <= std::numeric_limits<uint64_t>::max();
339 }
340 
341 
ParseDouble(const std::string & str,double * out)342 bool ParseDouble(const std::string& str, double *out)
343 {
344     if (!ParsePrechecks(str))
345         return false;
346     if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
347         return false;
348     std::istringstream text(str);
349     text.imbue(std::locale::classic());
350     double result;
351     text >> result;
352     if(out) *out = result;
353     return text.eof() && !text.fail();
354 }
355 
FormatParagraph(const std::string & in,size_t width,size_t indent)356 std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
357 {
358     std::stringstream out;
359     size_t ptr = 0;
360     size_t indented = 0;
361     while (ptr < in.size())
362     {
363         size_t lineend = in.find_first_of('\n', ptr);
364         if (lineend == std::string::npos) {
365             lineend = in.size();
366         }
367         const size_t linelen = lineend - ptr;
368         const size_t rem_width = width - indented;
369         if (linelen <= rem_width) {
370             out << in.substr(ptr, linelen + 1);
371             ptr = lineend + 1;
372             indented = 0;
373         } else {
374             size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
375             if (finalspace == std::string::npos || finalspace < ptr) {
376                 // No place to break; just include the entire word and move on
377                 finalspace = in.find_first_of("\n ", ptr);
378                 if (finalspace == std::string::npos) {
379                     // End of the string, just add it and break
380                     out << in.substr(ptr);
381                     break;
382                 }
383             }
384             out << in.substr(ptr, finalspace - ptr) << "\n";
385             if (in[finalspace] == '\n') {
386                 indented = 0;
387             } else if (indent) {
388                 out << std::string(indent, ' ');
389                 indented = indent;
390             }
391             ptr = finalspace + 1;
392         }
393     }
394     return out.str();
395 }
396 
i64tostr(int64_t n)397 std::string i64tostr(int64_t n)
398 {
399     return strprintf("%d", n);
400 }
401 
itostr(int n)402 std::string itostr(int n)
403 {
404     return strprintf("%d", n);
405 }
406 
atoi64(const char * psz)407 int64_t atoi64(const char* psz)
408 {
409 #ifdef _MSC_VER
410     return _atoi64(psz);
411 #else
412     return strtoll(psz, nullptr, 10);
413 #endif
414 }
415 
atoi64(const std::string & str)416 int64_t atoi64(const std::string& str)
417 {
418 #ifdef _MSC_VER
419     return _atoi64(str.c_str());
420 #else
421     return strtoll(str.c_str(), nullptr, 10);
422 #endif
423 }
424 
atoi(const std::string & str)425 int atoi(const std::string& str)
426 {
427     return atoi(str.c_str());
428 }
429 
430 /** Upper bound for mantissa.
431  * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
432  * Larger integers cannot consist of arbitrary combinations of 0-9:
433  *
434  *   999999999999999999  1^18-1
435  *  9223372036854775807  (1<<63)-1  (max int64_t)
436  *  9999999999999999999  1^19-1     (would overflow)
437  */
438 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
439 
440 /** Helper function for ParseFixedPoint */
ProcessMantissaDigit(char ch,int64_t & mantissa,int & mantissa_tzeros)441 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
442 {
443     if(ch == '0')
444         ++mantissa_tzeros;
445     else {
446         for (int i=0; i<=mantissa_tzeros; ++i) {
447             if (mantissa > (UPPER_BOUND / 10LL))
448                 return false; /* overflow */
449             mantissa *= 10;
450         }
451         mantissa += ch - '0';
452         mantissa_tzeros = 0;
453     }
454     return true;
455 }
456 
ParseFixedPoint(const std::string & val,int decimals,int64_t * amount_out)457 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
458 {
459     int64_t mantissa = 0;
460     int64_t exponent = 0;
461     int mantissa_tzeros = 0;
462     bool mantissa_sign = false;
463     bool exponent_sign = false;
464     int ptr = 0;
465     int end = val.size();
466     int point_ofs = 0;
467 
468     if (ptr < end && val[ptr] == '-') {
469         mantissa_sign = true;
470         ++ptr;
471     }
472     if (ptr < end)
473     {
474         if (val[ptr] == '0') {
475             /* pass single 0 */
476             ++ptr;
477         } else if (val[ptr] >= '1' && val[ptr] <= '9') {
478             while (ptr < end && IsDigit(val[ptr])) {
479                 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
480                     return false; /* overflow */
481                 ++ptr;
482             }
483         } else return false; /* missing expected digit */
484     } else return false; /* empty string or loose '-' */
485     if (ptr < end && val[ptr] == '.')
486     {
487         ++ptr;
488         if (ptr < end && IsDigit(val[ptr]))
489         {
490             while (ptr < end && IsDigit(val[ptr])) {
491                 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
492                     return false; /* overflow */
493                 ++ptr;
494                 ++point_ofs;
495             }
496         } else return false; /* missing expected digit */
497     }
498     if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
499     {
500         ++ptr;
501         if (ptr < end && val[ptr] == '+')
502             ++ptr;
503         else if (ptr < end && val[ptr] == '-') {
504             exponent_sign = true;
505             ++ptr;
506         }
507         if (ptr < end && IsDigit(val[ptr])) {
508             while (ptr < end && IsDigit(val[ptr])) {
509                 if (exponent > (UPPER_BOUND / 10LL))
510                     return false; /* overflow */
511                 exponent = exponent * 10 + val[ptr] - '0';
512                 ++ptr;
513             }
514         } else return false; /* missing expected digit */
515     }
516     if (ptr != end)
517         return false; /* trailing garbage */
518 
519     /* finalize exponent */
520     if (exponent_sign)
521         exponent = -exponent;
522     exponent = exponent - point_ofs + mantissa_tzeros;
523 
524     /* finalize mantissa */
525     if (mantissa_sign)
526         mantissa = -mantissa;
527 
528     /* convert to one 64-bit fixed-point value */
529     exponent += decimals;
530     if (exponent < 0)
531         return false; /* cannot represent values smaller than 10^-decimals */
532     if (exponent >= 18)
533         return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
534 
535     for (int i=0; i < exponent; ++i) {
536         if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
537             return false; /* overflow */
538         mantissa *= 10;
539     }
540     if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
541         return false; /* overflow */
542 
543     if (amount_out)
544         *amount_out = mantissa;
545 
546     return true;
547 }
548 
Downcase(std::string & str)549 void Downcase(std::string& str)
550 {
551     std::transform(str.begin(), str.end(), str.begin(), [](char c){return ToLower(c);});
552 }
553 
Capitalize(std::string str)554 std::string Capitalize(std::string str)
555 {
556     if (str.empty()) return str;
557     str[0] = ToUpper(str.front());
558     return str;
559 }
560