core: Move regex from stdlib to re2 (#8736)

Moves the regex handling from stdlib to re2
This commit is contained in:
Vaxry 2024-12-16 19:21:44 +01:00 committed by GitHub
parent dab50b3ef3
commit e06b520427
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 41 additions and 63 deletions

View file

@ -60,7 +60,8 @@ runs:
xcb-util \ xcb-util \
xcb-util-image \ xcb-util-image \
libzip \ libzip \
librsvg librsvg \
re2
- name: Get hyprwayland-scanner-git - name: Get hyprwayland-scanner-git
shell: bash shell: bash

View file

@ -101,8 +101,6 @@ else()
endif() endif()
find_package(OpenGL REQUIRED COMPONENTS ${GLES_VERSION}) 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(aquamarine_dep REQUIRED IMPORTED_TARGET aquamarine>=0.4.5)
pkg_check_modules(hyprlang_dep REQUIRED IMPORTED_TARGET hyprlang>=0.3.2) pkg_check_modules(hyprlang_dep REQUIRED IMPORTED_TARGET hyprlang>=0.3.2)
pkg_check_modules(hyprcursor_dep REQUIRED IMPORTED_TARGET hyprcursor>=0.1.7) pkg_check_modules(hyprcursor_dep REQUIRED IMPORTED_TARGET hyprcursor>=0.1.7)
@ -131,7 +129,8 @@ pkg_check_modules(
libdrm libdrm
libinput libinput
gbm gbm
gio-2.0) gio-2.0
re2)
find_package(hyprwayland-scanner 0.3.10 REQUIRED) find_package(hyprwayland-scanner 0.3.10 REQUIRED)

View file

@ -5,7 +5,7 @@ project(
DESCRIPTION "Control utility for Hyprland" 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") add_executable(hyprctl "main.cpp")

View file

@ -1,3 +1,5 @@
#include <re2/re2.h>
#include <cctype> #include <cctype>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -23,7 +25,6 @@
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
#include <cstdarg> #include <cstdarg>
#include <regex>
#include <sys/socket.h> #include <sys/socket.h>
#include <hyprutils/string/String.hpp> #include <hyprutils/string/String.hpp>
#include <cstring> #include <cstring>
@ -281,11 +282,10 @@ int requestHyprpaper(std::string arg) {
} }
void batchRequest(std::string arg, bool json) { 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) { if (json)
commands = "j/" + std::regex_replace(commands, std::regex(";\\s*"), ";j/"); RE2::GlobalReplace(&commands, ";\\s*", ";j/");
}
std::string rq = "[[BATCH]]" + commands; std::string rq = "[[BATCH]]" + commands;
request(rq); request(rq);

View file

@ -1,3 +1,5 @@
#include <re2/re2.h>
#include "Compositor.hpp" #include "Compositor.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "helpers/Splashes.hpp" #include "helpers/Splashes.hpp"
@ -2392,19 +2394,19 @@ PHLWINDOW CCompositor::getWindowByRegex(const std::string& regexp_) {
eFocusWindowMode mode = MODE_CLASS_REGEX; eFocusWindowMode mode = MODE_CLASS_REGEX;
std::regex regexCheck(regexp_); std::string regexCheck;
std::string matchCheck; std::string matchCheck;
if (regexp.starts_with("class:")) { if (regexp.starts_with("class:")) {
regexCheck = std::regex(regexp.substr(6)); regexCheck = regexp.substr(6);
} else if (regexp.starts_with("initialclass:")) { } else if (regexp.starts_with("initialclass:")) {
mode = MODE_INITIAL_CLASS_REGEX; mode = MODE_INITIAL_CLASS_REGEX;
regexCheck = std::regex(regexp.substr(13)); regexCheck = regexp.substr(13);
} else if (regexp.starts_with("title:")) { } else if (regexp.starts_with("title:")) {
mode = MODE_TITLE_REGEX; mode = MODE_TITLE_REGEX;
regexCheck = std::regex(regexp.substr(6)); regexCheck = regexp.substr(6);
} else if (regexp.starts_with("initialtitle:")) { } else if (regexp.starts_with("initialtitle:")) {
mode = MODE_INITIAL_TITLE_REGEX; mode = MODE_INITIAL_TITLE_REGEX;
regexCheck = std::regex(regexp.substr(13)); regexCheck = regexp.substr(13);
} else if (regexp.starts_with("address:")) { } else if (regexp.starts_with("address:")) {
mode = MODE_ADDRESS; mode = MODE_ADDRESS;
matchCheck = regexp.substr(8); matchCheck = regexp.substr(8);
@ -2420,25 +2422,25 @@ PHLWINDOW CCompositor::getWindowByRegex(const std::string& regexp_) {
switch (mode) { switch (mode) {
case MODE_CLASS_REGEX: { case MODE_CLASS_REGEX: {
const auto windowClass = w->m_szClass; const auto windowClass = w->m_szClass;
if (!std::regex_search(windowClass, regexCheck)) if (!RE2::FullMatch(windowClass, regexCheck))
continue; continue;
break; break;
} }
case MODE_INITIAL_CLASS_REGEX: { case MODE_INITIAL_CLASS_REGEX: {
const auto initialWindowClass = w->m_szInitialClass; const auto initialWindowClass = w->m_szInitialClass;
if (!std::regex_search(initialWindowClass, regexCheck)) if (!RE2::FullMatch(initialWindowClass, regexCheck))
continue; continue;
break; break;
} }
case MODE_TITLE_REGEX: { case MODE_TITLE_REGEX: {
const auto windowTitle = w->m_szTitle; const auto windowTitle = w->m_szTitle;
if (!std::regex_search(windowTitle, regexCheck)) if (!RE2::FullMatch(windowTitle, regexCheck))
continue; continue;
break; break;
} }
case MODE_INITIAL_TITLE_REGEX: { case MODE_INITIAL_TITLE_REGEX: {
const auto initialWindowTitle = w->m_szInitialTitle; const auto initialWindowTitle = w->m_szInitialTitle;
if (!std::regex_search(initialWindowTitle, regexCheck)) if (!RE2::FullMatch(initialWindowTitle, regexCheck))
continue; continue;
break; break;
} }

View file

@ -1,3 +1,5 @@
#include <re2/re2.h>
#include "ConfigManager.hpp" #include "ConfigManager.hpp"
#include "../managers/KeybindManager.hpp" #include "../managers/KeybindManager.hpp"
#include "../Compositor.hpp" #include "../Compositor.hpp"
@ -1256,17 +1258,12 @@ std::vector<SP<CWindowRule>> CConfigManager::getMatchingRules(PHLWINDOW pWindow,
if (rule->szValue.starts_with("tag:") && !tags.isTagged(rule->szValue.substr(4))) if (rule->szValue.starts_with("tag:") && !tags.isTagged(rule->szValue.substr(4)))
continue; continue;
if (rule->szValue.starts_with("title:")) { if (rule->szValue.starts_with("title:") && !RE2::FullMatch(pWindow->m_szTitle, rule->szValue.substr(6)))
std::regex RULECHECK(rule->szValue.substr(6)); continue;
if (!std::regex_search(pWindow->m_szTitle, RULECHECK)) if (!RE2::FullMatch(pWindow->m_szClass, rule->szValue))
continue; continue;
} else {
std::regex classCheck(rule->szValue);
if (!std::regex_search(pWindow->m_szClass, classCheck))
continue;
}
} catch (...) { } catch (...) {
Debug::log(ERR, "Regex error at {}", rule->szValue); Debug::log(ERR, "Regex error at {}", rule->szValue);
continue; continue;
@ -1354,33 +1351,18 @@ std::vector<SP<CWindowRule>> CConfigManager::getMatchingRules(PHLWINDOW pWindow,
if (!rule->szTag.empty() && !tags.isTagged(rule->szTag)) if (!rule->szTag.empty() && !tags.isTagged(rule->szTag))
continue; continue;
if (!rule->szClass.empty()) { if (!rule->szClass.empty() && !RE2::FullMatch(pWindow->m_szClass, rule->szClass))
std::regex RULECHECK(rule->szClass); continue;
if (!std::regex_search(pWindow->m_szClass, RULECHECK)) if (!rule->szTitle.empty() && !RE2::FullMatch(pWindow->m_szTitle, rule->szTitle))
continue; continue;
}
if (!rule->szTitle.empty()) { if (!rule->szInitialTitle.empty() && !RE2::FullMatch(pWindow->m_szInitialTitle, rule->szInitialTitle))
std::regex RULECHECK(rule->szTitle); continue;
if (!std::regex_search(pWindow->m_szTitle, RULECHECK)) if (!rule->szInitialClass.empty() && !RE2::FullMatch(pWindow->m_szInitialClass, rule->szInitialClass))
continue; 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) { } catch (std::exception& e) {
Debug::log(ERR, "Regex error at {} ({})", rule->szValue, e.what()); Debug::log(ERR, "Regex error at {} ({})", rule->szValue, e.what());
continue; continue;
@ -1437,12 +1419,8 @@ std::vector<SP<CLayerRule>> CConfigManager::getMatchingRules(PHLLS pLS) {
if (lr->targetNamespace.starts_with("address:0x")) { if (lr->targetNamespace.starts_with("address:0x")) {
if (std::format("address:0x{:x}", (uintptr_t)pLS.get()) != lr->targetNamespace) if (std::format("address:0x{:x}", (uintptr_t)pLS.get()) != lr->targetNamespace)
continue; continue;
} else { } else if (!RE2::FullMatch(pLS->layerSurface->layerNamespace, lr->targetNamespace))
std::regex NSCHECK(lr->targetNamespace); continue;
if (!std::regex_search(pLS->layerSurface->layerNamespace, NSCHECK))
continue;
}
// hit // hit
returns.emplace_back(lr); returns.emplace_back(lr);

View file

@ -9,7 +9,6 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <regex>
#include <optional> #include <optional>
#include <functional> #include <functional>
#include <xf86drmMode.h> #include <xf86drmMode.h>

View file

@ -1,3 +1,5 @@
#include <re2/re2.h>
#include <any> #include <any>
#include <bit> #include <bit>
#include <string_view> #include <string_view>
@ -1601,13 +1603,13 @@ PHLWINDOW CWindow::getSwallower() {
} }
if (!(*PSWALLOWREGEX).empty()) 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) if (candidates.size() <= 0)
return nullptr; return nullptr;
if (!(*PSWALLOWEXREGEX).empty()) 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) if (candidates.size() <= 0)
return nullptr; return nullptr;

View file

@ -1,5 +1,3 @@
#include "Compositor.hpp"
#include <algorithm> #include <algorithm>
#include <any> #include <any>
#include <array> #include <array>
@ -18,7 +16,6 @@
#include <optional> #include <optional>
#include <random> #include <random>
#include <ranges> #include <ranges>
#include <regex>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <string> #include <string>