diff --git a/src/macro-core/macro-condition-file.cpp b/src/macro-core/macro-condition-file.cpp index 80af066a..50bc4ebb 100644 --- a/src/macro-core/macro-condition-file.cpp +++ b/src/macro-core/macro-condition-file.cpp @@ -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); diff --git a/src/macro-core/macro-condition-tempvar.cpp b/src/macro-core/macro-condition-tempvar.cpp index 66670285..b3d6c200 100644 --- a/src/macro-core/macro-condition-tempvar.cpp +++ b/src/macro-core/macro-condition-tempvar.cpp @@ -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; diff --git a/src/macro-core/macro-condition-variable.cpp b/src/macro-core/macro-condition-variable.cpp index 9548afad..25c8d885 100644 --- a/src/macro-core/macro-condition-variable.cpp +++ b/src/macro-core/macro-condition-variable.cpp @@ -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(); } diff --git a/src/macro-core/macro-condition-websocket.cpp b/src/macro-core/macro-condition-websocket.cpp index 673cedce..711e1a10 100644 --- a/src/macro-core/macro-condition-websocket.cpp +++ b/src/macro-core/macro-condition-websocket.cpp @@ -20,17 +20,6 @@ const static std::map "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 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; } diff --git a/src/macro-core/macro-condition-window.cpp b/src/macro-core/macro-condition-window.cpp index c67e23da..9bdacdc4 100644 --- a/src/macro-core/macro-condition-window.cpp +++ b/src/macro-core/macro-condition-window.cpp @@ -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 ®ex) { 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; diff --git a/src/macro-external/twitch/macro-condition-twitch.cpp b/src/macro-external/twitch/macro-condition-twitch.cpp index 426c0fe4..416db9a8 100644 --- a/src/macro-external/twitch/macro-condition-twitch.cpp +++ b/src/macro-external/twitch/macro-condition-twitch.cpp @@ -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 ®ex, 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) diff --git a/src/macro-external/video/macro-condition-video.cpp b/src/macro-external/video/macro-condition-video.cpp index 1049f6ac..b862177d 100644 --- a/src/macro-external/video/macro-condition-video.cpp +++ b/src/macro-external/video/macro-condition-video.cpp @@ -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() diff --git a/src/utils/regex-config.cpp b/src/utils/regex-config.cpp index 35f28636..bc90eaa2 100644 --- a/src/utils/regex-config.cpp +++ b/src/utils/regex-config.cpp @@ -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; diff --git a/src/utils/regex-config.hpp b/src/utils/regex-config.hpp index e6a0701a..5c97fc9a 100644 --- a/src/utils/regex-config.hpp +++ b/src/utils/regex-config.hpp @@ -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(); diff --git a/src/utils/scene-item-selection.cpp b/src/utils/scene-item-selection.cpp index 01b215ef..d5de4723 100644 --- a/src/utils/scene-item-selection.cpp +++ b/src/utils/scene-item-selection.cpp @@ -62,27 +62,16 @@ static std::vector getSceneItemsWithName(obs_scene_t *scene, struct NamePatternMatchData { std::string pattern; - const RegexConfig &conf; + const RegexConfig ®ex; std::vector 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(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); } diff --git a/src/utils/utility.cpp b/src/utils/utility.cpp index ac664248..e2874293 100644 --- a/src/utils/utility.cpp +++ b/src/utils/utility.cpp @@ -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; }