tighter checking of what is a valid integer/floating point number

also use the check consistently when converting numbers
This commit is contained in:
Axel Kohlmeyer
2022-12-17 22:06:50 -05:00
parent b6c7d24b6d
commit 5a18cea6c9
2 changed files with 35 additions and 16 deletions

View File

@ -381,7 +381,7 @@ double utils::numeric(const char *file, int line, const std::string &str, bool d
std::string buf(str); std::string buf(str);
if (has_utf8(buf)) buf = utf8_subst(buf); if (has_utf8(buf)) buf = utf8_subst(buf);
if (buf.find_first_not_of("0123456789-+.eE") != std::string::npos) { if (!is_double(buf)) {
std::string msg("Expected floating point parameter instead of '"); std::string msg("Expected floating point parameter instead of '");
msg += buf + "' in input script or data file"; msg += buf + "' in input script or data file";
if (do_abort) if (do_abort)
@ -425,7 +425,7 @@ int utils::inumeric(const char *file, int line, const std::string &str, bool do_
std::string buf(str); std::string buf(str);
if (has_utf8(buf)) buf = utf8_subst(buf); if (has_utf8(buf)) buf = utf8_subst(buf);
if (buf.find_first_not_of("0123456789-+") != std::string::npos) { if (!is_integer(buf)) {
std::string msg("Expected integer parameter instead of '"); std::string msg("Expected integer parameter instead of '");
msg += buf + "' in input script or data file"; msg += buf + "' in input script or data file";
if (do_abort) if (do_abort)
@ -470,7 +470,7 @@ bigint utils::bnumeric(const char *file, int line, const std::string &str, bool
std::string buf(str); std::string buf(str);
if (has_utf8(buf)) buf = utf8_subst(buf); if (has_utf8(buf)) buf = utf8_subst(buf);
if (buf.find_first_not_of("0123456789-+") != std::string::npos) { if (!is_integer(buf)) {
std::string msg("Expected integer parameter instead of '"); std::string msg("Expected integer parameter instead of '");
msg += buf + "' in input script or data file"; msg += buf + "' in input script or data file";
if (do_abort) if (do_abort)
@ -515,7 +515,7 @@ tagint utils::tnumeric(const char *file, int line, const std::string &str, bool
std::string buf(str); std::string buf(str);
if (has_utf8(buf)) buf = utf8_subst(buf); if (has_utf8(buf)) buf = utf8_subst(buf);
if (buf.find_first_not_of("0123456789-+") != std::string::npos) { if (!is_integer(buf)) {
std::string msg("Expected integer parameter instead of '"); std::string msg("Expected integer parameter instead of '");
msg += buf + "' in input script or data file"; msg += buf + "' in input script or data file";
if (do_abort) if (do_abort)
@ -866,7 +866,6 @@ std::string utils::star_subst(const std::string &name, bigint step, int pad)
return fmt::format("{}{:0{}}{}", name.substr(0, star), step, pad, name.substr(star + 1)); return fmt::format("{}{:0{}}{}", name.substr(0, star), step, pad, name.substr(star + 1));
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
Remove accelerator style suffix from string Remove accelerator style suffix from string
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
@ -1153,11 +1152,7 @@ bool utils::is_integer(const std::string &str)
{ {
if (str.empty()) return false; if (str.empty()) return false;
for (const auto &c : str) { return strmatch(str, "^[+-]?\\d+$");
if (isdigit(c) || c == '-' || c == '+') continue;
return false;
}
return true;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -1168,13 +1163,12 @@ bool utils::is_double(const std::string &str)
{ {
if (str.empty()) return false; if (str.empty()) return false;
for (const auto &c : str) { if (strmatch(str, "^[-+]?\\d+\\.?\\d*$") || strmatch(str, "^[-+]?\\d*\\.\\d+$") ||
if (isdigit(c)) continue; strmatch(str, "^[-+]?\\d+\\.?\\d*[eE][-+]?\\d+$") ||
if (c == '-' || c == '+' || c == '.') continue; strmatch(str, "^[-+]?\\d*\\.\\d+[eE][-+]?\\d+$"))
if (c == 'e' || c == 'E') continue; return true;
else
return false; return false;
}
return true;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------

View File

@ -315,6 +315,26 @@ TEST(Utils, text_not_an_integer)
ASSERT_FALSE(utils::is_integer("one")); ASSERT_FALSE(utils::is_integer("one"));
} }
TEST(Utils, minus_not_an_integer)
{
ASSERT_FALSE(utils::is_integer("1-"));
}
TEST(Utils, plus_not_an_integer)
{
ASSERT_FALSE(utils::is_integer("1+"));
}
TEST(Utils, minus_not_a_double)
{
ASSERT_FALSE(utils::is_double("1-"));
}
TEST(Utils, plus_not_a_double)
{
ASSERT_FALSE(utils::is_double("1+"));
}
TEST(Utils, text_not_a_double) TEST(Utils, text_not_a_double)
{ {
ASSERT_FALSE(utils::is_double("half")); ASSERT_FALSE(utils::is_double("half"));
@ -365,6 +385,11 @@ TEST(Utils, signed_double_and_exponential)
ASSERT_TRUE(utils::is_double("-10E-22")); ASSERT_TRUE(utils::is_double("-10E-22"));
} }
TEST(Utils, signed_double_and_broken_exponential)
{
ASSERT_FALSE(utils::is_double("-10e10-2"));
}
TEST(Utils, is_double_with_d_exponential) TEST(Utils, is_double_with_d_exponential)
{ {
ASSERT_FALSE(utils::is_double("10d22")); ASSERT_FALSE(utils::is_double("10d22"));