From e06b520427a30f1c86562ad7cc3794bf03b40188 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:21:44 +0100 Subject: [PATCH] core: Move regex from stdlib to re2 (#8736) Moves the regex handling from stdlib to re2 --- .github/actions/setup_base/action.yml | 3 +- CMakeLists.txt | 5 +-- hyprctl/CMakeLists.txt | 2 +- hyprctl/main.cpp | 10 ++--- src/Compositor.cpp | 20 +++++----- src/config/ConfigManager.cpp | 54 ++++++++------------------- src/config/ConfigManager.hpp | 1 - src/desktop/Window.cpp | 6 ++- src/pch/pch.hpp | 3 -- 9 files changed, 41 insertions(+), 63 deletions(-) diff --git a/.github/actions/setup_base/action.yml b/.github/actions/setup_base/action.yml index e22f514ee..c510bb74f 100644 --- a/.github/actions/setup_base/action.yml +++ b/.github/actions/setup_base/action.yml @@ -60,7 +60,8 @@ runs: xcb-util \ xcb-util-image \ libzip \ - librsvg + librsvg \ + re2 - name: Get hyprwayland-scanner-git shell: bash diff --git a/CMakeLists.txt b/CMakeLists.txt index aa0d4b030..3441039e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,8 +101,6 @@ else() endif() find_package(OpenGL REQUIRED COMPONENTS ${GLES_VERSION}) -pkg_check_modules(hyprctl_deps REQUIRED IMPORTED_TARGET hyprutils>=0.2.4) - pkg_check_modules(aquamarine_dep REQUIRED IMPORTED_TARGET aquamarine>=0.4.5) pkg_check_modules(hyprlang_dep REQUIRED IMPORTED_TARGET hyprlang>=0.3.2) pkg_check_modules(hyprcursor_dep REQUIRED IMPORTED_TARGET hyprcursor>=0.1.7) @@ -131,7 +129,8 @@ pkg_check_modules( libdrm libinput gbm - gio-2.0) + gio-2.0 + re2) find_package(hyprwayland-scanner 0.3.10 REQUIRED) diff --git a/hyprctl/CMakeLists.txt b/hyprctl/CMakeLists.txt index aaffe411a..db5ef6157 100644 --- a/hyprctl/CMakeLists.txt +++ b/hyprctl/CMakeLists.txt @@ -5,7 +5,7 @@ project( DESCRIPTION "Control utility for Hyprland" ) -pkg_check_modules(deps REQUIRED IMPORTED_TARGET hyprutils>=0.1.1) +pkg_check_modules(hyprctl_deps REQUIRED IMPORTED_TARGET hyprutils>=0.2.4 re2) add_executable(hyprctl "main.cpp") diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 55c3cc7ec..676c8b982 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -23,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -281,11 +282,10 @@ int requestHyprpaper(std::string arg) { } void batchRequest(std::string arg, bool json) { - std::string commands = arg.substr(arg.find_first_of(" ") + 1); + std::string commands = arg.substr(arg.find_first_of(' ') + 1); - if (json) { - commands = "j/" + std::regex_replace(commands, std::regex(";\\s*"), ";j/"); - } + if (json) + RE2::GlobalReplace(&commands, ";\\s*", ";j/"); std::string rq = "[[BATCH]]" + commands; request(rq); diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 4276a74e6..ecf251f3b 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1,3 +1,5 @@ +#include + #include "Compositor.hpp" #include "debug/Log.hpp" #include "helpers/Splashes.hpp" @@ -2392,19 +2394,19 @@ PHLWINDOW CCompositor::getWindowByRegex(const std::string& regexp_) { eFocusWindowMode mode = MODE_CLASS_REGEX; - std::regex regexCheck(regexp_); + std::string regexCheck; std::string matchCheck; if (regexp.starts_with("class:")) { - regexCheck = std::regex(regexp.substr(6)); + regexCheck = regexp.substr(6); } else if (regexp.starts_with("initialclass:")) { mode = MODE_INITIAL_CLASS_REGEX; - regexCheck = std::regex(regexp.substr(13)); + regexCheck = regexp.substr(13); } else if (regexp.starts_with("title:")) { mode = MODE_TITLE_REGEX; - regexCheck = std::regex(regexp.substr(6)); + regexCheck = regexp.substr(6); } else if (regexp.starts_with("initialtitle:")) { mode = MODE_INITIAL_TITLE_REGEX; - regexCheck = std::regex(regexp.substr(13)); + regexCheck = regexp.substr(13); } else if (regexp.starts_with("address:")) { mode = MODE_ADDRESS; matchCheck = regexp.substr(8); @@ -2420,25 +2422,25 @@ PHLWINDOW CCompositor::getWindowByRegex(const std::string& regexp_) { switch (mode) { case MODE_CLASS_REGEX: { const auto windowClass = w->m_szClass; - if (!std::regex_search(windowClass, regexCheck)) + if (!RE2::FullMatch(windowClass, regexCheck)) continue; break; } case MODE_INITIAL_CLASS_REGEX: { const auto initialWindowClass = w->m_szInitialClass; - if (!std::regex_search(initialWindowClass, regexCheck)) + if (!RE2::FullMatch(initialWindowClass, regexCheck)) continue; break; } case MODE_TITLE_REGEX: { const auto windowTitle = w->m_szTitle; - if (!std::regex_search(windowTitle, regexCheck)) + if (!RE2::FullMatch(windowTitle, regexCheck)) continue; break; } case MODE_INITIAL_TITLE_REGEX: { const auto initialWindowTitle = w->m_szInitialTitle; - if (!std::regex_search(initialWindowTitle, regexCheck)) + if (!RE2::FullMatch(initialWindowTitle, regexCheck)) continue; break; } diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index bcbfe7e74..f77a6b42a 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -1,3 +1,5 @@ +#include + #include "ConfigManager.hpp" #include "../managers/KeybindManager.hpp" #include "../Compositor.hpp" @@ -1256,17 +1258,12 @@ std::vector> CConfigManager::getMatchingRules(PHLWINDOW pWindow, if (rule->szValue.starts_with("tag:") && !tags.isTagged(rule->szValue.substr(4))) continue; - if (rule->szValue.starts_with("title:")) { - std::regex RULECHECK(rule->szValue.substr(6)); + if (rule->szValue.starts_with("title:") && !RE2::FullMatch(pWindow->m_szTitle, rule->szValue.substr(6))) + continue; - if (!std::regex_search(pWindow->m_szTitle, RULECHECK)) - continue; - } else { - std::regex classCheck(rule->szValue); + if (!RE2::FullMatch(pWindow->m_szClass, rule->szValue)) + continue; - if (!std::regex_search(pWindow->m_szClass, classCheck)) - continue; - } } catch (...) { Debug::log(ERR, "Regex error at {}", rule->szValue); continue; @@ -1354,33 +1351,18 @@ std::vector> CConfigManager::getMatchingRules(PHLWINDOW pWindow, if (!rule->szTag.empty() && !tags.isTagged(rule->szTag)) continue; - if (!rule->szClass.empty()) { - std::regex RULECHECK(rule->szClass); + if (!rule->szClass.empty() && !RE2::FullMatch(pWindow->m_szClass, rule->szClass)) + continue; - if (!std::regex_search(pWindow->m_szClass, RULECHECK)) - continue; - } + if (!rule->szTitle.empty() && !RE2::FullMatch(pWindow->m_szTitle, rule->szTitle)) + continue; - if (!rule->szTitle.empty()) { - std::regex RULECHECK(rule->szTitle); + if (!rule->szInitialTitle.empty() && !RE2::FullMatch(pWindow->m_szInitialTitle, rule->szInitialTitle)) + continue; - if (!std::regex_search(pWindow->m_szTitle, RULECHECK)) - continue; - } + if (!rule->szInitialClass.empty() && !RE2::FullMatch(pWindow->m_szInitialClass, rule->szInitialClass)) + continue; - if (!rule->szInitialTitle.empty()) { - std::regex RULECHECK(rule->szInitialTitle); - - if (!std::regex_search(pWindow->m_szInitialTitle, RULECHECK)) - continue; - } - - if (!rule->szInitialClass.empty()) { - std::regex RULECHECK(rule->szInitialClass); - - if (!std::regex_search(pWindow->m_szInitialClass, RULECHECK)) - continue; - } } catch (std::exception& e) { Debug::log(ERR, "Regex error at {} ({})", rule->szValue, e.what()); continue; @@ -1437,12 +1419,8 @@ std::vector> CConfigManager::getMatchingRules(PHLLS pLS) { if (lr->targetNamespace.starts_with("address:0x")) { if (std::format("address:0x{:x}", (uintptr_t)pLS.get()) != lr->targetNamespace) continue; - } else { - std::regex NSCHECK(lr->targetNamespace); - - if (!std::regex_search(pLS->layerSurface->layerNamespace, NSCHECK)) - continue; - } + } else if (!RE2::FullMatch(pLS->layerSurface->layerNamespace, lr->targetNamespace)) + continue; // hit returns.emplace_back(lr); diff --git a/src/config/ConfigManager.hpp b/src/config/ConfigManager.hpp index 1eca5ac3e..7ce7d495e 100644 --- a/src/config/ConfigManager.hpp +++ b/src/config/ConfigManager.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/src/desktop/Window.cpp b/src/desktop/Window.cpp index 9d4b597bd..2858e9317 100644 --- a/src/desktop/Window.cpp +++ b/src/desktop/Window.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -1601,13 +1603,13 @@ PHLWINDOW CWindow::getSwallower() { } if (!(*PSWALLOWREGEX).empty()) - std::erase_if(candidates, [&](const auto& other) { return !std::regex_match(other->m_szClass, std::regex(*PSWALLOWREGEX)); }); + std::erase_if(candidates, [&](const auto& other) { return !RE2::FullMatch(other->m_szClass, *PSWALLOWREGEX); }); if (candidates.size() <= 0) return nullptr; if (!(*PSWALLOWEXREGEX).empty()) - std::erase_if(candidates, [&](const auto& other) { return std::regex_match(other->m_szTitle, std::regex(*PSWALLOWEXREGEX)); }); + std::erase_if(candidates, [&](const auto& other) { return RE2::FullMatch(other->m_szTitle, *PSWALLOWEXREGEX); }); if (candidates.size() <= 0) return nullptr; diff --git a/src/pch/pch.hpp b/src/pch/pch.hpp index eaec129bf..8dc98face 100644 --- a/src/pch/pch.hpp +++ b/src/pch/pch.hpp @@ -1,5 +1,3 @@ -#include "Compositor.hpp" - #include #include #include @@ -18,7 +16,6 @@ #include #include #include -#include #include #include #include