Use xs_regex_match() where applicable.

This commit is contained in:
default 2024-05-15 13:27:23 +02:00
parent d531649776
commit ff8d49a899
4 changed files with 29 additions and 23 deletions

14
data.c
View File

@ -2293,13 +2293,9 @@ int content_check(const char *file, const xs_dict *msg)
while (!r && !feof(f)) { while (!r && !feof(f)) {
xs *rx = xs_strip_i(xs_readline(f)); xs *rx = xs_strip_i(xs_readline(f));
if (*rx) { if (*rx && xs_regex_match(c, rx)) {
xs *l = xs_regex_select_n(c, rx, 1); srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
r = 1;
if (xs_list_len(l)) {
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); c = xs_tolower_i(c);
/* apply regex */ /* apply regex */
xs *l = xs_regex_select_n(c, i_regex, 1); if (xs_regex_match(c, i_regex)) {
if (xs_list_len(l)) {
if (xs_set_add(&seen, md5) == 1) if (xs_set_add(&seen, md5) == 1)
show--; show--;
} }

2
xs.h
View File

@ -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) xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value)
/* prepends a memory block to the dict */ /* 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));
} }

View File

@ -4,6 +4,7 @@
#define _XS_REGEX_H #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); 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) #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); 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 <regex.h> #include <regex.h>
xs_list *xs_regex_split_n(const char *str, const char *rx, int count) 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; regex_t re;
regmatch_t rm; regmatch_t rm;
int offset = 0; int offset = 0;
xs_list *list = NULL; xs_list *list = xs_list_new();
const char *p; const char *p;
if (regcomp(&re, rx, REG_EXTENDED)) if (regcomp(&re, rx, REG_EXTENDED))
return NULL; return list;
list = xs_list_new();
while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) { while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
/* add first the leading part of the string */ /* 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_list *list = xs_list_new();
xs *split = NULL; xs *split = NULL;
xs_list *p;
xs_val *v; xs_val *v;
int n = 0; int n = 0;
int c = 0;
/* split */ /* split */
split = xs_regex_split_n(str, rx, count); split = xs_regex_split_n(str, rx, count);
/* now iterate to get only the 'separators' (odd ones) */ /* now iterate to get only the 'separators' (odd ones) */
p = split; while (xs_list_next(split, &v, &c)) {
while (xs_list_iter(&p, &v)) {
if (n & 0x1) if (n & 0x1)
list = xs_list_append(list, v); 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_str *s = xs_str_new(NULL);
xs *split = xs_regex_split_n(str, rx, count); xs *split = xs_regex_split_n(str, rx, count);
xs_list *p;
xs_val *v; xs_val *v;
int n = 0; int n = 0;
int c = 0;
int pholder = !!strchr(rep, '&'); int pholder = !!strchr(rep, '&');
p = split; while (xs_list_next(split, &v, &c)) {
while (xs_list_iter(&p, &v)) {
if (n & 0x1) { if (n & 0x1) {
if (pholder) { if (pholder) {
/* rep has a placeholder; process char by char */ /* 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; 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_IMPLEMENTATION */
#endif /* XS_REGEX_H */ #endif /* XS_REGEX_H */

View File

@ -1 +1 @@
/* f3818ad611f09313008a2102a5e543c232e1d824 2024-05-02T23:45:38+02:00 */ /* 6e75e8736f7f1b6ea6c6774d4bd922b3ad56b771 2024-05-15T11:42:19+02:00 */