From ff8d49a8991977e07cf6bb50156b2ffda08d0c0e Mon Sep 17 00:00:00 2001 From: default Date: Wed, 15 May 2024 13:27:23 +0200 Subject: [PATCH] Use xs_regex_match() where applicable. --- data.c | 14 ++++---------- xs.h | 2 +- xs_regex.h | 34 +++++++++++++++++++++++----------- xs_version.h | 2 +- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/data.c b/data.c index 2410f5b..65100de 100644 --- a/data.c +++ b/data.c @@ -2293,13 +2293,9 @@ int content_check(const char *file, const xs_dict *msg) while (!r && !feof(f)) { xs *rx = xs_strip_i(xs_readline(f)); - if (*rx) { - xs *l = xs_regex_select_n(c, rx, 1); - - if (xs_list_len(l)) { - srv_debug(1, xs_fmt("content_check: match for '%s'", rx)); - r = 1; - } + if (*rx && xs_regex_match(c, rx)) { + srv_debug(1, xs_fmt("content_check: match for '%s'", rx)); + r = 1; } } @@ -2576,9 +2572,7 @@ xs_list *content_search(snac *user, const char *regex, c = xs_tolower_i(c); /* apply regex */ - xs *l = xs_regex_select_n(c, i_regex, 1); - - if (xs_list_len(l)) { + if (xs_regex_match(c, i_regex)) { if (xs_set_add(&seen, md5) == 1) show--; } diff --git a/xs.h b/xs.h index ad72207..f5c87ef 100644 --- a/xs.h +++ b/xs.h @@ -1049,7 +1049,7 @@ xs_dict *xs_dict_append(xs_dict *dict, const xs_str *key, const xs_val *value) xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value) /* prepends a memory block to the dict */ { - return _xs_dict_write_ditem(dict, 4, key, value, xs_size(value)); + return _xs_dict_write_ditem(dict, 1 + _XS_TYPE_SIZE, key, value, xs_size(value)); } diff --git a/xs_regex.h b/xs_regex.h index 1adbcf8..cb73a01 100644 --- a/xs_regex.h +++ b/xs_regex.h @@ -4,6 +4,7 @@ #define _XS_REGEX_H +int xs_regex_match(const char *str, const char *rx); xs_list *xs_regex_split_n(const char *str, const char *rx, int count); #define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL) xs_list *xs_regex_select_n(const char *str, const char *rx, int count); @@ -18,18 +19,21 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c #include xs_list *xs_regex_split_n(const char *str, const char *rx, int count) -/* splits str by regex */ +/* splits str using regex as a separator, at most count times. + Always returns a list: + len == 0: regcomp error + len == 1: full string (no matches) + len == odd: first part [ separator / next part ]... +*/ { regex_t re; regmatch_t rm; int offset = 0; - xs_list *list = NULL; + xs_list *list = xs_list_new(); const char *p; if (regcomp(&re, rx, REG_EXTENDED)) - return NULL; - - list = xs_list_new(); + return list; while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) { /* add first the leading part of the string */ @@ -60,16 +64,15 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count) { xs_list *list = xs_list_new(); xs *split = NULL; - xs_list *p; xs_val *v; int n = 0; + int c = 0; /* split */ split = xs_regex_split_n(str, rx, count); /* now iterate to get only the 'separators' (odd ones) */ - p = split; - while (xs_list_iter(&p, &v)) { + while (xs_list_next(split, &v, &c)) { if (n & 0x1) list = xs_list_append(list, v); @@ -86,13 +89,12 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c { xs_str *s = xs_str_new(NULL); xs *split = xs_regex_split_n(str, rx, count); - xs_list *p; xs_val *v; int n = 0; + int c = 0; int pholder = !!strchr(rep, '&'); - p = split; - while (xs_list_iter(&p, &v)) { + while (xs_list_next(split, &v, &c)) { if (n & 0x1) { if (pholder) { /* rep has a placeholder; process char by char */ @@ -128,6 +130,16 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c return s; } + +int xs_regex_match(const char *str, const char *rx) +/* returns if str matches the regex at least once */ +{ + xs *l = xs_regex_select_n(str, rx, 1); + + return xs_list_len(l) == 1; +} + + #endif /* XS_IMPLEMENTATION */ #endif /* XS_REGEX_H */ diff --git a/xs_version.h b/xs_version.h index a672ef4..16faf2b 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* f3818ad611f09313008a2102a5e543c232e1d824 2024-05-02T23:45:38+02:00 */ +/* 6e75e8736f7f1b6ea6c6774d4bd922b3ad56b771 2024-05-15T11:42:19+02:00 */