commit ff8d49a
default
·
2024-05-15 11:27:23 +0000 UTC
parent d531649
Use xs_regex_match() where applicable.
4 files changed,
+29,
-23
M
data.c
+4,
-10
1@@ -2293,13 +2293,9 @@ int content_check(const char *file, const xs_dict *msg)
2 while (!r && !feof(f)) {
3 xs *rx = xs_strip_i(xs_readline(f));
4
5- if (*rx) {
6- xs *l = xs_regex_select_n(c, rx, 1);
7-
8- if (xs_list_len(l)) {
9- srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
10- r = 1;
11- }
12+ if (*rx && xs_regex_match(c, rx)) {
13+ srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
14+ r = 1;
15 }
16 }
17
18@@ -2576,9 +2572,7 @@ xs_list *content_search(snac *user, const char *regex,
19 c = xs_tolower_i(c);
20
21 /* apply regex */
22- xs *l = xs_regex_select_n(c, i_regex, 1);
23-
24- if (xs_list_len(l)) {
25+ if (xs_regex_match(c, i_regex)) {
26 if (xs_set_add(&seen, md5) == 1)
27 show--;
28 }
M
xs.h
+1,
-1
1@@ -1049,7 +1049,7 @@ xs_dict *xs_dict_append(xs_dict *dict, const xs_str *key, const xs_val *value)
2 xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value)
3 /* prepends a memory block to the dict */
4 {
5- return _xs_dict_write_ditem(dict, 4, key, value, xs_size(value));
6+ return _xs_dict_write_ditem(dict, 1 + _XS_TYPE_SIZE, key, value, xs_size(value));
7 }
8
9
+23,
-11
1@@ -4,6 +4,7 @@
2
3 #define _XS_REGEX_H
4
5+int xs_regex_match(const char *str, const char *rx);
6 xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
7 #define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
8 xs_list *xs_regex_select_n(const char *str, const char *rx, int count);
9@@ -18,18 +19,21 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
10 #include <regex.h>
11
12 xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
13-/* splits str by regex */
14+/* splits str using regex as a separator, at most count times.
15+ Always returns a list:
16+ len == 0: regcomp error
17+ len == 1: full string (no matches)
18+ len == odd: first part [ separator / next part ]...
19+*/
20 {
21 regex_t re;
22 regmatch_t rm;
23 int offset = 0;
24- xs_list *list = NULL;
25+ xs_list *list = xs_list_new();
26 const char *p;
27
28 if (regcomp(&re, rx, REG_EXTENDED))
29- return NULL;
30-
31- list = xs_list_new();
32+ return list;
33
34 while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
35 /* add first the leading part of the string */
36@@ -60,16 +64,15 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
37 {
38 xs_list *list = xs_list_new();
39 xs *split = NULL;
40- xs_list *p;
41 xs_val *v;
42 int n = 0;
43+ int c = 0;
44
45 /* split */
46 split = xs_regex_split_n(str, rx, count);
47
48 /* now iterate to get only the 'separators' (odd ones) */
49- p = split;
50- while (xs_list_iter(&p, &v)) {
51+ while (xs_list_next(split, &v, &c)) {
52 if (n & 0x1)
53 list = xs_list_append(list, v);
54
55@@ -86,13 +89,12 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
56 {
57 xs_str *s = xs_str_new(NULL);
58 xs *split = xs_regex_split_n(str, rx, count);
59- xs_list *p;
60 xs_val *v;
61 int n = 0;
62+ int c = 0;
63 int pholder = !!strchr(rep, '&');
64
65- p = split;
66- while (xs_list_iter(&p, &v)) {
67+ while (xs_list_next(split, &v, &c)) {
68 if (n & 0x1) {
69 if (pholder) {
70 /* rep has a placeholder; process char by char */
71@@ -128,6 +130,16 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
72 return s;
73 }
74
75+
76+int xs_regex_match(const char *str, const char *rx)
77+/* returns if str matches the regex at least once */
78+{
79+ xs *l = xs_regex_select_n(str, rx, 1);
80+
81+ return xs_list_len(l) == 1;
82+}
83+
84+
85 #endif /* XS_IMPLEMENTATION */
86
87 #endif /* XS_REGEX_H */
+1,
-1
1@@ -1 +1 @@
2-/* f3818ad611f09313008a2102a5e543c232e1d824 2024-05-02T23:45:38+02:00 */
3+/* 6e75e8736f7f1b6ea6c6774d4bd922b3ad56b771 2024-05-15T11:42:19+02:00 */