diff options
Diffstat (limited to '')
| -rw-r--r-- | format.c | 2 | ||||
| -rw-r--r-- | snac.c | 1 | ||||
| -rw-r--r-- | xs_match.h | 71 | ||||
| -rw-r--r-- | xs_regex.h | 8 | ||||
| -rw-r--r-- | xs_version.h | 2 |
5 files changed, 78 insertions, 6 deletions
| @@ -235,7 +235,7 @@ xs_str *sanitize(const char *content) | |||
| 235 | 235 | ||
| 236 | if (valid_tags[i]) { | 236 | if (valid_tags[i]) { |
| 237 | /* accepted tag: rebuild it with only the accepted elements */ | 237 | /* accepted tag: rebuild it with only the accepted elements */ |
| 238 | xs *el = xs_regex_match(v, "(src|href|rel|class|target)=\"[^\"]*\""); | 238 | xs *el = xs_regex_select(v, "(src|href|rel|class|target)=\"[^\"]*\""); |
| 239 | xs *s3 = xs_join(el, " "); | 239 | xs *s3 = xs_join(el, " "); |
| 240 | 240 | ||
| 241 | s2 = xs_fmt("<%s%s%s%s>", | 241 | s2 = xs_fmt("<%s%s%s%s>", |
| @@ -17,6 +17,7 @@ | |||
| 17 | #include "xs_time.h" | 17 | #include "xs_time.h" |
| 18 | #include "xs_glob.h" | 18 | #include "xs_glob.h" |
| 19 | #include "xs_random.h" | 19 | #include "xs_random.h" |
| 20 | #include "xs_match.h" | ||
| 20 | 21 | ||
| 21 | #include "snac.h" | 22 | #include "snac.h" |
| 22 | 23 | ||
diff --git a/xs_match.h b/xs_match.h new file mode 100644 index 0000000..9f12c15 --- /dev/null +++ b/xs_match.h | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_MATCH_H | ||
| 4 | |||
| 5 | #define _XS_MATCH_H | ||
| 6 | |||
| 7 | /* spec is very similar to shell file globbing: | ||
| 8 | an * matches anything; | ||
| 9 | a ? matches any character; | ||
| 10 | | select alternative strings to match; | ||
| 11 | a \\ escapes a special character; | ||
| 12 | any other char matches itself. */ | ||
| 13 | |||
| 14 | int xs_match(const char *str, const char *spec); | ||
| 15 | |||
| 16 | #ifdef XS_IMPLEMENTATION | ||
| 17 | |||
| 18 | int xs_match(const char *str, const char *spec) | ||
| 19 | { | ||
| 20 | const char *o_str = str; | ||
| 21 | |||
| 22 | again: | ||
| 23 | if (*spec == '*') { | ||
| 24 | spec++; /* wildcard */ | ||
| 25 | |||
| 26 | do { | ||
| 27 | if (xs_match(str, spec)) | ||
| 28 | return 1; | ||
| 29 | str++; | ||
| 30 | } while (*str); | ||
| 31 | |||
| 32 | return 0; | ||
| 33 | } | ||
| 34 | |||
| 35 | if (*spec == '?' && *str) { | ||
| 36 | spec++; /* any character */ | ||
| 37 | str++; | ||
| 38 | goto again; | ||
| 39 | } | ||
| 40 | |||
| 41 | if (*spec == '|') | ||
| 42 | return 1; /* alternative separator? positive match */ | ||
| 43 | |||
| 44 | if (!*spec) | ||
| 45 | return 1; /* end of spec? positive match */ | ||
| 46 | |||
| 47 | if (*spec == '\\') | ||
| 48 | spec++; /* escaped char */ | ||
| 49 | |||
| 50 | if (*spec == *str) { | ||
| 51 | spec++; /* matched 1 char */ | ||
| 52 | str++; | ||
| 53 | goto again; | ||
| 54 | } | ||
| 55 | |||
| 56 | /* not matched; are there any alternatives? */ | ||
| 57 | while (*spec) { | ||
| 58 | if (*spec == '|') | ||
| 59 | return xs_match(o_str, spec + 1); /* try next alternative */ | ||
| 60 | |||
| 61 | if (*spec == '\\') | ||
| 62 | spec++; /* escaped char */ | ||
| 63 | spec++; | ||
| 64 | } | ||
| 65 | |||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | #endif /* XS_IMPLEMENTATION */ | ||
| 70 | |||
| 71 | #endif /* XS_MATCH_H */ | ||
| @@ -6,8 +6,8 @@ | |||
| 6 | 6 | ||
| 7 | xs_list *xs_regex_split_n(const char *str, const char *rx, int count); | 7 | xs_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) | 8 | #define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL) |
| 9 | xs_list *xs_regex_match_n(const char *str, const char *rx, int count); | 9 | xs_list *xs_regex_select_n(const char *str, const char *rx, int count); |
| 10 | #define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL) | 10 | #define xs_regex_select(str, rx) xs_regex_select_n(str, rx, XS_ALL) |
| 11 | xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int count); | 11 | xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int count); |
| 12 | #define xs_regex_replace_i(str, rx, rep) xs_regex_replace_in(str, rx, rep, XS_ALL) | 12 | #define xs_regex_replace_i(str, rx, rep) xs_regex_replace_in(str, rx, rep, XS_ALL) |
| 13 | #define xs_regex_replace_n(str, rx, rep, count) xs_regex_replace_in(xs_dup(str), rx, rep, count) | 13 | #define xs_regex_replace_n(str, rx, rep, count) xs_regex_replace_in(xs_dup(str), rx, rep, count) |
| @@ -55,8 +55,8 @@ xs_list *xs_regex_split_n(const char *str, const char *rx, int count) | |||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | xs_list *xs_regex_match_n(const char *str, const char *rx, int count) | 58 | xs_list *xs_regex_select_n(const char *str, const char *rx, int count) |
| 59 | /* returns a list with upto count matches */ | 59 | /* selects all matches and return them as a list */ |
| 60 | { | 60 | { |
| 61 | xs_list *list = xs_list_new(); | 61 | xs_list *list = xs_list_new(); |
| 62 | xs *split = NULL; | 62 | xs *split = NULL; |
diff --git a/xs_version.h b/xs_version.h index 5fd4466..f6c3412 100644 --- a/xs_version.h +++ b/xs_version.h | |||
| @@ -1 +1 @@ | |||
| /* 0ca53ca2b1c34efa95639d2a0f5bf4bd32f8958c */ | /* 06767a70773865042a70680aef50f7ecb077681a */ | ||