wildmatch: import wildmatch from git.git In commit 70a8fc999d (stop using fnmatch (either native or compat), 2014-02-15), upstream git has switched over all code from their internal fnmatch copy to its new wildmatch code. We haven't followed suit, and thus have developed some incompatibilities in how we match regular expressions. Import git's wildmatch from v2.22.0 and add a test suite based on their t3070-wildmatch.sh tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
diff --git a/COPYING b/COPYING
index da695eb..c0f61fb 100644
--- a/COPYING
+++ b/COPYING
@@ -991,3 +991,31 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+
+----------------------------------------------------------------------
+
+The bundled wildmatch code is licensed under the BSD license:
+
+Copyright Rich Salz.
+All rights reserved.
+
+Redistribution and use in any form are permitted provided that the
+following restrictions are are met:
+
+1. Source distributions must retain this entire copyright notice
+ and comment.
+2. Binary distributions must include the acknowledgement ``This
+ product includes software developed by Rich Salz'' in the
+ documentation or other materials provided with the
+ distribution. This must not be represented as an endorsement
+ or promotion without specific prior written permission.
+3. The origin of this software must not be misrepresented, either
+ by explicit claim or by omission. Credits must appear in the
+ source and documentation.
+4. Altered versions must be plainly marked as such in the source
+ and documentation and must not be misrepresented as being the
+ original software.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
diff --git a/src/wildmatch.c b/src/wildmatch.c
new file mode 100644
index 0000000..865f3f0
--- /dev/null
+++ b/src/wildmatch.c
@@ -0,0 +1,320 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ *
+ * Do shell-style pattern matching for ?, \, [], and * characters.
+ * It is 8bit clean.
+ *
+ * Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
+ * Rich $alz is now <rsalz@bbn.com>.
+ *
+ * Modified by Wayne Davison to special-case '/' matching, to make '**'
+ * work differently than '*', and to fix the character-class code.
+ *
+ * Imported from git.git.
+ */
+
+#include "wildmatch.h"
+
+#define GIT_SPACE 0x01
+#define GIT_DIGIT 0x02
+#define GIT_ALPHA 0x04
+#define GIT_GLOB_SPECIAL 0x08
+#define GIT_REGEX_SPECIAL 0x10
+#define GIT_PATHSPEC_MAGIC 0x20
+#define GIT_CNTRL 0x40
+#define GIT_PUNCT 0x80
+
+enum {
+ S = GIT_SPACE,
+ A = GIT_ALPHA,
+ D = GIT_DIGIT,
+ G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */
+ R = GIT_REGEX_SPECIAL, /* $, (, ), +, ., ^, {, | */
+ P = GIT_PATHSPEC_MAGIC, /* other non-alnum, except for ] and } */
+ X = GIT_CNTRL,
+ U = GIT_PUNCT,
+ Z = GIT_CNTRL | GIT_SPACE
+};
+
+static const unsigned char sane_ctype[256] = {
+ X, X, X, X, X, X, X, X, X, Z, Z, X, X, Z, X, X, /* 0.. 15 */
+ X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, /* 16.. 31 */
+ S, P, P, P, R, P, P, P, R, R, G, R, P, P, R, P, /* 32.. 47 */
+ D, D, D, D, D, D, D, D, D, D, P, P, P, P, P, G, /* 48.. 63 */
+ P, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */
+ A, A, A, A, A, A, A, A, A, A, A, G, G, U, R, P, /* 80.. 95 */
+ P, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */
+ A, A, A, A, A, A, A, A, A, A, A, R, R, U, P, X, /* 112..127 */
+ /* Nothing in the 128.. range */
+};
+
+#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
+#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
+
+typedef unsigned char uchar;
+
+/* What character marks an inverted character class? */
+#define NEGATE_CLASS '!'
+#define NEGATE_CLASS2 '^'
+
+#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
+ && *(class) == *(litmatch) \
+ && strncmp((char*)class, litmatch, len) == 0)
+
+#if defined STDC_HEADERS || !defined isascii
+# define ISASCII(c) 1
+#else
+# define ISASCII(c) isascii(c)
+#endif
+
+#ifdef isblank
+# define ISBLANK(c) (ISASCII(c) && isblank(c))
+#else
+# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
+#endif
+
+#ifdef isgraph
+# define ISGRAPH(c) (ISASCII(c) && isgraph(c))
+#else
+# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
+#endif
+
+#define ISPRINT(c) (ISASCII(c) && isprint(c))
+#define ISDIGIT(c) (ISASCII(c) && isdigit(c))
+#define ISALNUM(c) (ISASCII(c) && isalnum(c))
+#define ISALPHA(c) (ISASCII(c) && isalpha(c))
+#define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
+#define ISLOWER(c) (ISASCII(c) && islower(c))
+#define ISPUNCT(c) (ISASCII(c) && ispunct(c))
+#define ISSPACE(c) (ISASCII(c) && isspace(c))
+#define ISUPPER(c) (ISASCII(c) && isupper(c))
+#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
+
+/* Match pattern "p" against "text" */
+static int dowild(const uchar *p, const uchar *text, unsigned int flags)
+{
+ uchar p_ch;
+ const uchar *pattern = p;
+
+ for ( ; (p_ch = *p) != '\0'; text++, p++) {
+ int matched, match_slash, negated;
+ uchar t_ch, prev_ch;
+ if ((t_ch = *text) == '\0' && p_ch != '*')
+ return WM_ABORT_ALL;
+ if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
+ t_ch = tolower(t_ch);
+ if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
+ p_ch = tolower(p_ch);
+ switch (p_ch) {
+ case '\\':
+ /* Literal match with following character. Note that the test
+ * in "default" handles the p[1] == '\0' failure case. */
+ p_ch = *++p;
+ /* FALLTHROUGH */
+ default:
+ if (t_ch != p_ch)
+ return WM_NOMATCH;
+ continue;
+ case '?':
+ /* Match anything but '/'. */
+ if ((flags & WM_PATHNAME) && t_ch == '/')
+ return WM_NOMATCH;
+ continue;
+ case '*':
+ if (*++p == '*') {
+ const uchar *prev_p = p - 2;
+ while (*++p == '*') {}
+ if (!(flags & WM_PATHNAME))
+ /* without WM_PATHNAME, '*' == '**' */
+ match_slash = 1;
+ else if ((prev_p < pattern || *prev_p == '/') &&
+ (*p == '\0' || *p == '/' ||
+ (p[0] == '\\' && p[1] == '/'))) {
+ /*
+ * Assuming we already match 'foo/' and are at
+ * <star star slash>, just assume it matches
+ * nothing and go ahead match the rest of the
+ * pattern with the remaining string. This
+ * helps make foo/<*><*>/bar (<> because
+ * otherwise it breaks C comment syntax) match
+ * both foo/bar and foo/a/bar.
+ */
+ if (p[0] == '/' &&
+ dowild(p + 1, text, flags) == WM_MATCH)
+ return WM_MATCH;
+ match_slash = 1;
+ } else /* WM_PATHNAME is set */
+ match_slash = 0;
+ } else
+ /* without WM_PATHNAME, '*' == '**' */
+ match_slash = flags & WM_PATHNAME ? 0 : 1;
+ if (*p == '\0') {
+ /* Trailing "**" matches everything. Trailing "*" matches
+ * only if there are no more slash characters. */
+ if (!match_slash) {
+ if (strchr((char*)text, '/') != NULL)
+ return WM_NOMATCH;
+ }
+ return WM_MATCH;
+ } else if (!match_slash && *p == '/') {
+ /*
+ * _one_ asterisk followed by a slash
+ * with WM_PATHNAME matches the next
+ * directory
+ */
+ const char *slash = strchr((char*)text, '/');
+ if (!slash)
+ return WM_NOMATCH;
+ text = (const uchar*)slash;
+ /* the slash is consumed by the top-level for loop */
+ break;
+ }
+ while (1) {
+ if (t_ch == '\0')
+ break;
+ /*
+ * Try to advance faster when an asterisk is
+ * followed by a literal. We know in this case
+ * that the string before the literal
+ * must belong to "*".
+ * If match_slash is false, do not look past
+ * the first slash as it cannot belong to '*'.
+ */
+ if (!is_glob_special(*p)) {
+ p_ch = *p;
+ if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
+ p_ch = tolower(p_ch);
+ while ((t_ch = *text) != '\0' &&
+ (match_slash || t_ch != '/')) {
+ if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
+ t_ch = tolower(t_ch);
+ if (t_ch == p_ch)
+ break;
+ text++;
+ }
+ if (t_ch != p_ch)
+ return WM_NOMATCH;
+ }
+ if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
+ if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
+ return matched;
+ } else if (!match_slash && t_ch == '/')
+ return WM_ABORT_TO_STARSTAR;
+ t_ch = *++text;
+ }
+ return WM_ABORT_ALL;
+ case '[':
+ p_ch = *++p;
+#ifdef NEGATE_CLASS2
+ if (p_ch == NEGATE_CLASS2)
+ p_ch = NEGATE_CLASS;
+#endif
+ /* Assign literal 1/0 because of "matched" comparison. */
+ negated = p_ch == NEGATE_CLASS ? 1 : 0;
+ if (negated) {
+ /* Inverted character class. */
+ p_ch = *++p;
+ }
+ prev_ch = 0;
+ matched = 0;
+ do {
+ if (!p_ch)
+ return WM_ABORT_ALL;
+ if (p_ch == '\\') {
+ p_ch = *++p;
+ if (!p_ch)
+ return WM_ABORT_ALL;
+ if (t_ch == p_ch)
+ matched = 1;
+ } else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') {
+ p_ch = *++p;
+ if (p_ch == '\\') {
+ p_ch = *++p;
+ if (!p_ch)
+ return WM_ABORT_ALL;
+ }
+ if (t_ch <= p_ch && t_ch >= prev_ch)
+ matched = 1;
+ else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) {
+ uchar t_ch_upper = toupper(t_ch);
+ if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+ matched = 1;
+ }
+ p_ch = 0; /* This makes "prev_ch" get set to 0. */
+ } else if (p_ch == '[' && p[1] == ':') {
+ const uchar *s;
+ int i;
+ for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/
+ if (!p_ch)
+ return WM_ABORT_ALL;
+ i = p - s - 1;
+ if (i < 0 || p[-1] != ':') {
+ /* Didn't find ":]", so treat like a normal set. */
+ p = s - 2;
+ p_ch = '[';
+ if (t_ch == p_ch)
+ matched = 1;
+ continue;
+ }
+ if (CC_EQ(s,i, "alnum")) {
+ if (ISALNUM(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "alpha")) {
+ if (ISALPHA(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "blank")) {
+ if (ISBLANK(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "cntrl")) {
+ if (ISCNTRL(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "digit")) {
+ if (ISDIGIT(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "graph")) {
+ if (ISGRAPH(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "lower")) {
+ if (ISLOWER(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "print")) {
+ if (ISPRINT(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "punct")) {
+ if (ISPUNCT(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "space")) {
+ if (ISSPACE(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "upper")) {
+ if (ISUPPER(t_ch))
+ matched = 1;
+ else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+ matched = 1;
+ } else if (CC_EQ(s,i, "xdigit")) {
+ if (ISXDIGIT(t_ch))
+ matched = 1;
+ } else /* malformed [:class:] string */
+ return WM_ABORT_ALL;
+ p_ch = 0; /* This makes "prev_ch" get set to 0. */
+ } else if (t_ch == p_ch)
+ matched = 1;
+ } while (prev_ch = p_ch, (p_ch = *++p) != ']');
+ if (matched == negated ||
+ ((flags & WM_PATHNAME) && t_ch == '/'))
+ return WM_NOMATCH;
+ continue;
+ }
+ }
+
+ return *text ? WM_NOMATCH : WM_MATCH;
+}
+
+/* Match the "pattern" against the "text" string. */
+int wildmatch(const char *pattern, const char *text, unsigned int flags)
+{
+ return dowild((const uchar*)pattern, (const uchar*)text, flags);
+}
diff --git a/src/wildmatch.h b/src/wildmatch.h
new file mode 100644
index 0000000..44bb575
--- /dev/null
+++ b/src/wildmatch.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_wildmatch_h__
+#define INCLUDE_wildmatch_h__
+
+#include "common.h"
+
+#define WM_CASEFOLD 1
+#define WM_PATHNAME 2
+
+#define WM_NOMATCH 1
+#define WM_MATCH 0
+#define WM_ABORT_ALL -1
+#define WM_ABORT_TO_STARSTAR -2
+
+int wildmatch(const char *pattern, const char *text, unsigned int flags);
+
+#endif
diff --git a/tests/core/wildmatch.c b/tests/core/wildmatch.c
new file mode 100644
index 0000000..e7897c6
--- /dev/null
+++ b/tests/core/wildmatch.c
@@ -0,0 +1,248 @@
+#include "clar_libgit2.h"
+
+#include "wildmatch.h"
+
+#define assert_matches(string, pattern, wildmatch, iwildmatch, pathmatch, ipathmatch) \
+ assert_matches_(string, pattern, wildmatch, iwildmatch, pathmatch, ipathmatch, __FILE__, __LINE__)
+
+static void assert_matches_(const char *string, const char *pattern,
+ char expected_wildmatch, char expected_iwildmatch,
+ char expected_pathmatch, char expected_ipathmatch,
+ const char *file, size_t line)
+{
+ if (wildmatch(pattern, string, WM_PATHNAME) == expected_wildmatch)
+ clar__fail(file, line, "Test failed (wildmatch).", string, 1);
+ if (wildmatch(pattern, string, WM_PATHNAME|WM_CASEFOLD) == expected_iwildmatch)
+ clar__fail(file, line, "Test failed (iwildmatch).", string, 1);
+ if (wildmatch(pattern, string, 0) == expected_pathmatch)
+ clar__fail(file, line, "Test failed (pathmatch).", string, 1);
+ if (wildmatch(pattern, string, WM_CASEFOLD) == expected_ipathmatch)
+ clar__fail(file, line, "Test failed (ipathmatch).", string, 1);
+}
+
+/*
+ * Below testcases are imported from git.git, t3070-wildmatch,sh at tag v2.22.0.
+ * Note that we've only imported the direct wildcard tests, but not the matching
+ * tests for git-ls-files.
+ */
+
+void test_core_wildmatch__basic_wildmatch(void)
+{
+ assert_matches("foo", "foo", 1, 1, 1, 1);
+ assert_matches("foo", "bar", 0, 0, 0, 0);
+ assert_matches("", "", 1, 1, 1, 1);
+ assert_matches("foo", "???", 1, 1, 1, 1);
+ assert_matches("foo", "??", 0, 0, 0, 0);
+ assert_matches("foo", "*", 1, 1, 1, 1);
+ assert_matches("foo", "f*", 1, 1, 1, 1);
+ assert_matches("foo", "*f", 0, 0, 0, 0);
+ assert_matches("foo", "*foo*", 1, 1, 1, 1);
+ assert_matches("foobar", "*ob*a*r*", 1, 1, 1, 1);
+ assert_matches("aaaaaaabababab", "*ab", 1, 1, 1, 1);
+ assert_matches("foo*", "foo\\*", 1, 1, 1, 1);
+ assert_matches("foobar", "foo\\*bar", 0, 0, 0, 0);
+ assert_matches("f\\oo", "f\\\\oo", 1, 1, 1, 1);
+ assert_matches("ball", "*[al]?", 1, 1, 1, 1);
+ assert_matches("ten", "[ten]", 0, 0, 0, 0);
+ assert_matches("ten", "**[!te]", 1, 1, 1, 1);
+ assert_matches("ten", "**[!ten]", 0, 0, 0, 0);
+ assert_matches("ten", "t[a-g]n", 1, 1, 1, 1);
+ assert_matches("ten", "t[!a-g]n", 0, 0, 0, 0);
+ assert_matches("ton", "t[!a-g]n", 1, 1, 1, 1);
+ assert_matches("ton", "t[^a-g]n", 1, 1, 1, 1);
+ assert_matches("a]b", "a[]]b", 1, 1, 1, 1);
+ assert_matches("a-b", "a[]-]b", 1, 1, 1, 1);
+ assert_matches("a]b", "a[]-]b", 1, 1, 1, 1);
+ assert_matches("aab", "a[]-]b", 0, 0, 0, 0);
+ assert_matches("aab", "a[]a-]b", 1, 1, 1, 1);
+ assert_matches("]", "]", 1, 1, 1, 1);
+}
+
+void test_core_wildmatch__slash_matching_features(void)
+{
+ assert_matches("foo/baz/bar", "foo*bar", 0, 0, 1, 1);
+ assert_matches("foo/baz/bar", "foo**bar", 0, 0, 1, 1);
+ assert_matches("foobazbar", "foo**bar", 1, 1, 1, 1);
+ assert_matches("foo/baz/bar", "foo/**/bar", 1, 1, 1, 1);
+ assert_matches("foo/baz/bar", "foo/**/**/bar", 1, 1, 0, 0);
+ assert_matches("foo/b/a/z/bar", "foo/**/bar", 1, 1, 1, 1);
+ assert_matches("foo/b/a/z/bar", "foo/**/**/bar", 1, 1, 1, 1);
+ assert_matches("foo/bar", "foo/**/bar", 1, 1, 0, 0);
+ assert_matches("foo/bar", "foo/**/**/bar", 1, 1, 0, 0);
+ assert_matches("foo/bar", "foo?bar", 0, 0, 1, 1);
+ assert_matches("foo/bar", "foo[/]bar", 0, 0, 1, 1);
+ assert_matches("foo/bar", "foo[^a-z]bar", 0, 0, 1, 1);
+ assert_matches("foo/bar", "f[^eiu][^eiu][^eiu][^eiu][^eiu]r", 0, 0, 1, 1);
+ assert_matches("foo-bar", "f[^eiu][^eiu][^eiu][^eiu][^eiu]r", 1, 1, 1, 1);
+ assert_matches("foo", "**/foo", 1, 1, 0, 0);
+ assert_matches("XXX/foo", "**/foo", 1, 1, 1, 1);
+ assert_matches("bar/baz/foo", "**/foo", 1, 1, 1, 1);
+ assert_matches("bar/baz/foo", "*/foo", 0, 0, 1, 1);
+ assert_matches("foo/bar/baz", "**/bar*", 0, 0, 1, 1);
+ assert_matches("deep/foo/bar/baz", "**/bar/*", 1, 1, 1, 1);
+ assert_matches("deep/foo/bar/baz/", "**/bar/*", 0, 0, 1, 1);
+ assert_matches("deep/foo/bar/baz/", "**/bar/**", 1, 1, 1, 1);
+ assert_matches("deep/foo/bar", "**/bar/*", 0, 0, 0, 0);
+ assert_matches("deep/foo/bar/", "**/bar/**", 1, 1, 1, 1);
+ assert_matches("foo/bar/baz", "**/bar**", 0, 0, 1, 1);
+ assert_matches("foo/bar/baz/x", "*/bar/**", 1, 1, 1, 1);
+ assert_matches("deep/foo/bar/baz/x", "*/bar/**", 0, 0, 1, 1);
+ assert_matches("deep/foo/bar/baz/x", "**/bar/*/*", 1, 1, 1, 1);
+}
+
+void test_core_wildmatch__various_additional(void)
+{
+ assert_matches("acrt", "a[c-c]st", 0, 0, 0, 0);
+ assert_matches("acrt", "a[c-c]rt", 1, 1, 1, 1);
+ assert_matches("]", "[!]-]", 0, 0, 0, 0);
+ assert_matches("a", "[!]-]", 1, 1, 1, 1);
+ assert_matches("", "\\", 0, 0, 0, 0);
+ assert_matches("\\", "\\", 0, 0, 0, 0);
+ assert_matches("XXX/\\", "*/\\", 0, 0, 0, 0);
+ assert_matches("XXX/\\", "*/\\\\", 1, 1, 1, 1);
+ assert_matches("foo", "foo", 1, 1, 1, 1);
+ assert_matches("@foo", "@foo", 1, 1, 1, 1);
+ assert_matches("foo", "@foo", 0, 0, 0, 0);
+ assert_matches("[ab]", "\\[ab]", 1, 1, 1, 1);
+ assert_matches("[ab]", "[[]ab]", 1, 1, 1, 1);
+ assert_matches("[ab]", "[[:]ab]", 1, 1, 1, 1);
+ assert_matches("[ab]", "[[::]ab]", 0, 0, 0, 0);
+ assert_matches("[ab]", "[[:digit]ab]", 1, 1, 1, 1);
+ assert_matches("[ab]", "[\\[:]ab]", 1, 1, 1, 1);
+ assert_matches("?a?b", "\\??\\?b", 1, 1, 1, 1);
+ assert_matches("abc", "\\a\\b\\c", 1, 1, 1, 1);
+ assert_matches("foo", "", 0, 0, 0, 0);
+ assert_matches("foo/bar/baz/to", "**/t[o]", 1, 1, 1, 1);
+}
+
+void test_core_wildmatch__character_classes(void)
+{
+ assert_matches("a1B", "[[:alpha:]][[:digit:]][[:upper:]]", 1, 1, 1, 1);
+ assert_matches("a", "[[:digit:][:upper:][:space:]]", 0, 1, 0, 1);
+ assert_matches("A", "[[:digit:][:upper:][:space:]]", 1, 1, 1, 1);
+ assert_matches("1", "[[:digit:][:upper:][:space:]]", 1, 1, 1, 1);
+ assert_matches("1", "[[:digit:][:upper:][:spaci:]]", 0, 0, 0, 0);
+ assert_matches(" ", "[[:digit:][:upper:][:space:]]", 1, 1, 1, 1);
+ assert_matches(".", "[[:digit:][:upper:][:space:]]", 0, 0, 0, 0);
+ assert_matches(".", "[[:digit:][:punct:][:space:]]", 1, 1, 1, 1);
+ assert_matches("5", "[[:xdigit:]]", 1, 1, 1, 1);
+ assert_matches("f", "[[:xdigit:]]", 1, 1, 1, 1);
+ assert_matches("D", "[[:xdigit:]]", 1, 1, 1, 1);
+ assert_matches("_", "[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]", 1, 1, 1, 1);
+ assert_matches(".", "[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]", 1, 1, 1, 1);
+ assert_matches("5", "[a-c[:digit:]x-z]", 1, 1, 1, 1);
+ assert_matches("b", "[a-c[:digit:]x-z]", 1, 1, 1, 1);
+ assert_matches("y", "[a-c[:digit:]x-z]", 1, 1, 1, 1);
+ assert_matches("q", "[a-c[:digit:]x-z]", 0, 0, 0, 0);
+}
+
+void test_core_wildmatch__additional_with_malformed(void)
+{
+ assert_matches("]", "[\\\\-^]", 1, 1, 1, 1);
+ assert_matches("[", "[\\\\-^]", 0, 0, 0, 0);
+ assert_matches("-", "[\\-_]", 1, 1, 1, 1);
+ assert_matches("]", "[\\]]", 1, 1, 1, 1);
+ assert_matches("\\]", "[\\]]", 0, 0, 0, 0);
+ assert_matches("\\", "[\\]]", 0, 0, 0, 0);
+ assert_matches("ab", "a[]b", 0, 0, 0, 0);
+ assert_matches("a[]b", "a[]b", 0, 0, 0, 0);
+ assert_matches("ab[", "ab[", 0, 0, 0, 0);
+ assert_matches("ab", "[!", 0, 0, 0, 0);
+ assert_matches("ab", "[-", 0, 0, 0, 0);
+ assert_matches("-", "[-]", 1, 1, 1, 1);
+ assert_matches("-", "[a-", 0, 0, 0, 0);
+ assert_matches("-", "[!a-", 0, 0, 0, 0);
+ assert_matches("-", "[--A]", 1, 1, 1, 1);
+ assert_matches("5", "[--A]", 1, 1, 1, 1);
+ assert_matches(" ", "[ --]", 1, 1, 1, 1);
+ assert_matches("$", "[ --]", 1, 1, 1, 1);
+ assert_matches("-", "[ --]", 1, 1, 1, 1);
+ assert_matches("0", "[ --]", 0, 0, 0, 0);
+ assert_matches("-", "[---]", 1, 1, 1, 1);
+ assert_matches("-", "[------]", 1, 1, 1, 1);
+ assert_matches("j", "[a-e-n]", 0, 0, 0, 0);
+ assert_matches("-", "[a-e-n]", 1, 1, 1, 1);
+ assert_matches("a", "[!------]", 1, 1, 1, 1);
+ assert_matches("[", "[]-a]", 0, 0, 0, 0);
+ assert_matches("^", "[]-a]", 1, 1, 1, 1);
+ assert_matches("^", "[!]-a]", 0, 0, 0, 0);
+ assert_matches("[", "[!]-a]", 1, 1, 1, 1);
+ assert_matches("^", "[a^bc]", 1, 1, 1, 1);
+ assert_matches("-b]", "[a-]b]", 1, 1, 1, 1);
+ assert_matches("\\", "[\\]", 0, 0, 0, 0);
+ assert_matches("\\", "[\\\\]", 1, 1, 1, 1);
+ assert_matches("\\", "[!\\\\]", 0, 0, 0, 0);
+ assert_matches("G", "[A-\\\\]", 1, 1, 1, 1);
+ assert_matches("aaabbb", "b*a", 0, 0, 0, 0);
+ assert_matches("aabcaa", "*ba*", 0, 0, 0, 0);
+ assert_matches(",", "[,]", 1, 1, 1, 1);
+ assert_matches(",", "[\\\\,]", 1, 1, 1, 1);
+ assert_matches("\\", "[\\\\,]", 1, 1, 1, 1);
+ assert_matches("-", "[,-.]", 1, 1, 1, 1);
+ assert_matches("+", "[,-.]", 0, 0, 0, 0);
+ assert_matches("-.]", "[,-.]", 0, 0, 0, 0);
+ assert_matches("2", "[\\1-\\3]", 1, 1, 1, 1);
+ assert_matches("3", "[\\1-\\3]", 1, 1, 1, 1);
+ assert_matches("4", "[\\1-\\3]", 0, 0, 0, 0);
+ assert_matches("\\", "[[-\\]]", 1, 1, 1, 1);
+ assert_matches("[", "[[-\\]]", 1, 1, 1, 1);
+ assert_matches("]", "[[-\\]]", 1, 1, 1, 1);
+ assert_matches("-", "[[-\\]]", 0, 0, 0, 0);
+}
+
+void test_core_wildmatch__recursion(void)
+{
+ assert_matches("-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", 1, 1, 1, 1);
+ assert_matches("-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", 0, 0, 0, 0);
+ assert_matches("-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1", "-*-*-*-*-*-*-12-*-*-*-m-*-*-*", 0, 0, 0, 0);
+ assert_matches("XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1", "XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*", 1, 1, 1, 1);
+ assert_matches("XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1", "XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*", 0, 0, 0, 0);
+ assert_matches("abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt", "**/*a*b*g*n*t", 1, 1, 1, 1);
+ assert_matches("abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz", "**/*a*b*g*n*t", 0, 0, 0, 0);
+ assert_matches("foo", "*/*/*", 0, 0, 0, 0);
+ assert_matches("foo/bar", "*/*/*", 0, 0, 0, 0);
+ assert_matches("foo/bba/arr", "*/*/*", 1, 1, 1, 1);
+ assert_matches("foo/bb/aa/rr", "*/*/*", 0, 0, 1, 1);
+ assert_matches("foo/bb/aa/rr", "**/**/**", 1, 1, 1, 1);
+ assert_matches("abcXdefXghi", "*X*i", 1, 1, 1, 1);
+ assert_matches("ab/cXd/efXg/hi", "*X*i", 0, 0, 1, 1);
+ assert_matches("ab/cXd/efXg/hi", "*/*X*/*/*i", 1, 1, 1, 1);
+ assert_matches("ab/cXd/efXg/hi", "**/*X*/**/*i", 1, 1, 1, 1);
+}
+
+void test_core_wildmatch__pathmatch(void)
+{
+ assert_matches("foo", "fo", 0, 0, 0, 0);
+ assert_matches("foo/bar", "foo/bar", 1, 1, 1, 1);
+ assert_matches("foo/bar", "foo/*", 1, 1, 1, 1);
+ assert_matches("foo/bba/arr", "foo/*", 0, 0, 1, 1);
+ assert_matches("foo/bba/arr", "foo/**", 1, 1, 1, 1);
+ assert_matches("foo/bba/arr", "foo*", 0, 0, 1, 1);
+ assert_matches("foo/bba/arr", "foo**", 0, 0, 1, 1);
+ assert_matches("foo/bba/arr", "foo/*arr", 0, 0, 1, 1);
+ assert_matches("foo/bba/arr", "foo/**arr", 0, 0, 1, 1);
+ assert_matches("foo/bba/arr", "foo/*z", 0, 0, 0, 0);
+ assert_matches("foo/bba/arr", "foo/**z", 0, 0, 0, 0);
+ assert_matches("foo/bar", "foo?bar", 0, 0, 1, 1);
+ assert_matches("foo/bar", "foo[/]bar", 0, 0, 1, 1);
+ assert_matches("foo/bar", "foo[^a-z]bar", 0, 0, 1, 1);
+ assert_matches("ab/cXd/efXg/hi", "*Xg*i", 0, 0, 1, 1);
+}
+
+void test_core_wildmatch__case_sensitivity(void)
+{
+ assert_matches("a", "[A-Z]", 0, 1, 0, 1);
+ assert_matches("A", "[A-Z]", 1, 1, 1, 1);
+ assert_matches("A", "[a-z]", 0, 1, 0, 1);
+ assert_matches("a", "[a-z]", 1, 1, 1, 1);
+ assert_matches("a", "[[:upper:]]", 0, 1, 0, 1);
+ assert_matches("A", "[[:upper:]]", 1, 1, 1, 1);
+ assert_matches("A", "[[:lower:]]", 0, 1, 0, 1);
+ assert_matches("a", "[[:lower:]]", 1, 1, 1, 1);
+ assert_matches("A", "[B-Za]", 0, 1, 0, 1);
+ assert_matches("a", "[B-Za]", 1, 1, 1, 1);
+ assert_matches("A", "[B-a]", 0, 1, 0, 1);
+ assert_matches("a", "[B-a]", 1, 1, 1, 1);
+ assert_matches("z", "[Z-y]", 0, 1, 0, 1);
+ assert_matches("Z", "[Z-y]", 1, 1, 1, 1);
+}