LCOV - code coverage report
Current view: top level - src/util - strencodings.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 82.4 % 227 187
Test Date: 2025-06-01 05:27:21 Functions: 94.7 % 19 18
Branches: 78.3 % 286 224

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <crypto/hex_base.h>
       9                 :             : #include <span.h>
      10                 :             : 
      11                 :             : #include <array>
      12                 :             : #include <cassert>
      13                 :             : #include <cstring>
      14                 :             : #include <limits>
      15                 :             : #include <optional>
      16                 :             : #include <ostream>
      17                 :             : #include <string>
      18                 :             : #include <vector>
      19                 :             : 
      20                 :             : static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      21                 :             : 
      22                 :             : static const std::string SAFE_CHARS[] =
      23                 :             : {
      24                 :             :     CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
      25                 :             :     CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
      26                 :             :     CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
      27                 :             :     CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
      28                 :             : };
      29                 :             : 
      30                 :       38179 : std::string SanitizeString(std::string_view str, int rule)
      31                 :             : {
      32                 :       38179 :     std::string result;
      33         [ +  + ]:      345041 :     for (char c : str) {
      34         [ +  + ]:      306862 :         if (SAFE_CHARS[rule].find(c) != std::string::npos) {
      35         [ +  - ]:      131803 :             result.push_back(c);
      36                 :             :         }
      37                 :             :     }
      38                 :       38179 :     return result;
      39                 :           0 : }
      40                 :             : 
      41                 :      221557 : bool IsHex(std::string_view str)
      42                 :             : {
      43         [ +  + ]:   287409224 :     for (char c : str) {
      44         [ +  + ]:   287272198 :         if (HexDigit(c) < 0) return false;
      45                 :             :     }
      46   [ +  +  +  + ]:      137026 :     return (str.size() > 0) && (str.size()%2 == 0);
      47                 :             : }
      48                 :             : 
      49                 :             : template <typename Byte>
      50                 :      500769 : std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
      51                 :             : {
      52         [ +  - ]:      500769 :     std::vector<Byte> vch;
      53         [ +  - ]:      500769 :     vch.reserve(str.size() / 2); // two hex characters form a single byte
      54                 :             : 
      55                 :      500769 :     auto it = str.begin();
      56                 :      500769 :     while (it != str.end()) {
      57         [ +  + ]:   140208045 :         if (IsSpace(*it)) {
      58                 :        4289 :             ++it;
      59                 :        4289 :             continue;
      60                 :             :         }
      61         [ +  - ]:   140203756 :         auto c1 = HexDigit(*(it++));
      62         [ +  + ]:   140203756 :         if (it == str.end()) return std::nullopt;
      63         [ +  - ]:   140203734 :         auto c2 = HexDigit(*(it++));
      64         [ +  + ]:   140203734 :         if (c1 < 0 || c2 < 0) return std::nullopt;
      65   [ +  -  +  + ]:   280912340 :         vch.push_back(Byte(c1 << 4) | Byte(c2));
      66                 :             :     }
      67                 :      500654 :     return vch;
      68                 :      500769 : }
      69                 :             : template std::optional<std::vector<std::byte>> TryParseHex(std::string_view);
      70                 :             : template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view);
      71                 :             : 
      72                 :      403000 : bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
      73                 :             : {
      74                 :      403000 :     bool valid = false;
      75         [ +  + ]:      403000 :     size_t colon = in.find_last_of(':');
      76                 :             :     // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
      77                 :      403000 :     bool fHaveColon = colon != in.npos;
      78   [ +  +  +  +  :      403000 :     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
                   +  + ]
      79   [ +  +  +  + ]:      403000 :     bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(':', colon - 1) != in.npos)};
      80   [ +  +  +  +  :      403000 :     if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
                   +  + ]
      81         [ +  + ]:       88867 :         if (const auto n{ToIntegral<uint16_t>(in.substr(colon + 1))}) {
      82                 :       39056 :             in = in.substr(0, colon);
      83                 :       39056 :             portOut = *n;
      84                 :       39056 :             valid = (portOut != 0);
      85                 :             :         }
      86                 :             :     } else {
      87                 :             :         valid = true;
      88                 :             :     }
      89   [ +  +  +  +  :      403000 :     if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') {
                   +  + ]
      90                 :        1974 :         hostOut = in.substr(1, in.size() - 2);
      91                 :             :     } else {
      92                 :      401026 :         hostOut = in;
      93                 :             :     }
      94                 :             : 
      95                 :      403000 :     return valid;
      96                 :             : }
      97                 :             : 
      98                 :       47794 : std::string EncodeBase64(std::span<const unsigned char> input)
      99                 :             : {
     100                 :       47794 :     static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     101                 :             : 
     102         [ +  - ]:       47794 :     std::string str;
     103         [ +  - ]:       47794 :     str.reserve(((input.size() + 2) / 3) * 4);
     104         [ +  - ]:    48510383 :     ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
     105   [ +  -  +  + ]:       95393 :     while (str.size() % 4) str += '=';
     106                 :       47794 :     return str;
     107                 :           0 : }
     108                 :             : 
     109                 :       16575 : std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str)
     110                 :             : {
     111                 :       16575 :     static const int8_t decode64_table[256]{
     112                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     113                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     114                 :             :         -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
     115                 :             :         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
     116                 :             :         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
     117                 :             :         29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
     118                 :             :         49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     119                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     120                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     121                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     122                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     123                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     124                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
     125                 :             :     };
     126                 :             : 
     127         [ +  + ]:       16575 :     if (str.size() % 4 != 0) return {};
     128                 :             :     /* One or two = characters at the end are permitted. */
     129   [ +  +  +  + ]:       16449 :     if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
     130   [ +  +  +  + ]:       16449 :     if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
     131                 :             : 
     132                 :       16449 :     std::vector<unsigned char> ret;
     133         [ +  - ]:       16449 :     ret.reserve((str.size() * 3) / 4);
     134         [ +  - ]:       16449 :     bool valid = ConvertBits<6, 8, false>(
     135                 :    24851818 :         [&](unsigned char c) { ret.push_back(c); },
     136                 :             :         str.begin(), str.end(),
     137         [ +  + ]:    33140997 :         [](char c) { return decode64_table[uint8_t(c)]; }
     138                 :             :     );
     139         [ +  + ]:       16449 :     if (!valid) return {};
     140                 :             : 
     141                 :       16383 :     return ret;
     142                 :       16449 : }
     143                 :             : 
     144                 :      227310 : std::string EncodeBase32(std::span<const unsigned char> input, bool pad)
     145                 :             : {
     146                 :      227310 :     static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
     147                 :             : 
     148         [ +  - ]:      227310 :     std::string str;
     149         [ +  - ]:      227310 :     str.reserve(((input.size() + 4) / 5) * 8);
     150         [ +  - ]:    15798397 :     ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
     151         [ +  + ]:      227310 :     if (pad) {
     152                 :      174671 :         while (str.size() % 8) {
     153   [ +  -  +  + ]:      174671 :             str += '=';
     154                 :             :         }
     155                 :             :     }
     156                 :      227310 :     return str;
     157                 :           0 : }
     158                 :             : 
     159                 :        6962 : std::string EncodeBase32(std::string_view str, bool pad)
     160                 :             : {
     161                 :        6962 :     return EncodeBase32(MakeUCharSpan(str), pad);
     162                 :             : }
     163                 :             : 
     164                 :       59548 : std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
     165                 :             : {
     166                 :       59548 :     static const int8_t decode32_table[256]{
     167                 :             :         -1, -1, -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, -1, -1,
     169                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
     170                 :             :         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
     171                 :             :         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,  0,  1,  2,
     172                 :             :          3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
     173                 :             :         23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     174                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     175                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     176                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     177                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     178                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     179                 :             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
     180                 :             :     };
     181                 :             : 
     182         [ +  + ]:       59548 :     if (str.size() % 8 != 0) return {};
     183                 :             :     /* 1, 3, 4, or 6 padding '=' suffix characters are permitted. */
     184   [ +  -  +  + ]:       33327 :     if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
     185   [ +  -  +  + ]:       33327 :     if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
     186   [ +  -  +  + ]:       33327 :     if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
     187   [ +  -  +  + ]:       33327 :     if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
     188                 :             : 
     189                 :       33327 :     std::vector<unsigned char> ret;
     190         [ +  - ]:       33327 :     ret.reserve((str.size() * 5) / 8);
     191         [ +  - ]:       33327 :     bool valid = ConvertBits<5, 8, false>(
     192                 :      932454 :         [&](unsigned char c) { ret.push_back(c); },
     193                 :             :         str.begin(), str.end(),
     194         [ +  + ]:     1514308 :         [](char c) { return decode32_table[uint8_t(c)]; }
     195                 :             :     );
     196                 :             : 
     197         [ +  + ]:       33327 :     if (!valid) return {};
     198                 :             : 
     199                 :       25973 :     return ret;
     200                 :       33327 : }
     201                 :             : 
     202                 :        3260 : std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
     203                 :             : {
     204         [ -  + ]:        3260 :     assert(width >= indent);
     205                 :        3260 :     std::stringstream out;
     206                 :        3260 :     size_t ptr = 0;
     207                 :        3260 :     size_t indented = 0;
     208         [ +  + ]:        9005 :     while (ptr < in.size())
     209                 :             :     {
     210         [ +  + ]:        3153 :         size_t lineend = in.find_first_of('\n', ptr);
     211         [ +  + ]:        3153 :         if (lineend == std::string::npos) {
     212                 :        2614 :             lineend = in.size();
     213                 :             :         }
     214                 :        3153 :         const size_t linelen = lineend - ptr;
     215                 :        3153 :         const size_t rem_width = width - indented;
     216         [ +  + ]:        3153 :         if (linelen <= rem_width) {
     217         [ +  - ]:        2113 :             out << in.substr(ptr, linelen + 1);
     218                 :        2113 :             ptr = lineend + 1;
     219                 :        2113 :             indented = 0;
     220                 :             :         } else {
     221                 :        1040 :             size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
     222         [ +  + ]:        1040 :             if (finalspace == std::string::npos || finalspace < ptr) {
     223                 :             :                 // No place to break; just include the entire word and move on
     224                 :         805 :                 finalspace = in.find_first_of("\n ", ptr);
     225         [ +  + ]:         805 :                 if (finalspace == std::string::npos) {
     226                 :             :                     // End of the string, just add it and break
     227         [ +  - ]:         668 :                     out << in.substr(ptr);
     228                 :             :                     break;
     229                 :             :                 }
     230                 :             :             }
     231   [ +  -  +  - ]:         744 :             out << in.substr(ptr, finalspace - ptr) << "\n";
     232         [ +  + ]:         372 :             if (in[finalspace] == '\n') {
     233                 :             :                 indented = 0;
     234         [ +  + ]:         312 :             } else if (indent) {
     235   [ +  -  +  - ]:         223 :                 out << std::string(indent, ' ');
     236                 :         223 :                 indented = indent;
     237                 :             :             }
     238                 :         372 :             ptr = finalspace + 1;
     239                 :             :         }
     240                 :             :     }
     241         [ +  - ]:        6520 :     return out.str();
     242                 :        3260 : }
     243                 :             : 
     244                 :             : /** Upper bound for mantissa.
     245                 :             :  * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
     246                 :             :  * Larger integers cannot consist of arbitrary combinations of 0-9:
     247                 :             :  *
     248                 :             :  *   999999999999999999  1^18-1
     249                 :             :  *  9223372036854775807  (1<<63)-1  (max int64_t)
     250                 :             :  *  9999999999999999999  1^19-1     (would overflow)
     251                 :             :  */
     252                 :             : static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
     253                 :             : 
     254                 :             : /** Helper function for ParseFixedPoint */
     255                 :     1263348 : static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
     256                 :             : {
     257         [ +  + ]:     1263348 :     if(ch == '0')
     258                 :     1261395 :         ++mantissa_tzeros;
     259                 :             :     else {
     260         [ +  + ]:        8504 :         for (int i=0; i<=mantissa_tzeros; ++i) {
     261         [ +  + ]:        6594 :             if (mantissa > (UPPER_BOUND / 10LL))
     262                 :             :                 return false; /* overflow */
     263                 :        6551 :             mantissa *= 10;
     264                 :             :         }
     265                 :        1910 :         mantissa += ch - '0';
     266                 :        1910 :         mantissa_tzeros = 0;
     267                 :             :     }
     268                 :             :     return true;
     269                 :             : }
     270                 :             : 
     271                 :        3423 : bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
     272                 :             : {
     273                 :        3423 :     int64_t mantissa = 0;
     274                 :        3423 :     int64_t exponent = 0;
     275                 :        3423 :     int mantissa_tzeros = 0;
     276                 :        3423 :     bool mantissa_sign = false;
     277                 :        3423 :     bool exponent_sign = false;
     278                 :        3423 :     int ptr = 0;
     279         [ +  + ]:        3423 :     int end = val.size();
     280                 :        3423 :     int point_ofs = 0;
     281                 :             : 
     282   [ +  +  +  + ]:        3423 :     if (ptr < end && val[ptr] == '-') {
     283                 :             :         mantissa_sign = true;
     284                 :             :         ++ptr;
     285                 :             :     }
     286         [ +  + ]:        3423 :     if (ptr < end)
     287                 :             :     {
     288         [ +  + ]:        3391 :         if (val[ptr] == '0') {
     289                 :             :             /* pass single 0 */
     290                 :         466 :             ++ptr;
     291   [ +  +  +  + ]:        2925 :         } else if (val[ptr] >= '1' && val[ptr] <= '9') {
     292   [ +  +  +  + ]:     1086052 :             while (ptr < end && IsDigit(val[ptr])) {
     293         [ +  + ]:     1085819 :                 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
     294                 :             :                     return false; /* overflow */
     295                 :     1085789 :                 ++ptr;
     296                 :             :             }
     297                 :             :         } else return false; /* missing expected digit */
     298                 :             :     } else return false; /* empty string or loose '-' */
     299   [ +  +  +  + ]:         699 :     if (ptr < end && val[ptr] == '.')
     300                 :             :     {
     301                 :         521 :         ++ptr;
     302   [ +  +  +  + ]:         521 :         if (ptr < end && IsDigit(val[ptr]))
     303                 :             :         {
     304   [ +  +  +  + ]:      178022 :             while (ptr < end && IsDigit(val[ptr])) {
     305         [ +  + ]:      177529 :                 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
     306                 :             :                     return false; /* overflow */
     307                 :      177516 :                 ++ptr;
     308                 :      177516 :                 ++point_ofs;
     309                 :             :             }
     310                 :             :         } else return false; /* missing expected digit */
     311                 :             :     }
     312   [ +  +  +  +  :         671 :     if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
                   +  + ]
     313                 :             :     {
     314                 :         114 :         ++ptr;
     315   [ +  +  +  + ]:         114 :         if (ptr < end && val[ptr] == '+')
     316                 :           8 :             ++ptr;
     317   [ +  +  +  + ]:         106 :         else if (ptr < end && val[ptr] == '-') {
     318                 :          19 :             exponent_sign = true;
     319                 :          19 :             ++ptr;
     320                 :             :         }
     321   [ +  +  +  + ]:         114 :         if (ptr < end && IsDigit(val[ptr])) {
     322   [ +  +  +  + ]:       32067 :             while (ptr < end && IsDigit(val[ptr])) {
     323         [ +  + ]:       31982 :                 if (exponent > (UPPER_BOUND / 10LL))
     324                 :             :                     return false; /* overflow */
     325                 :       31973 :                 exponent = exponent * 10 + val[ptr] - '0';
     326                 :       31973 :                 ++ptr;
     327                 :             :             }
     328                 :             :         } else return false; /* missing expected digit */
     329                 :             :     }
     330         [ +  + ]:         642 :     if (ptr != end)
     331                 :             :         return false; /* trailing garbage */
     332                 :             : 
     333                 :             :     /* finalize exponent */
     334         [ +  + ]:         569 :     if (exponent_sign)
     335                 :          13 :         exponent = -exponent;
     336                 :         569 :     exponent = exponent - point_ofs + mantissa_tzeros;
     337                 :             : 
     338                 :             :     /* finalize mantissa */
     339         [ +  + ]:         569 :     if (mantissa_sign)
     340                 :          29 :         mantissa = -mantissa;
     341                 :             : 
     342                 :             :     /* convert to one 64-bit fixed-point value */
     343                 :         569 :     exponent += decimals;
     344         [ +  + ]:         569 :     if (exponent < 0)
     345                 :             :         return false; /* cannot represent values smaller than 10^-decimals */
     346         [ +  + ]:         533 :     if (exponent >= 18)
     347                 :             :         return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
     348                 :             : 
     349         [ +  + ]:        3946 :     for (int i=0; i < exponent; ++i) {
     350         [ +  + ]:        3466 :         if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
     351                 :             :             return false; /* overflow */
     352                 :        3438 :         mantissa *= 10;
     353                 :             :     }
     354         [ +  - ]:         480 :     if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
     355                 :             :         return false; /* overflow */
     356                 :             : 
     357         [ +  - ]:         480 :     if (amount_out)
     358                 :         480 :         *amount_out = mantissa;
     359                 :             : 
     360                 :             :     return true;
     361                 :             : }
     362                 :             : 
     363                 :       39598 : std::string ToLower(std::string_view str)
     364                 :             : {
     365         [ +  - ]:       39598 :     std::string r;
     366         [ +  - ]:       39598 :     r.reserve(str.size());
     367   [ +  +  +  -  :    37311600 :     for (auto ch : str) r += ToLower(ch);
                   +  + ]
     368                 :       39598 :     return r;
     369                 :           0 : }
     370                 :             : 
     371                 :        4002 : std::string ToUpper(std::string_view str)
     372                 :             : {
     373         [ +  - ]:        4002 :     std::string r;
     374         [ +  - ]:        4002 :     r.reserve(str.size());
     375   [ +  +  +  -  :       69666 :     for (auto ch : str) r += ToUpper(ch);
                   +  + ]
     376                 :        4002 :     return r;
     377                 :           0 : }
     378                 :             : 
     379                 :        1010 : std::string Capitalize(std::string str)
     380                 :             : {
     381         [ +  + ]:        1010 :     if (str.empty()) return str;
     382         [ +  + ]:        1697 :     str[0] = ToUpper(str.front());
     383                 :         982 :     return str;
     384                 :             : }
     385                 :             : 
     386                 :           0 : std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
     387                 :             : {
     388         [ #  # ]:           0 :     if (str.empty()) {
     389                 :           0 :         return std::nullopt;
     390                 :             :     }
     391                 :           0 :     auto multiplier = default_multiplier;
     392   [ #  #  #  #  :           0 :     char unit = str.back();
             #  #  #  #  
                      # ]
     393   [ #  #  #  #  :           0 :     switch (unit) {
             #  #  #  #  
                      # ]
     394                 :             :     case 'k':
     395                 :             :         multiplier = ByteUnit::k;
     396                 :             :         break;
     397                 :           0 :     case 'K':
     398                 :           0 :         multiplier = ByteUnit::K;
     399                 :           0 :         break;
     400                 :           0 :     case 'm':
     401                 :           0 :         multiplier = ByteUnit::m;
     402                 :           0 :         break;
     403                 :           0 :     case 'M':
     404                 :           0 :         multiplier = ByteUnit::M;
     405                 :           0 :         break;
     406                 :           0 :     case 'g':
     407                 :           0 :         multiplier = ByteUnit::g;
     408                 :           0 :         break;
     409                 :           0 :     case 'G':
     410                 :           0 :         multiplier = ByteUnit::G;
     411                 :           0 :         break;
     412                 :           0 :     case 't':
     413                 :           0 :         multiplier = ByteUnit::t;
     414                 :           0 :         break;
     415                 :           0 :     case 'T':
     416                 :           0 :         multiplier = ByteUnit::T;
     417                 :           0 :         break;
     418                 :           0 :     default:
     419                 :           0 :         unit = 0;
     420                 :           0 :         break;
     421                 :             :     }
     422                 :             : 
     423                 :           0 :     uint64_t unit_amount = static_cast<uint64_t>(multiplier);
     424                 :           0 :     auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
     425   [ #  #  #  # ]:           0 :     if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) { // check overflow
     426                 :           0 :         return std::nullopt;
     427                 :             :     }
     428                 :           0 :     return *parsed_num * unit_amount;
     429                 :             : }
        

Generated by: LCOV version 2.0-1