Add Matches() helper function

This commit is contained in:
WarmUpTill 2023-12-13 17:03:49 +01:00 committed by WarmUpTill
parent 2defa06ba4
commit 2cc8a15118
11 changed files with 35 additions and 80 deletions

View File

@ -52,12 +52,7 @@ bool MacroConditionFile::MatchFileContent(QString &filedata)
}
if (_regex.Enabled()) {
auto expr = _regex.GetRegularExpression(_text);
if (!expr.isValid()) {
return false;
}
auto match = expr.match(filedata);
return match.hasMatch();
return _regex.Matches(filedata, QString::fromStdString(_text));
}
QString text = QString::fromStdString(_text);

View File

@ -68,12 +68,7 @@ bool MacroConditionTempVar::Compare(const TempVariable &var) const
}
if (_regex.Enabled()) {
auto expr = _regex.GetRegularExpression(_strValue);
if (!expr.isValid()) {
return false;
}
auto match = expr.match(QString::fromStdString(*value));
return match.hasMatch();
return _regex.Matches(*value, _strValue);
}
return std::string(_strValue) == *value;

View File

@ -52,14 +52,8 @@ static bool compareNumber(const Variable &var, double value, bool less)
bool MacroConditionVariable::Compare(const Variable &var) const
{
if (_regex.Enabled()) {
auto expr = _regex.GetRegularExpression(_strValue);
if (!expr.isValid()) {
return false;
}
auto match = expr.match(QString::fromStdString(var.Value()));
return match.hasMatch();
return _regex.Matches(var.Value(), _strValue);
}
return std::string(_strValue) == var.Value();
}

View File

@ -20,17 +20,6 @@ const static std::map<MacroConditionWebsocket::Type, std::string>
"AdvSceneSwitcher.condition.websocket.type.event"},
};
static bool matchRegex(const RegexConfig &conf, const std::string &msg,
const std::string &expr)
{
auto regex = conf.GetRegularExpression(expr);
if (!regex.isValid()) {
return false;
}
auto match = regex.match(QString::fromStdString(msg));
return match.hasMatch();
}
bool MacroConditionWebsocket::CheckCondition()
{
std::vector<std::string> messages;
@ -52,7 +41,7 @@ bool MacroConditionWebsocket::CheckCondition()
for (const auto &msg : messages) {
if (_regex.Enabled()) {
if (matchRegex(_regex, msg, _message)) {
if (_regex.Matches(msg, _message)) {
SetVariableValue(msg);
return true;
}

View File

@ -14,28 +14,17 @@ bool MacroConditionWindow::_registered = MacroConditionFactory::Register(
{MacroConditionWindow::Create, MacroConditionWindowEdit::Create,
"AdvSceneSwitcher.condition.window"});
static bool matchRegex(const RegexConfig &conf, const std::string &msg,
const std::string &expr)
{
auto regex = conf.GetRegularExpression(expr);
if (!regex.isValid()) {
return false;
}
auto match = regex.match(QString::fromStdString(msg));
return match.hasMatch();
}
static bool windowContainsText(const std::string &window,
const std::string &matchText,
const RegexConfig &conf)
const RegexConfig &regex)
{
auto text = GetTextInWindow(window);
if (!text.has_value()) {
return false;
}
if (conf.Enabled()) {
return matchRegex(conf, *text, matchText);
if (regex.Enabled()) {
return regex.Matches(*text, matchText);
}
return text == matchText;
}
@ -84,7 +73,7 @@ bool MacroConditionWindow::WindowRegexMatches(
// enabled in the backend and use the regular expression ".*".
for (const auto &window : windowList) {
if (matchRegex(_windowRegex, window, _window) &&
if (_windowRegex.Matches(window, _window) &&
WindowMatchesRequirements(window)) {
SetVariableValueBasedOnMatch(window);
return true;

View File

@ -339,19 +339,14 @@ bool MacroConditionTwitch::CheckChannelLiveEvents(TwitchToken &token)
return false;
}
static bool stringMatches(const RegexConfig &conf, const std::string &string,
static bool stringMatches(const RegexConfig &regex, const std::string &string,
const std::string &expr)
{
if (!conf.Enabled()) {
if (!regex.Enabled()) {
return string == expr;
}
auto regex = conf.GetRegularExpression(expr);
if (!regex.isValid()) {
return false;
}
auto match = regex.match(QString::fromStdString(string));
return match.hasMatch();
return regex.Matches(string, expr);
}
bool MacroConditionTwitch::CheckChatMessages(TwitchToken &token)

View File

@ -341,13 +341,7 @@ bool MacroConditionVideo::CheckOCR()
if (!_ocrParameters.regex.Enabled()) {
return text == std::string(_ocrParameters.text);
}
auto expr =
_ocrParameters.regex.GetRegularExpression(_ocrParameters.text);
if (!expr.isValid()) {
return false;
}
auto match = expr.match(QString::fromStdString(text));
return match.hasMatch();
return _ocrParameters.regex.Matches(text, _ocrParameters.text);
}
bool MacroConditionVideo::CheckColor()

View File

@ -51,6 +51,23 @@ RegexConfig::GetRegularExpression(const std::string &expr) const
return GetRegularExpression(QString::fromStdString(expr));
}
bool RegexConfig::Matches(const QString &text, const QString &expression) const
{
auto regex = GetRegularExpression(expression);
if (!regex.isValid()) {
return false;
}
auto match = regex.match(text);
return match.hasMatch();
}
bool RegexConfig::Matches(const std::string &text,
const std::string &expression) const
{
return Matches(QString::fromStdString(text),
QString::fromStdString(expression));
}
RegexConfig RegexConfig::PartialMatchRegexConfig()
{
RegexConfig conf;

View File

@ -29,6 +29,9 @@ public:
};
QRegularExpression GetRegularExpression(const QString &) const;
QRegularExpression GetRegularExpression(const std::string &) const;
bool Matches(const QString &text, const QString &expression) const;
bool Matches(const std::string &text,
const std::string &expression) const;
static RegexConfig PartialMatchRegexConfig();

View File

@ -62,27 +62,16 @@ static std::vector<OBSSceneItem> getSceneItemsWithName(obs_scene_t *scene,
struct NamePatternMatchData {
std::string pattern;
const RegexConfig &conf;
const RegexConfig &regex;
std::vector<OBSSceneItem> items = {};
};
static bool matchesRegex(const RegexConfig &conf, const std::string &msg,
const std::string &expr)
{
auto regex = conf.GetRegularExpression(expr);
if (!regex.isValid()) {
return false;
}
auto match = regex.match(QString::fromStdString(msg));
return match.hasMatch();
}
static bool getSceneItemsByPatternHelper(obs_scene_t *, obs_sceneitem_t *item,
void *ptr)
{
auto data = reinterpret_cast<NamePatternMatchData *>(ptr);
auto sourceName = obs_source_get_name(obs_sceneitem_get_source(item));
if (matchesRegex(data->conf, sourceName, data->pattern)) {
if (data->regex.Matches(sourceName, data->pattern)) {
data->items.emplace_back(item);
}

View File

@ -413,12 +413,7 @@ bool MatchJson(const std::string &json1, const std::string &json2,
}
if (regex.Enabled()) {
auto expr = regex.GetRegularExpression(j2);
if (!expr.isValid()) {
return false;
}
auto match = expr.match(QString::fromStdString(j1));
return match.hasMatch();
return regex.Matches(j1, j2);
}
return j1 == j2;
}