Use strto* instead of std::sto*

std::sto* throw if the string does not start with a number, but doesn't throw
if it's not just a number. No idea who invented such behaviour ¯\_(ツ)_/¯
mutable-v2
Vitaliy Filippov 2021-11-16 12:48:28 +03:00
parent 34781ed7ee
commit 55363fc265
1 changed files with 24 additions and 3 deletions

View File

@ -214,9 +214,30 @@ class JsonString final : public Value<Json::STRING, string> {
string as_string() const { return m_value; }
const string &string_value() const override { return m_value; }
bool bool_value() const override { return m_value != "" && m_value != "0"; }
double number_value() const override { size_t pos = 0; double v = std::stod(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; }
int64_ int64_value() const override { size_t pos = 0; int64_ v = std::stoll(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; }
uint64_ uint64_value() const override { size_t pos = 0; uint64_ v = std::stoull(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; }
double number_value() const override
{
char *endptr = NULL;
double v = strtod(m_value.c_str(), &endptr);
if (endptr < m_value.c_str()+m_value.length())
v = 0;
return v;
}
int64_ int64_value() const override
{
char *endptr = NULL;
int64_ v = strtoll(m_value.c_str(), &endptr, 10);
if (endptr < m_value.c_str()+m_value.length())
v = 0;
return v;
}
uint64_ uint64_value() const override
{
char *endptr = NULL;
uint64_ v = strtoull(m_value.c_str(), &endptr, 10);
if (endptr < m_value.c_str()+m_value.length())
v = 0;
return v;
}
public:
explicit JsonString(const string &value) : Value(value) {}
explicit JsonString(string &&value) : Value(move(value)) {}