diff --git a/src/utils.cpp b/src/utils.cpp index d9320f464e..7bb157afe5 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -851,7 +851,7 @@ std::vector utils::split_words(const std::string &text) ------------------------------------------------------------------------- */ bool utils::is_integer(const std::string &str) { - if (str.size() == 0) { + if (str.empty()) { return false; } @@ -867,7 +867,7 @@ bool utils::is_integer(const std::string &str) { ------------------------------------------------------------------------- */ bool utils::is_double(const std::string &str) { - if (str.size() == 0) { + if (str.empty()) { return false; } @@ -880,6 +880,22 @@ bool utils::is_double(const std::string &str) { return true; } +/* ---------------------------------------------------------------------- + Return whether string is a valid ID string +------------------------------------------------------------------------- */ + +bool utils::is_id(const std::string &str) { + if (str.empty()) { + return false; + } + + for (auto c : str) { + if (isalnum(c) || (c == '_')) continue; + return false; + } + return true; +} + /* ---------------------------------------------------------------------- strip off leading part of path, return just the filename ------------------------------------------------------------------------- */ diff --git a/src/utils.h b/src/utils.h index 70a4c08cd3..eab81f1343 100644 --- a/src/utils.h +++ b/src/utils.h @@ -335,6 +335,14 @@ namespace LAMMPS_NS { bool is_double(const std::string &str); + /** Check if string is a valid ID + * ID strings may contain only letters, numbers, and underscores. + * + * \param str string that should be checked + * \return true, if string contains valid id, false otherwise */ + + bool is_id(const std::string &str); + /** Try to detect pathname from FILE pointer. * * Currently only supported on Linux, otherwise will report "(unknown)". diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index a0e4022d2c..5b509318c9 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -287,6 +287,61 @@ TEST(Utils, signed_double_and_d_exponential) ASSERT_FALSE(utils::is_double("-10D-22")); } +TEST(Utils, valid_id1) +{ + ASSERT_TRUE(utils::is_id("abc")); +} + +TEST(Utils, valid_id2) +{ + ASSERT_TRUE(utils::is_id("123")); +} + +TEST(Utils, valid_id3) +{ + ASSERT_TRUE(utils::is_id("abc123")); +} + +TEST(Utils, valid_id4) +{ + ASSERT_TRUE(utils::is_id("abc_123")); +} + +TEST(Utils, valid_id5) +{ + ASSERT_TRUE(utils::is_id("123_abc")); +} + +TEST(Utils, valid_id6) +{ + ASSERT_TRUE(utils::is_id("_123")); +} + +TEST(Utils, valid_id7) +{ + ASSERT_TRUE(utils::is_id("___")); +} + +TEST(Utils, invalid_id1) +{ + ASSERT_FALSE(utils::is_id("+abc")); +} + +TEST(Utils, invalid_id2) +{ + ASSERT_FALSE(utils::is_id("a[1]")); +} + +TEST(Utils, invalid_id3) +{ + ASSERT_FALSE(utils::is_id("b(c)")); +} + +TEST(Utils, invalid_id4) +{ + ASSERT_FALSE(utils::is_id("a$12")); +} + TEST(Utils, strmatch_beg) { ASSERT_TRUE(utils::strmatch("rigid/small/omp", "^rigid"));