Add utils::is_double and utils::is_integer

This commit is contained in:
Richard Berger
2020-05-18 16:35:33 -04:00
parent 9e3759d0fc
commit 46239e4577
3 changed files with 84 additions and 0 deletions

View File

@ -348,6 +348,40 @@ size_t utils::count_words(const std::string & text, const std::string & seperato
return words.count();
}
/* ----------------------------------------------------------------------
Return whether string is a valid integer number
------------------------------------------------------------------------- */
bool utils::is_integer(const std::string & str) {
if (str.size() == 0) {
return false;
}
for (auto c : str) {
if (isdigit(c) || c == '-' || c == '+') continue;
return false;
}
return true;
}
/* ----------------------------------------------------------------------
Return whether string is a valid floating-point number
------------------------------------------------------------------------- */
bool utils::is_double(const std::string & str) {
if (str.size() == 0) {
return false;
}
for (auto c : str) {
if (isdigit(c)) continue;
if (c == '-' || c == '+' || c == '.') continue;
if (c == 'e' || c == 'E') continue;
return false;
}
return true;
}
/* ------------------------------------------------------------------ */
extern "C" {