1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
/***********************************************/
/**
* @file conditionStringMatchPattern.h
*
* @brief Determines if a pattern or a regular expression matches the entire string.
*
* @author Torsten Mayer-Guerr
* @date 2019-03-17
*
*/
/***********************************************/
#ifndef __GROOPS_CONDITIONSTRINGMATCHPATTERN__
#define __GROOPS_CONDITIONSTRINGMATCHPATTERN__
// Latex documentation
#ifdef DOCSTRING_Condition
static const char *docstringConditionStringMatchPattern = R"(
\subsection{StringMatchPattern}
Determines if a pattern or a regular expression matches the entire string.
)";
#endif
/***********************************************/
#include "base/import.h"
#include "base/string.h"
#include "classes/condition/condition.h"
#include <regex>
/***** CLASS ***********************************/
/** @brief Determines if a pattern or a regular expression matches the entire string.
* @ingroup conditionGroup
* @see Condition */
class ConditionStringMatchPattern : public Condition
{
FileName text, pattern;
Bool isRegularExpression, caseSensitive;
public:
ConditionStringMatchPattern(Config &config);
Bool condition(const VariableList &varList) const;
};
/***********************************************/
/***** Inlines *********************************/
/***********************************************/
inline ConditionStringMatchPattern::ConditionStringMatchPattern(Config &config)
{
try
{
readConfig(config, "string", text, Config::OPTIONAL, "", "should contain a {variable}");
readConfig(config, "pattern", pattern, Config::OPTIONAL, "", "");
readConfig(config, "isRegularExpression", isRegularExpression, Config::DEFAULT, "0", "pattern is a regular expression");
readConfig(config, "caseSensitive", caseSensitive, Config::DEFAULT, "1", "treat lower and upper case as distinct");
if(isCreateSchema(config)) return;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
inline Bool ConditionStringMatchPattern::condition(const VariableList &varList) const
{
try
{
std::string t = text(varList).str();
std::string p = pattern(varList).str();
if(!caseSensitive)
{
t = String::lowerCase(t);
p = String::lowerCase(p);
}
if(isRegularExpression)
return std::regex_match(t, std::regex(p));
else
return (t == p);
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
#endif
|