LCOV - code coverage report
Current view: top level - src/util - moneystr.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 61 62 98.4 %
Date: 2022-04-21 14:51:19 Functions: 2 2 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 34 36 94.4 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2021 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/moneystr.h>
#       7                 :            : 
#       8                 :            : #include <consensus/amount.h>
#       9                 :            : #include <tinyformat.h>
#      10                 :            : #include <util/strencodings.h>
#      11                 :            : #include <util/string.h>
#      12                 :            : 
#      13                 :            : #include <optional>
#      14                 :            : 
#      15                 :            : std::string FormatMoney(const CAmount n)
#      16                 :     175385 : {
#      17                 :            :     // Note: not using straight sprintf here because we do NOT want
#      18                 :            :     // localized number formatting.
#      19                 :     175385 :     static_assert(COIN > 1);
#      20                 :     175385 :     int64_t quotient = n / COIN;
#      21                 :     175385 :     int64_t remainder = n % COIN;
#      22         [ +  + ]:     175385 :     if (n < 0) {
#      23                 :        695 :         quotient = -quotient;
#      24                 :        695 :         remainder = -remainder;
#      25                 :        695 :     }
#      26                 :     175385 :     std::string str = strprintf("%d.%08d", quotient, remainder);
#      27                 :            : 
#      28                 :            :     // Right-trim excess zeros before the decimal point:
#      29                 :     175385 :     int nTrim = 0;
#      30 [ +  + ][ +  + ]:     666003 :     for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i)
#      31                 :     490618 :         ++nTrim;
#      32         [ +  + ]:     175385 :     if (nTrim)
#      33                 :     139646 :         str.erase(str.size()-nTrim, nTrim);
#      34                 :            : 
#      35         [ +  + ]:     175385 :     if (n < 0)
#      36                 :        695 :         str.insert((unsigned int)0, 1, '-');
#      37                 :     175385 :     return str;
#      38                 :     175385 : }
#      39                 :            : 
#      40                 :            : 
#      41                 :            : std::optional<CAmount> ParseMoney(const std::string& money_string)
#      42                 :        930 : {
#      43         [ +  + ]:        930 :     if (!ValidAsCString(money_string)) {
#      44                 :          6 :         return std::nullopt;
#      45                 :          6 :     }
#      46                 :        924 :     const std::string str = TrimString(money_string);
#      47         [ +  + ]:        924 :     if (str.empty()) {
#      48                 :          6 :         return std::nullopt;
#      49                 :          6 :     }
#      50                 :            : 
#      51                 :        918 :     std::string strWhole;
#      52                 :        918 :     int64_t nUnits = 0;
#      53                 :        918 :     const char* p = str.c_str();
#      54         [ +  + ]:       1930 :     for (; *p; p++)
#      55                 :       1916 :     {
#      56         [ +  + ]:       1916 :         if (*p == '.')
#      57                 :        890 :         {
#      58                 :        890 :             p++;
#      59                 :        890 :             int64_t nMult = COIN / 10;
#      60 [ +  + ][ +  + ]:       4458 :             while (IsDigit(*p) && (nMult > 0))
#      61                 :       3568 :             {
#      62                 :       3568 :                 nUnits += nMult * (*p++ - '0');
#      63                 :       3568 :                 nMult /= 10;
#      64                 :       3568 :             }
#      65                 :        890 :             break;
#      66                 :        890 :         }
#      67         [ +  + ]:       1026 :         if (IsSpace(*p))
#      68                 :          8 :             return std::nullopt;
#      69         [ +  + ]:       1018 :         if (!IsDigit(*p))
#      70                 :          6 :             return std::nullopt;
#      71                 :       1012 :         strWhole.insert(strWhole.end(), *p);
#      72                 :       1012 :     }
#      73         [ +  + ]:        904 :     if (*p) {
#      74                 :          8 :         return std::nullopt;
#      75                 :          8 :     }
#      76         [ +  + ]:        896 :     if (strWhole.size() > 10) // guard against 63 bit overflow
#      77                 :          2 :         return std::nullopt;
#      78 [ -  + ][ -  + ]:        894 :     if (nUnits < 0 || nUnits > COIN)
#      79                 :          0 :         return std::nullopt;
#      80                 :        894 :     int64_t nWhole = LocaleIndependentAtoi<int64_t>(strWhole);
#      81                 :        894 :     CAmount value = nWhole * COIN + nUnits;
#      82                 :            : 
#      83         [ +  + ]:        894 :     if (!MoneyRange(value)) {
#      84                 :          2 :         return std::nullopt;
#      85                 :          2 :     }
#      86                 :            : 
#      87                 :        892 :     return value;
#      88                 :        894 : }

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a