summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2024-05-15 13:27:23 +0200
committerGravatar default2024-05-15 13:27:23 +0200
commitff8d49a8991977e07cf6bb50156b2ffda08d0c0e (patch)
tree6de74de6401df99d2071db04a42124ce441b27a3
parentUpdated RELEASE_NOTES. (diff)
downloadpenes-snac2-ff8d49a8991977e07cf6bb50156b2ffda08d0c0e.tar.gz
penes-snac2-ff8d49a8991977e07cf6bb50156b2ffda08d0c0e.tar.xz
penes-snac2-ff8d49a8991977e07cf6bb50156b2ffda08d0c0e.zip
Use xs_regex_match() where applicable.
Diffstat (limited to '')
-rw-r--r--data.c14
-rw-r--r--xs.h2
-rw-r--r--xs_regex.h34
-rw-r--r--xs_version.h2
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)
2293 while (!r && !feof(f)) { 2293 while (!r && !feof(f)) {
2294 xs *rx = xs_strip_i(xs_readline(f)); 2294 xs *rx = xs_strip_i(xs_readline(f));
2295 2295
2296 if (*rx) { 2296 if (*rx && xs_regex_match(c, rx)) {
2297 xs *l = xs_regex_select_n(c, rx, 1); 2297 srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
2298 2298 r = 1;
2299 if (xs_list_len(l)) {
2300 srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
2301 r = 1;
2302 }
2303 } 2299 }
2304 } 2300 }
2305 2301
@@ -2576,9 +2572,7 @@ xs_list *content_search(snac *user, const char *regex,
2576 c = xs_tolower_i(c); 2572 c = xs_tolower_i(c);
2577 2573
2578 /* apply regex */ 2574 /* apply regex */
2579 xs *l = xs_regex_select_n(c, i_regex, 1); 2575 if (xs_regex_match(c, i_regex)) {
2580
2581 if (xs_list_len(l)) {
2582 if (xs_set_add(&seen, md5) == 1) 2576 if (xs_set_add(&seen, md5) == 1)
2583 show--; 2577 show--;
2584 } 2578 }
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)
1049xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value) 1049xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value)
1050/* prepends a memory block to the dict */ 1050/* prepends a memory block to the dict */
1051{ 1051{
1052 return _xs_dict_write_ditem(dict, 4, key, value, xs_size(value)); 1052 return _xs_dict_write_ditem(dict, 1 + _XS_TYPE_SIZE, key, value, xs_size(value));
1053} 1053}
1054 1054
1055 1055
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 @@
4 4
5#define _XS_REGEX_H 5#define _XS_REGEX_H
6 6
7int xs_regex_match(const char *str, const char *rx);
7xs_list *xs_regex_split_n(const char *str, const char *rx, int count); 8xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
8#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL) 9#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
9xs_list *xs_regex_select_n(const char *str, const char *rx, int count); 10xs_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
18#include <regex.h> 19#include <regex.h>
19 20
20xs_list *xs_regex_split_n(const char *str, const char *rx, int count) 21xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
21/* splits str by regex */ 22/* splits str using regex as a separator, at most count times.
23 Always returns a list:
24 len == 0: regcomp error
25 len == 1: full string (no matches)
26 len == odd: first part [ separator / next part ]...
27*/
22{ 28{
23 regex_t re; 29 regex_t re;
24 regmatch_t rm; 30 regmatch_t rm;
25 int offset = 0; 31 int offset = 0;
26 xs_list *list = NULL; 32 xs_list *list = xs_list_new();
27 const char *p; 33 const char *p;
28 34
29 if (regcomp(&re, rx, REG_EXTENDED)) 35 if (regcomp(&re, rx, REG_EXTENDED))
30 return NULL; 36 return list;
31
32 list = xs_list_new();
33 37
34 while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) { 38 while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
35 /* add first the leading part of the string */ 39 /* 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)
60{ 64{
61 xs_list *list = xs_list_new(); 65 xs_list *list = xs_list_new();
62 xs *split = NULL; 66 xs *split = NULL;
63 xs_list *p;
64 xs_val *v; 67 xs_val *v;
65 int n = 0; 68 int n = 0;
69 int c = 0;
66 70
67 /* split */ 71 /* split */
68 split = xs_regex_split_n(str, rx, count); 72 split = xs_regex_split_n(str, rx, count);
69 73
70 /* now iterate to get only the 'separators' (odd ones) */ 74 /* now iterate to get only the 'separators' (odd ones) */
71 p = split; 75 while (xs_list_next(split, &v, &c)) {
72 while (xs_list_iter(&p, &v)) {
73 if (n & 0x1) 76 if (n & 0x1)
74 list = xs_list_append(list, v); 77 list = xs_list_append(list, v);
75 78
@@ -86,13 +89,12 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
86{ 89{
87 xs_str *s = xs_str_new(NULL); 90 xs_str *s = xs_str_new(NULL);
88 xs *split = xs_regex_split_n(str, rx, count); 91 xs *split = xs_regex_split_n(str, rx, count);
89 xs_list *p;
90 xs_val *v; 92 xs_val *v;
91 int n = 0; 93 int n = 0;
94 int c = 0;
92 int pholder = !!strchr(rep, '&'); 95 int pholder = !!strchr(rep, '&');
93 96
94 p = split; 97 while (xs_list_next(split, &v, &c)) {
95 while (xs_list_iter(&p, &v)) {
96 if (n & 0x1) { 98 if (n & 0x1) {
97 if (pholder) { 99 if (pholder) {
98 /* rep has a placeholder; process char by char */ 100 /* 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
128 return s; 130 return s;
129} 131}
130 132
133
134int xs_regex_match(const char *str, const char *rx)
135/* returns if str matches the regex at least once */
136{
137 xs *l = xs_regex_select_n(str, rx, 1);
138
139 return xs_list_len(l) == 1;
140}
141
142
131#endif /* XS_IMPLEMENTATION */ 143#endif /* XS_IMPLEMENTATION */
132 144
133#endif /* XS_REGEX_H */ 145#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 */