summaryrefslogtreecommitdiff
path: root/xs_match.h
diff options
context:
space:
mode:
authorGravatar default2023-09-17 02:52:44 +0200
committerGravatar default2023-09-17 02:52:44 +0200
commit952da47c6d2a209e6a41fabe7a5365380d63a5e5 (patch)
tree07677a47664d9d4490ab16e521d7cfdabeb4fd6c /xs_match.h
parentAlso accept Update activities for Page and Article. (diff)
downloadsnac2-952da47c6d2a209e6a41fabe7a5365380d63a5e5.tar.gz
snac2-952da47c6d2a209e6a41fabe7a5365380d63a5e5.tar.xz
snac2-952da47c6d2a209e6a41fabe7a5365380d63a5e5.zip
Backport from xs.
Diffstat (limited to 'xs_match.h')
-rw-r--r--xs_match.h71
1 files changed, 71 insertions, 0 deletions
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
14int xs_match(const char *str, const char *spec);
15
16#ifdef XS_IMPLEMENTATION
17
18int xs_match(const char *str, const char *spec)
19{
20 const char *o_str = str;
21
22again:
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 */