Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/util/moneystr.cpp
Line
Count
Source
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/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 <cstdint>
14
#include <optional>
15
16
using util::ContainsNoNUL;
17
using util::TrimString;
18
19
std::string FormatMoney(const CAmount n)
20
217k
{
21
    // Note: not using straight sprintf here because we do NOT want
22
    // localized number formatting.
23
217k
    static_assert(COIN > 1);
24
217k
    int64_t quotient = n / COIN;
25
217k
    int64_t remainder = n % COIN;
26
217k
    if (n < 0) {
27
1.40k
        quotient = -quotient;
28
1.40k
        remainder = -remainder;
29
1.40k
    }
30
217k
    std::string str = strprintf("%d.%08d", quotient, remainder);
31
32
    // Right-trim excess zeros before the decimal point:
33
217k
    int nTrim = 0;
34
941k
    for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i)
35
723k
        ++nTrim;
36
217k
    if (nTrim)
37
179k
        str.erase(str.size()-nTrim, nTrim);
38
39
217k
    if (n < 0)
40
1.40k
        str.insert(uint32_t{0}, 1, '-');
41
217k
    return str;
42
217k
}
43
44
45
std::optional<CAmount> ParseMoney(const std::string& money_string)
46
1.32k
{
47
1.32k
    if (!ContainsNoNUL(money_string)) {
48
6
        return std::nullopt;
49
6
    }
50
1.32k
    const std::string str = TrimString(money_string);
51
1.32k
    if (str.empty()) {
52
6
        return std::nullopt;
53
6
    }
54
55
1.31k
    std::string strWhole;
56
1.31k
    int64_t nUnits = 0;
57
1.31k
    const char* p = str.c_str();
58
2.72k
    for (; *p; p++)
59
2.61k
    {
60
2.61k
        if (*p == '.')
61
1.18k
        {
62
1.18k
            p++;
63
1.18k
            int64_t nMult = COIN / 10;
64
6.42k
            while (IsDigit(*p) && (nMult > 0))
65
5.23k
            {
66
5.23k
                nUnits += nMult * (*p++ - '0');
67
5.23k
                nMult /= 10;
68
5.23k
            }
69
1.18k
            break;
70
1.18k
        }
71
1.42k
        if (IsSpace(*p))
72
8
            return std::nullopt;
73
1.41k
        if (!IsDigit(*p))
74
6
            return std::nullopt;
75
1.40k
        strWhole.insert(strWhole.end(), *p);
76
1.40k
    }
77
1.30k
    if (*p) {
78
8
        return std::nullopt;
79
8
    }
80
1.29k
    if (strWhole.size() > 10) // guard against 63 bit overflow
81
2
        return std::nullopt;
82
1.29k
    if (nUnits < 0 || nUnits > COIN)
83
0
        return std::nullopt;
84
1.29k
    int64_t nWhole = LocaleIndependentAtoi<int64_t>(strWhole);
85
1.29k
    CAmount value = nWhole * COIN + nUnits;
86
87
1.29k
    if (!MoneyRange(value)) {
88
2
        return std::nullopt;
89
2
    }
90
91
1.28k
    return value;
92
1.29k
}