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