Fixed implementation: cannot change definition of tl::Extractor::read_word and need a new method to read a word.

This commit is contained in:
Matthias Koefferlein 2019-11-03 21:50:47 +01:00
parent 41ab2d6d79
commit fe9e808d61
5 changed files with 67 additions and 4 deletions

View File

@ -313,7 +313,7 @@ SaltGrain::valid_api_version (const std::string &v)
while (! ex.at_end ()) {
std::string feature;
ex.try_read_word (feature);
ex.try_read_name (feature);
bool first = true;
while (! ex.at_end () && ! ex.test (";")) {

View File

@ -162,7 +162,7 @@ SaltAPIVersionCheck::check (const std::string &api_version)
while (! ex.at_end ()) {
std::string fname;
ex.try_read_word (fname);
ex.try_read_name (fname);
std::string v;
while (! ex.at_end () && ! ex.test (";")) {

View File

@ -1023,6 +1023,15 @@ Extractor::read_word (std::string &value, const char *non_term)
return *this;
}
Extractor &
Extractor::read_name (std::string &value, const char *non_term)
{
if (! try_read_name (value, non_term)) {
error (tl::to_string (tr ("Expected a name string")));
}
return *this;
}
Extractor &
Extractor::read_word_or_quoted (std::string &value, const char *non_term)
{
@ -1228,7 +1237,7 @@ Extractor::try_read (bool &value)
}
bool
Extractor::try_read_word (std::string &string, const char *non_term)
Extractor::try_read_name (std::string &string, const char *non_term)
{
if (! *skip ()) {
return false;
@ -1252,6 +1261,23 @@ Extractor::try_read_word (std::string &string, const char *non_term)
return ! string.empty ();
}
bool
Extractor::try_read_word (std::string &string, const char *non_term)
{
if (! *skip ()) {
return false;
}
string.clear ();
while (*m_cp && (safe_isalnum (*m_cp) || strchr (non_term, *m_cp) != NULL)) {
string += *m_cp;
++m_cp;
}
return ! string.empty ();
}
bool
Extractor::try_read_word_or_quoted (std::string &string, const char *non_term)
{

View File

@ -528,6 +528,13 @@ public:
*/
Extractor &read (std::string &value, const char *term = "");
/**
* @brief Read a name string
*
* Name strings are like words, but for the first character digits are not allowed.
*/
Extractor &read_name (std::string &value, const char *non_term = "_.$");
/**
* @brief Read a string consisting of "word" characters
*
@ -610,6 +617,13 @@ public:
*/
bool try_read (std::string &string, const char *term = "");
/**
* @brief Try to read a name string
*
* Name strings are like words, but for the first character digits are not allowed.
*/
bool try_read_name (std::string &value, const char *non_term = "_.$");
/**
* @brief Try to read a string consisting of "word" characters
*

View File

@ -312,26 +312,49 @@ TEST(8)
x = Extractor ("a_word!");
x.read_word (s);
EXPECT_EQ (s, "a_word");
x = Extractor ("a_word!");
s.clear ();
x.read_name (s);
EXPECT_EQ (s, "a_word");
EXPECT_EQ (x.test ("!"), true);
x = Extractor ("0_word!");
EXPECT_EQ (x.try_read_word (s), false);
EXPECT_EQ (x.try_read_word (s), true);
x = Extractor ("0_word!");
EXPECT_EQ (x.try_read_name (s), false);
x = Extractor ("a_word!");
EXPECT_EQ (x.try_read_word (s), true);
EXPECT_EQ (s, "a_word");
EXPECT_EQ (x.test ("!"), true);
x = Extractor ("a_word!");
EXPECT_EQ (x.try_read_name (s), true);
EXPECT_EQ (s, "a_word");
EXPECT_EQ (x.test ("!"), true);
x = Extractor ("a_word!");
x.read_word (s, "_!");
EXPECT_EQ (s, "a_word!");
EXPECT_EQ (x.at_end (), true);
x = Extractor ("a_word!");
x.read_name (s, "_!");
EXPECT_EQ (s, "a_word!");
EXPECT_EQ (x.at_end (), true);
x = Extractor ("a_word!");
EXPECT_EQ (x.try_read_word (s, "_!"), true);
EXPECT_EQ (s, "a_word!");
EXPECT_EQ (x.at_end (), true);
x = Extractor ("a_word!");
EXPECT_EQ (x.try_read_name (s, "_!"), true);
EXPECT_EQ (s, "a_word!");
EXPECT_EQ (x.at_end (), true);
x = Extractor ("a_word!");
x.read_word_or_quoted (s);
EXPECT_EQ (s, "a_word");