diff --git a/src/utils.cpp b/src/utils.cpp index 367f1a01db..65487be31c 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -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" { diff --git a/src/utils.h b/src/utils.h index 4698035130..3c37b18e20 100644 --- a/src/utils.h +++ b/src/utils.h @@ -142,6 +142,20 @@ namespace LAMMPS_NS { * \return number of words found */ size_t count_words(const std::string & text, const std::string & seperators = " \t\r\n\f"); + + /** + * \brief Check if string can be converted to valid integer + * \param text string that should be checked + * \return true, if string contains valid integer, false otherwise + */ + bool is_integer(const std::string & str); + + /** + * \brief Check if string can be converted to valid floating-point number + * \param text string that should be checked + * \return true, if string contains valid floating-point number, false otherwise + */ + bool is_double(const std::string & str); } } diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 87d85a536a..f7f298920f 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -14,3 +14,39 @@ TEST(Utils, trim_comment) { TEST(Utils, count_words) { ASSERT_EQ(utils::count_words("some text # comment"), 2); } + +TEST(Utils, valid_integer) { + ASSERT_TRUE(utils::is_integer("10")); +} + +TEST(Utils, valid_double) { + ASSERT_TRUE(utils::is_double("10.0")); +} + +TEST(Utils, empty_not_an_integer) { + ASSERT_FALSE(utils::is_integer("")); +} + +TEST(Utils, empty_not_a_double) { + ASSERT_FALSE(utils::is_double("")); +} + +TEST(Utils, double_not_an_integer) { + ASSERT_FALSE(utils::is_integer("10.0")); +} + +TEST(Utils, integer_is_double) { + ASSERT_TRUE(utils::is_double("10")); +} + +TEST(Utils, is_double_with_exponential) { + ASSERT_TRUE(utils::is_double("10e22")); +} + +TEST(Utils, is_double_with_neg_exponential) { + ASSERT_TRUE(utils::is_double("10e-22")); +} + +TEST(Utils, signed_double_and_exponential) { + ASSERT_TRUE(utils::is_double("-10E-22")); +}