Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 1 | /* |
| 2 | ** Do shell-style pattern matching for ?, \, [], and * characters. |
| 3 | ** It is 8bit clean. |
| 4 | ** |
| 5 | ** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986. |
| 6 | ** Rich $alz is now <rsalz@bbn.com>. |
| 7 | ** |
| 8 | ** Modified by Wayne Davison to special-case '/' matching, to make '**' |
| 9 | ** work differently than '*', and to fix the character-class code. |
| 10 | */ |
| 11 | |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 12 | #include "git-compat-util.h" |
Nguyễn Thái Ngọc Duy | feabcc1 | 2012-10-15 13:25:55 +0700 | [diff] [blame] | 13 | #include "wildmatch.h" |
| 14 | |
| 15 | typedef unsigned char uchar; |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 16 | |
Phillip Wood | 91b81b6 | 2023-03-20 16:10:02 +0000 | [diff] [blame] | 17 | /* Internal return values */ |
| 18 | #define WM_ABORT_ALL -1 |
| 19 | #define WM_ABORT_TO_STARSTAR -2 |
| 20 | |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 21 | /* What character marks an inverted character class? */ |
| 22 | #define NEGATE_CLASS '!' |
| 23 | #define NEGATE_CLASS2 '^' |
| 24 | |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 25 | #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \ |
| 26 | && *(class) == *(litmatch) \ |
| 27 | && strncmp((char*)class, litmatch, len) == 0) |
| 28 | |
| 29 | #if defined STDC_HEADERS || !defined isascii |
| 30 | # define ISASCII(c) 1 |
| 31 | #else |
| 32 | # define ISASCII(c) isascii(c) |
| 33 | #endif |
| 34 | |
| 35 | #ifdef isblank |
| 36 | # define ISBLANK(c) (ISASCII(c) && isblank(c)) |
| 37 | #else |
| 38 | # define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
| 39 | #endif |
| 40 | |
| 41 | #ifdef isgraph |
| 42 | # define ISGRAPH(c) (ISASCII(c) && isgraph(c)) |
| 43 | #else |
| 44 | # define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c)) |
| 45 | #endif |
| 46 | |
| 47 | #define ISPRINT(c) (ISASCII(c) && isprint(c)) |
| 48 | #define ISDIGIT(c) (ISASCII(c) && isdigit(c)) |
| 49 | #define ISALNUM(c) (ISASCII(c) && isalnum(c)) |
| 50 | #define ISALPHA(c) (ISASCII(c) && isalpha(c)) |
| 51 | #define ISCNTRL(c) (ISASCII(c) && iscntrl(c)) |
| 52 | #define ISLOWER(c) (ISASCII(c) && islower(c)) |
| 53 | #define ISPUNCT(c) (ISASCII(c) && ispunct(c)) |
| 54 | #define ISSPACE(c) (ISASCII(c) && isspace(c)) |
| 55 | #define ISUPPER(c) (ISASCII(c) && isupper(c)) |
| 56 | #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c)) |
| 57 | |
Nguyễn Thái Ngọc Duy | b0e0287 | 2012-10-15 13:25:53 +0700 | [diff] [blame] | 58 | /* Match pattern "p" against "text" */ |
Nguyễn Thái Ngọc Duy | 0c52816 | 2013-01-01 09:44:06 +0700 | [diff] [blame] | 59 | static int dowild(const uchar *p, const uchar *text, unsigned int flags) |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 60 | { |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 61 | uchar p_ch; |
Nguyễn Thái Ngọc Duy | 3a078de | 2013-01-01 09:44:02 +0700 | [diff] [blame] | 62 | const uchar *pattern = p; |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 63 | |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 64 | for ( ; (p_ch = *p) != '\0'; text++, p++) { |
Nguyễn Thái Ngọc Duy | b6a3d33 | 2013-01-01 09:44:04 +0700 | [diff] [blame] | 65 | int matched, match_slash, negated; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 66 | uchar t_ch, prev_ch; |
| 67 | if ((t_ch = *text) == '\0' && p_ch != '*') |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 68 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 0c52816 | 2013-01-01 09:44:06 +0700 | [diff] [blame] | 69 | if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 70 | t_ch = tolower(t_ch); |
Nguyễn Thái Ngọc Duy | 0c52816 | 2013-01-01 09:44:06 +0700 | [diff] [blame] | 71 | if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) |
Nguyễn Thái Ngọc Duy | 164bf83 | 2012-10-15 13:25:59 +0700 | [diff] [blame] | 72 | p_ch = tolower(p_ch); |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 73 | switch (p_ch) { |
| 74 | case '\\': |
| 75 | /* Literal match with following character. Note that the test |
| 76 | * in "default" handles the p[1] == '\0' failure case. */ |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 77 | p_ch = *++p; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 78 | /* FALLTHROUGH */ |
| 79 | default: |
| 80 | if (t_ch != p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 81 | return WM_NOMATCH; |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 82 | continue; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 83 | case '?': |
| 84 | /* Match anything but '/'. */ |
Nguyễn Thái Ngọc Duy | c41244e | 2013-01-01 09:44:07 +0700 | [diff] [blame] | 85 | if ((flags & WM_PATHNAME) && t_ch == '/') |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 86 | return WM_NOMATCH; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 87 | continue; |
| 88 | case '*': |
| 89 | if (*++p == '*') { |
Phillip Wood | 81b26f8 | 2023-03-20 16:10:01 +0000 | [diff] [blame] | 90 | const uchar *prev_p = p; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 91 | while (*++p == '*') {} |
Nguyễn Thái Ngọc Duy | c41244e | 2013-01-01 09:44:07 +0700 | [diff] [blame] | 92 | if (!(flags & WM_PATHNAME)) |
| 93 | /* without WM_PATHNAME, '*' == '**' */ |
| 94 | match_slash = 1; |
Phillip Wood | 81b26f8 | 2023-03-20 16:10:01 +0000 | [diff] [blame] | 95 | else if ((prev_p - pattern < 2 || *(prev_p - 2) == '/') && |
Nguyễn Thái Ngọc Duy | 40bbee0 | 2012-10-15 13:26:00 +0700 | [diff] [blame] | 96 | (*p == '\0' || *p == '/' || |
| 97 | (p[0] == '\\' && p[1] == '/'))) { |
Nguyễn Thái Ngọc Duy | 4c251e5 | 2012-10-15 13:26:01 +0700 | [diff] [blame] | 98 | /* |
| 99 | * Assuming we already match 'foo/' and are at |
| 100 | * <star star slash>, just assume it matches |
| 101 | * nothing and go ahead match the rest of the |
| 102 | * pattern with the remaining string. This |
| 103 | * helps make foo/<*><*>/bar (<> because |
| 104 | * otherwise it breaks C comment syntax) match |
| 105 | * both foo/bar and foo/a/bar. |
| 106 | */ |
| 107 | if (p[0] == '/' && |
Nguyễn Thái Ngọc Duy | 0c52816 | 2013-01-01 09:44:06 +0700 | [diff] [blame] | 108 | dowild(p + 1, text, flags) == WM_MATCH) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 109 | return WM_MATCH; |
| 110 | match_slash = 1; |
Nguyễn Thái Ngọc Duy | e5bbe09 | 2018-10-27 10:48:23 +0200 | [diff] [blame] | 111 | } else /* WM_PATHNAME is set */ |
| 112 | match_slash = 0; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 113 | } else |
Nguyễn Thái Ngọc Duy | c41244e | 2013-01-01 09:44:07 +0700 | [diff] [blame] | 114 | /* without WM_PATHNAME, '*' == '**' */ |
| 115 | match_slash = flags & WM_PATHNAME ? 0 : 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 116 | if (*p == '\0') { |
| 117 | /* Trailing "**" matches everything. Trailing "*" matches |
| 118 | * only if there are no more slash characters. */ |
Nguyễn Thái Ngọc Duy | b6a3d33 | 2013-01-01 09:44:04 +0700 | [diff] [blame] | 119 | if (!match_slash) { |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 120 | if (strchr((char *)text, '/')) |
Phillip Wood | 1f2e05f | 2023-03-20 16:10:00 +0000 | [diff] [blame] | 121 | return WM_ABORT_TO_STARSTAR; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 122 | } |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 123 | return WM_MATCH; |
Nguyễn Thái Ngọc Duy | 4698344 | 2013-01-01 09:44:09 +0700 | [diff] [blame] | 124 | } else if (!match_slash && *p == '/') { |
| 125 | /* |
| 126 | * _one_ asterisk followed by a slash |
| 127 | * with WM_PATHNAME matches the next |
| 128 | * directory |
| 129 | */ |
| 130 | const char *slash = strchr((char*)text, '/'); |
| 131 | if (!slash) |
Phillip Wood | 1f2e05f | 2023-03-20 16:10:00 +0000 | [diff] [blame] | 132 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 4698344 | 2013-01-01 09:44:09 +0700 | [diff] [blame] | 133 | text = (const uchar*)slash; |
| 134 | /* the slash is consumed by the top-level for loop */ |
| 135 | break; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 136 | } |
| 137 | while (1) { |
| 138 | if (t_ch == '\0') |
| 139 | break; |
Nguyễn Thái Ngọc Duy | 6f1a31f | 2013-01-01 09:44:10 +0700 | [diff] [blame] | 140 | /* |
| 141 | * Try to advance faster when an asterisk is |
| 142 | * followed by a literal. We know in this case |
Li Peng | 832c0e5 | 2016-05-06 20:36:46 +0800 | [diff] [blame] | 143 | * that the string before the literal |
Nguyễn Thái Ngọc Duy | 6f1a31f | 2013-01-01 09:44:10 +0700 | [diff] [blame] | 144 | * must belong to "*". |
| 145 | * If match_slash is false, do not look past |
| 146 | * the first slash as it cannot belong to '*'. |
| 147 | */ |
| 148 | if (!is_glob_special(*p)) { |
| 149 | p_ch = *p; |
| 150 | if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) |
| 151 | p_ch = tolower(p_ch); |
| 152 | while ((t_ch = *text) != '\0' && |
| 153 | (match_slash || t_ch != '/')) { |
| 154 | if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) |
| 155 | t_ch = tolower(t_ch); |
| 156 | if (t_ch == p_ch) |
| 157 | break; |
| 158 | text++; |
| 159 | } |
Phillip Wood | 1f2e05f | 2023-03-20 16:10:00 +0000 | [diff] [blame] | 160 | if (t_ch != p_ch) { |
| 161 | if (match_slash) |
| 162 | return WM_ABORT_ALL; |
| 163 | else |
| 164 | return WM_ABORT_TO_STARSTAR; |
| 165 | } |
Nguyễn Thái Ngọc Duy | 6f1a31f | 2013-01-01 09:44:10 +0700 | [diff] [blame] | 166 | } |
Nguyễn Thái Ngọc Duy | 0c52816 | 2013-01-01 09:44:06 +0700 | [diff] [blame] | 167 | if ((matched = dowild(p, text, flags)) != WM_NOMATCH) { |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 168 | if (!match_slash || matched != WM_ABORT_TO_STARSTAR) |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 169 | return matched; |
Nguyễn Thái Ngọc Duy | b6a3d33 | 2013-01-01 09:44:04 +0700 | [diff] [blame] | 170 | } else if (!match_slash && t_ch == '/') |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 171 | return WM_ABORT_TO_STARSTAR; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 172 | t_ch = *++text; |
| 173 | } |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 174 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 175 | case '[': |
| 176 | p_ch = *++p; |
| 177 | #ifdef NEGATE_CLASS2 |
| 178 | if (p_ch == NEGATE_CLASS2) |
| 179 | p_ch = NEGATE_CLASS; |
| 180 | #endif |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 181 | /* Assign literal 1/0 because of "matched" comparison. */ |
| 182 | negated = p_ch == NEGATE_CLASS ? 1 : 0; |
Nguyễn Thái Ngọc Duy | b6a3d33 | 2013-01-01 09:44:04 +0700 | [diff] [blame] | 183 | if (negated) { |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 184 | /* Inverted character class. */ |
| 185 | p_ch = *++p; |
| 186 | } |
| 187 | prev_ch = 0; |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 188 | matched = 0; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 189 | do { |
| 190 | if (!p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 191 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 192 | if (p_ch == '\\') { |
| 193 | p_ch = *++p; |
| 194 | if (!p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 195 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 196 | if (t_ch == p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 197 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 198 | } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') { |
| 199 | p_ch = *++p; |
| 200 | if (p_ch == '\\') { |
| 201 | p_ch = *++p; |
| 202 | if (!p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 203 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 204 | } |
| 205 | if (t_ch <= p_ch && t_ch >= prev_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 206 | matched = 1; |
Anthony Ramine | b79c0c3 | 2013-05-30 12:19:10 +0200 | [diff] [blame] | 207 | else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) { |
| 208 | uchar t_ch_upper = toupper(t_ch); |
| 209 | if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch) |
| 210 | matched = 1; |
| 211 | } |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 212 | p_ch = 0; /* This makes "prev_ch" get set to 0. */ |
| 213 | } else if (p_ch == '[' && p[1] == ':') { |
| 214 | const uchar *s; |
| 215 | int i; |
| 216 | for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/ |
| 217 | if (!p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 218 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 219 | i = p - s - 1; |
| 220 | if (i < 0 || p[-1] != ':') { |
| 221 | /* Didn't find ":]", so treat like a normal set. */ |
| 222 | p = s - 2; |
| 223 | p_ch = '['; |
| 224 | if (t_ch == p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 225 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 226 | continue; |
| 227 | } |
| 228 | if (CC_EQ(s,i, "alnum")) { |
| 229 | if (ISALNUM(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 230 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 231 | } else if (CC_EQ(s,i, "alpha")) { |
| 232 | if (ISALPHA(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 233 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 234 | } else if (CC_EQ(s,i, "blank")) { |
| 235 | if (ISBLANK(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 236 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 237 | } else if (CC_EQ(s,i, "cntrl")) { |
| 238 | if (ISCNTRL(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 239 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 240 | } else if (CC_EQ(s,i, "digit")) { |
| 241 | if (ISDIGIT(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 242 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 243 | } else if (CC_EQ(s,i, "graph")) { |
| 244 | if (ISGRAPH(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 245 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 246 | } else if (CC_EQ(s,i, "lower")) { |
| 247 | if (ISLOWER(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 248 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 249 | } else if (CC_EQ(s,i, "print")) { |
| 250 | if (ISPRINT(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 251 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 252 | } else if (CC_EQ(s,i, "punct")) { |
| 253 | if (ISPUNCT(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 254 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 255 | } else if (CC_EQ(s,i, "space")) { |
| 256 | if (ISSPACE(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 257 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 258 | } else if (CC_EQ(s,i, "upper")) { |
| 259 | if (ISUPPER(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 260 | matched = 1; |
Anthony Ramine | b79c0c3 | 2013-05-30 12:19:10 +0200 | [diff] [blame] | 261 | else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) |
| 262 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 263 | } else if (CC_EQ(s,i, "xdigit")) { |
| 264 | if (ISXDIGIT(t_ch)) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 265 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 266 | } else /* malformed [:class:] string */ |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 267 | return WM_ABORT_ALL; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 268 | p_ch = 0; /* This makes "prev_ch" get set to 0. */ |
| 269 | } else if (t_ch == p_ch) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 270 | matched = 1; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 271 | } while (prev_ch = p_ch, (p_ch = *++p) != ']'); |
Nguyễn Thái Ngọc Duy | c41244e | 2013-01-01 09:44:07 +0700 | [diff] [blame] | 272 | if (matched == negated || |
| 273 | ((flags & WM_PATHNAME) && t_ch == '/')) |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 274 | return WM_NOMATCH; |
Nguyễn Thái Ngọc Duy | 327f2f3 | 2012-10-15 13:25:54 +0700 | [diff] [blame] | 275 | continue; |
| 276 | } |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 277 | } |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 278 | |
Nguyễn Thái Ngọc Duy | 9b3497c | 2013-01-01 09:44:05 +0700 | [diff] [blame] | 279 | return *text ? WM_NOMATCH : WM_MATCH; |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* Match the "pattern" against the "text" string. */ |
Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 +0000 | [diff] [blame] | 283 | int wildmatch(const char *pattern, const char *text, unsigned int flags) |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 284 | { |
Phillip Wood | 91b81b6 | 2023-03-20 16:10:02 +0000 | [diff] [blame] | 285 | int res = dowild((const uchar*)pattern, (const uchar*)text, flags); |
| 286 | return res == WM_MATCH ? WM_MATCH : WM_NOMATCH; |
Nguyễn Thái Ngọc Duy | 5230f60 | 2012-10-15 13:25:52 +0700 | [diff] [blame] | 287 | } |