new convencience function for checking valid IDs (includes unit tests)
This commit is contained in:
@ -851,7 +851,7 @@ std::vector<std::string> 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
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
@ -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)".
|
||||
|
||||
@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user