ENH: wordRes matcher method that distinguishes literal vs. regex

- useful for customizing the behaviour of white/black lists depending
  on the type of the match.
This commit is contained in:
Mark Olesen
2018-10-07 18:30:33 +02:00
parent 8d6f83e666
commit 3b77493abc
3 changed files with 129 additions and 55 deletions

View File

@ -53,7 +53,7 @@ class wordRes
{
// Private Methods
//- Check for any match of text in list of matchers
//- Smart match as literal or regex, stopping on the first match.
inline static bool found_match
(
const UList<wordRe>& patterns,
@ -61,6 +61,17 @@ class wordRes
bool literal=false
);
//- Smart match across entire list, returning the match type.
// Stops on the first literal match, or continues to examine
// if a regex match occurs.
// \return wordRe::LITERAL, wordRe::REGEX on match and
// wordRe::UNKNOWN otherwise.
inline static wordRe::compOption found_matched
(
const UList<wordRe>& patterns,
const std::string& text
);
public:
@ -111,16 +122,23 @@ public:
// No filtering attempted on regular expressions.
void uniq();
//- Return true if string matches ANY of the regular expressions
// Smart match as regular expression or as a string.
// Optionally force a literal match only
inline bool match(const std::string& text, bool literal=false) const;
//- Smart match as literal or regex, stopping on the first match.
//
// \param literal Force literal match only.
// \return True if text matches ANY of the entries.
inline bool match(const std::string& text, bool literal = false) const;
//- Smart match in the list of matchers, returning the match type.
// It stops if there is a literal match, or continues to examine
// other regexs.
// \return LITERAL if a lteral match was found, REGEX if a regex
// match was found and UNKNOWN otherwise.
inline wordRe::compOption matched(const std::string& text) const;
// Member operators
// Member Operators
//- Perform smart match on text, as per match()
// Allows use as a predicate.
//- Identical to match(), for use as a predicate.
inline bool operator()(const std::string& text) const;
};

View File

@ -50,6 +50,36 @@ inline bool Foam::wordRes::found_match
}
inline Foam::wordRe::compOption Foam::wordRes::found_matched
(
const UList<wordRe>& patterns,
const std::string& text
)
{
auto retval(wordRe::compOption::UNKNOWN);
for (const wordRe& select : patterns)
{
if (select.isLiteral())
{
if (select.match(text, true))
{
return wordRe::compOption::LITERAL;
}
}
else if (wordRe::compOption::UNKNOWN == retval)
{
if (select.match(text, false))
{
retval = wordRe::compOption::REGEX;
}
}
}
return retval;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::wordRes::match(const std::string& text, bool literal) const
@ -58,6 +88,13 @@ inline bool Foam::wordRes::match(const std::string& text, bool literal) const
}
inline Foam::wordRe::compOption
Foam::wordRes::matched(const std::string& text) const
{
return found_matched(*this, text);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline bool Foam::wordRes::operator()(const std::string& text) const