add a "matches()" method to the Tokenizer and ValueTokenizer classes using utils::strmatch()

This commit is contained in:
Axel Kohlmeyer
2024-07-05 15:45:49 -04:00
parent eeaecb3ed3
commit 8fcde04097
3 changed files with 73 additions and 0 deletions

View File

@ -99,6 +99,8 @@ void Tokenizer::reset()
}
/*! Search the text to be processed for a sub-string.
*
* This method does a generic substring match.
*
* \param str string to be searched for
* \return true if string was found, false if not */
@ -107,6 +109,20 @@ bool Tokenizer::contains(const std::string &str) const
return text.find(str) != std::string::npos;
}
/*! Search the text to be processed for regular expression match.
*
\verbatim embed:rst
This method matches the current string against a regular expression using
the :cpp:func:`utils::strmatch() <LAMMPS_NS::utils::strmatch>` function.
\endverbatim
*
* \param str regular expression to be matched against
* \return true if string was found, false if not */
bool Tokenizer::matches(const std::string &str) const
{
return utils::strmatch(text, str);
}
/*! Skip over a given number of tokens
*
* \param n number of tokens to skip over */
@ -233,6 +249,8 @@ bool ValueTokenizer::has_next() const
}
/*! Search the text to be processed for a sub-string.
*
* This method does a generic substring match.
*
* \param value string with value to be searched for
* \return true if string was found, false if not */
@ -241,6 +259,20 @@ bool ValueTokenizer::contains(const std::string &value) const
return tokens.contains(value);
}
/*! Search the text to be processed for regular expression match.
*
\verbatim embed:rst
This method matches the current string against a regular expression using
the :cpp:func:`utils::strmatch() <LAMMPS_NS::utils::strmatch>` function.
\endverbatim
*
* \param str regular expression to be matched against
* \return true if string was found, false if not */
bool ValueTokenizer::matches(const std::string &str) const
{
return tokens.matches(str);
}
/*! Retrieve next token
*
* \return string with next token */

View File

@ -46,6 +46,7 @@ class Tokenizer {
void skip(int n = 1);
bool has_next() const;
bool contains(const std::string &str) const;
bool matches(const std::string &str) const;
std::string next();
size_t count();
@ -119,6 +120,7 @@ class ValueTokenizer {
bool has_next() const;
bool contains(const std::string &value) const;
bool matches(const std::string &str) const;
void skip(int ntokens = 1);
size_t count();

View File

@ -173,6 +173,32 @@ TEST(Tokenizer, default_separators)
ASSERT_EQ(t.count(), 2);
}
TEST(Tokenizer, contains)
{
Tokenizer values("test word");
ASSERT_TRUE(values.contains("test"));
ASSERT_TRUE(values.contains("word"));
}
TEST(Tokenizer, not_contains)
{
Tokenizer values("test word");
ASSERT_FALSE(values.contains("test2"));
}
TEST(Tokenizer, matches)
{
Tokenizer values("test word");
ASSERT_TRUE(values.matches("test"));
ASSERT_TRUE(values.matches("word"));
}
TEST(Tokenizer, not_matches)
{
Tokenizer values("test word");
ASSERT_FALSE(values.matches("test2"));
}
TEST(Tokenizer, as_vector1)
{
Tokenizer t(" \r\n test \t word \f");
@ -346,6 +372,19 @@ TEST(ValueTokenizer, not_contains)
ASSERT_FALSE(values.contains("test2"));
}
TEST(ValueTokenizer, matches)
{
ValueTokenizer values("test word");
ASSERT_TRUE(values.matches("test"));
ASSERT_TRUE(values.matches("word"));
}
TEST(ValueTokenizer, not_matches)
{
ValueTokenizer values("test word");
ASSERT_FALSE(values.matches("test2"));
}
TEST(ValueTokenizer, missing_int)
{
ValueTokenizer values("10");