/root/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::ContainsNUL; |
17 | | using util::TrimString; |
18 | | |
19 | | std::string FormatMoney(const CAmount n) |
20 | 3.32M | { |
21 | | // Note: not using straight sprintf here because we do NOT want |
22 | | // localized number formatting. |
23 | 3.32M | static_assert(COIN > 1); |
24 | 3.32M | int64_t quotient = n / COIN; |
25 | 3.32M | int64_t remainder = n % COIN; |
26 | 3.32M | if (n < 0) { Branch (26:9): [True: 1.18M, False: 2.14M]
|
27 | 1.18M | quotient = -quotient; |
28 | 1.18M | remainder = -remainder; |
29 | 1.18M | } |
30 | 3.32M | std::string str = strprintf("%d.%08d", quotient, remainder); |
31 | | |
32 | | // Right-trim excess zeros before the decimal point: |
33 | 3.32M | int nTrim = 0; |
34 | 3.98M | for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i) Branch (34:33): [True: 709k, False: 3.27M]
Branch (34:50): [True: 661k, False: 48.8k]
|
35 | 661k | ++nTrim; |
36 | 3.32M | if (nTrim) Branch (36:9): [True: 337k, False: 2.98M]
|
37 | 337k | str.erase(str.size()-nTrim, nTrim); |
38 | | |
39 | 3.32M | if (n < 0) Branch (39:9): [True: 1.18M, False: 2.14M]
|
40 | 1.18M | str.insert(uint32_t{0}, 1, '-'); |
41 | 3.32M | return str; |
42 | 3.32M | } |
43 | | |
44 | | |
45 | | std::optional<CAmount> ParseMoney(const std::string& money_string) |
46 | 799 | { |
47 | 799 | if (ContainsNUL(money_string)) { Branch (47:9): [True: 8, False: 791]
|
48 | 8 | return std::nullopt; |
49 | 8 | } |
50 | 791 | const std::string str = TrimString(money_string); |
51 | 791 | if (str.empty()) { Branch (51:9): [True: 3, False: 788]
|
52 | 3 | return std::nullopt; |
53 | 3 | } |
54 | | |
55 | 788 | std::string strWhole; |
56 | 788 | int64_t nUnits = 0; |
57 | 788 | const char* p = str.c_str(); |
58 | 990k | for (; *p; p++) Branch (58:12): [True: 990k, False: 34]
|
59 | 990k | { |
60 | 990k | if (*p == '.') Branch (60:13): [True: 178, False: 990k]
|
61 | 178 | { |
62 | 178 | p++; |
63 | 178 | int64_t nMult = COIN / 10; |
64 | 1.42k | while (IsDigit(*p) && (nMult > 0)) Branch (64:20): [True: 1.25k, False: 174]
Branch (64:35): [True: 1.24k, False: 4]
|
65 | 1.24k | { |
66 | 1.24k | nUnits += nMult * (*p++ - '0'); |
67 | 1.24k | nMult /= 10; |
68 | 1.24k | } |
69 | 178 | break; |
70 | 178 | } |
71 | 990k | if (IsSpace(*p)) Branch (71:13): [True: 10, False: 990k]
|
72 | 10 | return std::nullopt; |
73 | 990k | if (!IsDigit(*p)) Branch (73:13): [True: 566, False: 989k]
|
74 | 566 | return std::nullopt; |
75 | 989k | strWhole.insert(strWhole.end(), *p); |
76 | 989k | } |
77 | 212 | if (*p) { Branch (77:9): [True: 15, False: 197]
|
78 | 15 | return std::nullopt; |
79 | 15 | } |
80 | 197 | if (strWhole.size() > 10) // guard against 63 bit overflow Branch (80:9): [True: 82, False: 115]
|
81 | 82 | return std::nullopt; |
82 | 115 | if (nUnits < 0 || nUnits > COIN) Branch (82:9): [True: 0, False: 115]
Branch (82:23): [True: 0, False: 115]
|
83 | 0 | return std::nullopt; |
84 | 115 | int64_t nWhole = LocaleIndependentAtoi<int64_t>(strWhole); |
85 | 115 | CAmount value = nWhole * COIN + nUnits; |
86 | | |
87 | 115 | if (!MoneyRange(value)) { Branch (87:9): [True: 19, False: 96]
|
88 | 19 | return std::nullopt; |
89 | 19 | } |
90 | | |
91 | 96 | return value; |
92 | 115 | } |