aboutsummaryrefslogtreecommitdiff
path: root/src/util/moneystr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/moneystr.cpp')
-rw-r--r--src/util/moneystr.cpp30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/util/moneystr.cpp b/src/util/moneystr.cpp
index 3f9ce7dce4..1aed7daacf 100644
--- a/src/util/moneystr.cpp
+++ b/src/util/moneystr.cpp
@@ -5,10 +5,13 @@
#include <util/moneystr.h>
+#include <consensus/amount.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <util/string.h>
+#include <optional>
+
std::string FormatMoney(const CAmount n)
{
// Note: not using straight sprintf here because we do NOT want
@@ -35,14 +38,14 @@ std::string FormatMoney(const CAmount n)
}
-bool ParseMoney(const std::string& money_string, CAmount& nRet)
+std::optional<CAmount> ParseMoney(const std::string& money_string)
{
if (!ValidAsCString(money_string)) {
- return false;
+ return std::nullopt;
}
const std::string str = TrimString(money_string);
if (str.empty()) {
- return false;
+ return std::nullopt;
}
std::string strWhole;
@@ -62,21 +65,24 @@ bool ParseMoney(const std::string& money_string, CAmount& nRet)
break;
}
if (IsSpace(*p))
- return false;
+ return std::nullopt;
if (!IsDigit(*p))
- return false;
+ return std::nullopt;
strWhole.insert(strWhole.end(), *p);
}
if (*p) {
- return false;
+ return std::nullopt;
}
if (strWhole.size() > 10) // guard against 63 bit overflow
- return false;
+ return std::nullopt;
if (nUnits < 0 || nUnits > COIN)
- return false;
- int64_t nWhole = atoi64(strWhole);
- CAmount nValue = nWhole*COIN + nUnits;
+ return std::nullopt;
+ int64_t nWhole = LocaleIndependentAtoi<int64_t>(strWhole);
+ CAmount value = nWhole * COIN + nUnits;
+
+ if (!MoneyRange(value)) {
+ return std::nullopt;
+ }
- nRet = nValue;
- return true;
+ return value;
}