use std::stoXX() functions and check for incomplete conversions and exceptions
This commit is contained in:
@ -398,17 +398,23 @@ double utils::numeric(const char *file, int line, const std::string &str, bool d
|
|||||||
}
|
}
|
||||||
|
|
||||||
double rv = 0;
|
double rv = 0;
|
||||||
|
auto msg = fmt::format("Floating point number {} in input script or data file is invalid", buf);
|
||||||
try {
|
try {
|
||||||
rv = stod(buf);
|
std::size_t endpos;
|
||||||
|
rv = std::stod(buf, &endpos);
|
||||||
|
if (buf.size() != endpos) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
|
}
|
||||||
} catch (std::invalid_argument const &) {
|
} catch (std::invalid_argument const &) {
|
||||||
auto msg = fmt::format("Floating point number {} in input script or data file is invalid", buf);
|
|
||||||
if (do_abort)
|
if (do_abort)
|
||||||
lmp->error->one(file, line, msg);
|
lmp->error->one(file, line, msg);
|
||||||
else
|
else
|
||||||
lmp->error->all(file, line, msg);
|
lmp->error->all(file, line, msg);
|
||||||
} catch (std::out_of_range const &) {
|
} catch (std::out_of_range const &) {
|
||||||
auto msg =
|
msg = fmt::format("Floating point number {} in input script or data file is out of range", buf);
|
||||||
fmt::format("Floating point number {} in input script or data file is out of range", buf);
|
|
||||||
if (do_abort)
|
if (do_abort)
|
||||||
lmp->error->one(file, line, msg);
|
lmp->error->one(file, line, msg);
|
||||||
else
|
else
|
||||||
@ -459,10 +465,23 @@ int utils::inumeric(const char *file, int line, const std::string &str, bool do_
|
|||||||
}
|
}
|
||||||
|
|
||||||
int rv = 0;
|
int rv = 0;
|
||||||
|
auto msg = fmt::format("Integer {} in input script or data file is invalid", buf);
|
||||||
try {
|
try {
|
||||||
rv = stoi(buf);
|
std::size_t endpos;
|
||||||
|
rv = std::stoi(buf, &endpos);
|
||||||
|
if (buf.size() != endpos) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
|
}
|
||||||
|
} catch (std::invalid_argument const &) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
} catch (std::out_of_range const &) {
|
} catch (std::out_of_range const &) {
|
||||||
auto msg = fmt::format("Integer {} in input script or data file is out of range", buf);
|
msg = fmt::format("Integer {} in input script or data file is out of range", buf);
|
||||||
if (do_abort)
|
if (do_abort)
|
||||||
lmp->error->one(file, line, msg);
|
lmp->error->one(file, line, msg);
|
||||||
else
|
else
|
||||||
@ -514,9 +533,22 @@ bigint utils::bnumeric(const char *file, int line, const std::string &str, bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
long long rv = 0;
|
long long rv = 0;
|
||||||
|
auto msg = fmt::format("Integer {} in input script or data file is invalid", buf);
|
||||||
try {
|
try {
|
||||||
rv = stoll(buf);
|
std::size_t endpos;
|
||||||
if (rv > MAXBIGINT) throw std::out_of_range("64-bit");
|
rv = std::stoll(buf, &endpos);
|
||||||
|
if (buf.size() != endpos) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
|
}
|
||||||
|
if ((rv < (-MAXBIGINT - 1) || (rv > MAXBIGINT))) throw std::out_of_range("bigint");
|
||||||
|
} catch (std::invalid_argument const &) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
} catch (std::out_of_range const &) {
|
} catch (std::out_of_range const &) {
|
||||||
auto msg = fmt::format("Integer {} in input script or data file is out of range", buf);
|
auto msg = fmt::format("Integer {} in input script or data file is out of range", buf);
|
||||||
if (do_abort)
|
if (do_abort)
|
||||||
@ -570,9 +602,22 @@ tagint utils::tnumeric(const char *file, int line, const std::string &str, bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
long long rv = 0;
|
long long rv = 0;
|
||||||
|
auto msg = fmt::format("Integer {} in input script or data file is invalid", buf);
|
||||||
try {
|
try {
|
||||||
rv = stoll(buf);
|
std::size_t endpos;
|
||||||
if (rv > MAXTAGINT) throw std::out_of_range("64-bit");
|
rv = std::stoll(buf, &endpos);
|
||||||
|
if (buf.size() != endpos) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
|
}
|
||||||
|
if ((rv < (-MAXTAGINT - 1) || (rv > MAXTAGINT))) throw std::out_of_range("tagint");
|
||||||
|
} catch (std::invalid_argument const &) {
|
||||||
|
if (do_abort)
|
||||||
|
lmp->error->one(file, line, msg);
|
||||||
|
else
|
||||||
|
lmp->error->all(file, line, msg);
|
||||||
} catch (std::out_of_range const &) {
|
} catch (std::out_of_range const &) {
|
||||||
auto msg = fmt::format("Integer {} in input script or data file is out of range", buf);
|
auto msg = fmt::format("Integer {} in input script or data file is out of range", buf);
|
||||||
if (do_abort)
|
if (do_abort)
|
||||||
@ -614,19 +659,19 @@ void utils::bounds(const char *file, int line, const std::string &str,
|
|||||||
|
|
||||||
found = str.find_first_of('*');
|
found = str.find_first_of('*');
|
||||||
if (found == std::string::npos) { // contains no '*'
|
if (found == std::string::npos) { // contains no '*'
|
||||||
nlo = nhi = strtol(str.c_str(), nullptr, 10);
|
nlo = nhi = std::stol(str, nullptr, 10);
|
||||||
} else if (str.size() == 1) { // is only '*'
|
} else if (str.size() == 1) { // is only '*'
|
||||||
nlo = nmin;
|
nlo = nmin;
|
||||||
nhi = nmax;
|
nhi = nmax;
|
||||||
} else if (found == 0) { // is '*j'
|
} else if (found == 0) { // is '*j'
|
||||||
nlo = nmin;
|
nlo = nmin;
|
||||||
nhi = strtol(str.substr(1).c_str(), nullptr, 10);
|
nhi = std::stol(str.substr(1), nullptr, 10);
|
||||||
} else if (str.size() == found + 1) { // is 'i*'
|
} else if (str.size() == found + 1) { // is 'i*'
|
||||||
nlo = strtol(str.c_str(), nullptr, 10);
|
nlo = std::stol(str, nullptr, 10);
|
||||||
nhi = nmax;
|
nhi = nmax;
|
||||||
} else { // is 'i*j'
|
} else { // is 'i*j'
|
||||||
nlo = strtol(str.c_str(), nullptr, 10);
|
nlo = std::stol(str, nullptr, 10);
|
||||||
nhi = strtol(str.substr(found + 1).c_str(), nullptr, 10);
|
nhi = std::stol(str.substr(found + 1), nullptr, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -1697,10 +1742,10 @@ double utils::timespec2seconds(const std::string ×pec)
|
|||||||
int utils::date2num(const std::string &date)
|
int utils::date2num(const std::string &date)
|
||||||
{
|
{
|
||||||
std::size_t found = date.find_first_not_of("0123456789 ");
|
std::size_t found = date.find_first_not_of("0123456789 ");
|
||||||
int num = strtol(date.substr(0, found).c_str(), nullptr, 10);
|
int num = std::stol(date.substr(0, found), nullptr, 10);
|
||||||
auto month = date.substr(found);
|
auto month = date.substr(found);
|
||||||
found = month.find_first_of("0123456789 ");
|
found = month.find_first_of("0123456789 ");
|
||||||
num += strtol(month.substr(found).c_str(), nullptr, 10) * 10000;
|
num += std::stol(month.substr(found), nullptr, 10) * 10000;
|
||||||
if (num < 1000000) num += 20000000;
|
if (num < 1000000) num += 20000000;
|
||||||
|
|
||||||
if (strmatch(month, "^Jan"))
|
if (strmatch(month, "^Jan"))
|
||||||
|
|||||||
Reference in New Issue
Block a user