feat: use regex to match no strstr
This commit is contained in:
@@ -7,6 +7,12 @@
|
||||
|
||||
#include "util.h"
|
||||
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#include <pcre2.h>
|
||||
#include <wchar.h>
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
34
src/maomao.c
34
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);
|
||||
|
||||
Reference in New Issue
Block a user