From 64b15fe4e7b8c5918d30f08d3cd043681240bec8 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Sun, 18 May 2025 18:26:06 +0800 Subject: [PATCH] feat: use regex to match no strstr --- README.md | 2 +- meson.build | 2 ++ src/common/util.c | 37 +++++++++++++++++++++++++++++++++++++ src/common/util.h | 2 ++ src/maomao.c | 34 +++++++++++++++++----------------- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2f6a4a4..1d3f28d 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ https://github.com/user-attachments/assets/c9bf9415-fad1-4400-bcdc-3ad2d76de85a ## depend ```bash -yay -S glibc wayland libinput libdrm pixman libxkbcommon git meson ninja wayland-protocols libdisplay-info libliftoff hwdata seatd +yay -S glibc wayland libinput libdrm pixman libxkbcommon git meson ninja wayland-protocols libdisplay-info libliftoff hwdata seatd pcre2 ``` ## arch diff --git a/meson.build b/meson.build index 791eda9..8e312fe 100644 --- a/meson.build +++ b/meson.build @@ -37,6 +37,7 @@ wlroots_dep = dependency('wlroots-0.19') xkbcommon_dep = dependency('xkbcommon') libinput_dep = dependency('libinput') libwayland_client_dep = dependency('wayland-client') +pcre2_dep = dependency('libpcre2-8') # 获取 Git Commit Hash 和最新的 tag git = find_program('git', required : false) @@ -79,6 +80,7 @@ executable('maomao', xkbcommon_dep, libinput_dep, libwayland_client_dep, + pcre2_dep, ], install : true, c_args : c_args diff --git a/src/common/util.c b/src/common/util.c index 2f1195c..24359f3 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -7,6 +7,12 @@ #include "util.h" +#define PCRE2_CODE_UNIT_WIDTH 8 +#include +#include + + + void die(const char *fmt, ...) { va_list ap; @@ -45,3 +51,34 @@ int fd_set_nonblock(int fd) { return 0; } + +int regex_match(const char *pattern, const char *str) { + int errnum; + PCRE2_SIZE erroffset; + pcre2_code *re = pcre2_compile( + (PCRE2_SPTR)pattern, + PCRE2_ZERO_TERMINATED, + PCRE2_UTF, // 启用 UTF-8 支持 + &errnum, &erroffset, NULL + ); + if (!re) { + PCRE2_UCHAR errbuf[256]; + pcre2_get_error_message(errnum, errbuf, sizeof(errbuf)); + fprintf(stderr, "PCRE2 error: %s at offset %zu\n", errbuf, erroffset); + return 0; + } + + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL); + int ret = pcre2_match( + re, + (PCRE2_SPTR)str, + strlen(str), + 0, 0, + match_data, + NULL + ); + + pcre2_match_data_free(match_data); + pcre2_code_free(re); + return ret >= 0; +} diff --git a/src/common/util.h b/src/common/util.h index 226980d..ff6943e 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -1,5 +1,7 @@ /* See LICENSE.dwm file for copyright and license details. */ + void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); int fd_set_nonblock(int fd); +int regex_match(const char *pattern_mb, const char *str_mb); diff --git a/src/maomao.c b/src/maomao.c index 88fb124..6c2f969 100644 --- a/src/maomao.c +++ b/src/maomao.c @@ -1579,10 +1579,10 @@ Client *get_client_by_id_or_title(const char *arg_id, const char *arg_title) { if (arg_title && strncmp(arg_title, "none", 4) == 0) arg_title = NULL; - if ((arg_title && strstr(title, arg_title) && !arg_id) || - (arg_id && strstr(appid, arg_id) && !arg_title) || - (arg_id && strstr(appid, arg_id) && arg_title && - strstr(title, arg_title))) { + if ((arg_title && regex_match(arg_title, title) && !arg_id) || + (arg_id && regex_match(arg_id, appid) && !arg_title) || + (arg_id && regex_match(arg_id, appid) && arg_title && + regex_match(arg_title, title))) { target_client = c; break; } @@ -1774,10 +1774,10 @@ applyrulesgeom(Client *c) { if (config.window_rules_count < 1) break; r = &config.window_rules[ji]; - if ((r->title && strstr(title, r->title) && !r->id) || - (r->id && strstr(appid, r->id) && !r->title) || - (r->id && strstr(appid, r->id) && r->title && - strstr(title, r->title))) { + if ((regex_match(r->title,title) && !r->id) || + (r->id && regex_match(r->id,appid) && !r->title) || + (r->id && regex_match(r->id,appid) && r->title && + regex_match(r->title,title))) { c->geom.width = r->width > 0 ? r->width : c->geom.width; c->geom.height = r->height > 0 ? r->height : c->geom.height; // 重新计算居中的坐标 @@ -1814,10 +1814,10 @@ applyrules(Client *c) { break; r = &config.window_rules[ji]; - if ((r->title && strstr(title, r->title) && !r->id) || - (r->id && strstr(appid, r->id) && !r->title) || - (r->id && strstr(appid, r->id) && r->title && - strstr(title, r->title))) { + if ((r->title && regex_match(r->title,title) && !r->id) || + (r->id && regex_match(r->id,appid) && !r->title) || + (r->id && regex_match(r->id,appid) && r->title && + regex_match(r->title,title))) { c->isterm = r->isterm > 0 ? r->isterm : c->isterm; c->noswallow = r->noswallow > 0 ? r->noswallow : c->noswallow; c->scratchpad_geom.width = @@ -3244,7 +3244,7 @@ void createmon(struct wl_listener *listener, void *data) { break; r = &config.monitor_rules[ji]; - if (!r->name || strstr(wlr_output->name, r->name)) { + if (!r->name || regex_match(r->name, wlr_output->name)) { m->mfact = r->mfact; m->nmaster = r->nmaster; m->m.x = r->x; @@ -4125,10 +4125,10 @@ bool keypressglobal(struct wlr_surface *last_surface, appid = client_get_appid(c); title = client_get_title(c); - if ((r->title && strstr(title, r->title) && !r->id) || - (r->id && strstr(appid, r->id) && !r->title) || - (r->id && strstr(appid, r->id) && r->title && - strstr(title, r->title))) { + if ((r->title && regex_match(r->title, title) && !r->id) || + (r->id && regex_match(r->id, appid) && !r->title) || + (r->id && regex_match(r->id, appid) && r->title && + regex_match(r->title, title))) { reset = true; wlr_seat_keyboard_enter(seat, client_surface(c), keycodes, 0, &keyboard->modifiers);