/bitcoin/src/util/moneystr.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-2022 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 | 260k | { |
21 | | // Note: not using straight sprintf here because we do NOT want |
22 | | // localized number formatting. |
23 | 260k | static_assert(COIN > 1); |
24 | 260k | int64_t quotient = n / COIN; |
25 | 260k | int64_t remainder = n % COIN; |
26 | 260k | if (n < 0) { Branch (26:9): [True: 0, False: 260k]
|
27 | 0 | quotient = -quotient; |
28 | 0 | remainder = -remainder; |
29 | 0 | } |
30 | 260k | std::string str = strprintf("%d.%08d", quotient, remainder); |
31 | | |
32 | | // Right-trim excess zeros before the decimal point: |
33 | 260k | int nTrim = 0; |
34 | 1.59M | for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i) Branch (34:33): [True: 1.50M, False: 82.5k]
Branch (34:50): [True: 1.33M, False: 177k]
|
35 | 1.33M | ++nTrim; |
36 | 260k | if (nTrim) Branch (36:9): [True: 257k, False: 2.55k]
|
37 | 257k | str.erase(str.size()-nTrim, nTrim); |
38 | | |
39 | 260k | if (n < 0) Branch (39:9): [True: 0, False: 260k]
|
40 | 0 | str.insert(uint32_t{0}, 1, '-'); |
41 | 260k | return str; |
42 | 260k | } |
43 | | |
44 | | |
45 | | std::optional<CAmount> ParseMoney(const std::string& money_string) |
46 | 11.0k | { |
47 | 11.0k | if (!ContainsNoNUL(money_string)) { Branch (47:9): [True: 0, False: 11.0k]
|
48 | 0 | return std::nullopt; |
49 | 0 | } |
50 | 11.0k | const std::string str = TrimString(money_string); |
51 | 11.0k | if (str.empty()) { Branch (51:9): [True: 0, False: 11.0k]
|
52 | 0 | return std::nullopt; |
53 | 0 | } |
54 | | |
55 | 11.0k | std::string strWhole; |
56 | 11.0k | int64_t nUnits = 0; |
57 | 11.0k | const char* p = str.c_str(); |
58 | 22.1k | for (; *p; p++) Branch (58:12): [True: 22.1k, False: 0]
|
59 | 22.1k | { |
60 | 22.1k | if (*p == '.') Branch (60:13): [True: 11.0k, False: 11.0k]
|
61 | 11.0k | { |
62 | 11.0k | p++; |
63 | 11.0k | int64_t nMult = COIN / 10; |
64 | 55.4k | while (IsDigit(*p) && (nMult > 0)) Branch (64:20): [True: 44.3k, False: 11.0k]
Branch (64:35): [True: 44.3k, False: 0]
|
65 | 44.3k | { |
66 | 44.3k | nUnits += nMult * (*p++ - '0'); |
67 | 44.3k | nMult /= 10; |
68 | 44.3k | } |
69 | 11.0k | break; |
70 | 11.0k | } |
71 | 11.0k | if (IsSpace(*p)) Branch (71:13): [True: 0, False: 11.0k]
|
72 | 0 | return std::nullopt; |
73 | 11.0k | if (!IsDigit(*p)) Branch (73:13): [True: 0, False: 11.0k]
|
74 | 0 | return std::nullopt; |
75 | 11.0k | strWhole.insert(strWhole.end(), *p); |
76 | 11.0k | } |
77 | 11.0k | if (*p) { Branch (77:9): [True: 0, False: 11.0k]
|
78 | 0 | return std::nullopt; |
79 | 0 | } |
80 | 11.0k | if (strWhole.size() > 10) // guard against 63 bit overflow Branch (80:9): [True: 0, False: 11.0k]
|
81 | 0 | return std::nullopt; |
82 | 11.0k | if (nUnits < 0 || nUnits > COIN) Branch (82:9): [True: 0, False: 11.0k]
Branch (82:23): [True: 0, False: 11.0k]
|
83 | 0 | return std::nullopt; |
84 | 11.0k | int64_t nWhole = LocaleIndependentAtoi<int64_t>(strWhole); |
85 | 11.0k | CAmount value = nWhole * COIN + nUnits; |
86 | | |
87 | 11.0k | if (!MoneyRange(value)) { Branch (87:9): [True: 0, False: 11.0k]
|
88 | 0 | return std::nullopt; |
89 | 0 | } |
90 | | |
91 | 11.0k | return value; |
92 | 11.0k | } |