index-pack: fix compilation with NO_PTHREADS
[gitweb.git] / wildmatch.c
index 2a655fa7d4185471ea5ff791a1a441422ebd87f9..f91ba99f32c047e5f3238668ae83de647ab92df2 100644 (file)
@@ -52,7 +52,7 @@ typedef unsigned char uchar;
 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
 
 /* Match pattern "p" against "text" */
-static int dowild(const uchar *p, const uchar *text, int force_lower_case)
+static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 {
        uchar p_ch;
        const uchar *pattern = p;
@@ -62,9 +62,9 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                uchar t_ch, prev_ch;
                if ((t_ch = *text) == '\0' && p_ch != '*')
                        return WM_ABORT_ALL;
-               if (force_lower_case && ISUPPER(t_ch))
+               if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
                        t_ch = tolower(t_ch);
-               if (force_lower_case && ISUPPER(p_ch))
+               if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
                        p_ch = tolower(p_ch);
                switch (p_ch) {
                case '\\':
@@ -78,14 +78,17 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                        continue;
                case '?':
                        /* Match anything but '/'. */
-                       if (t_ch == '/')
+                       if ((flags & WM_PATHNAME) && t_ch == '/')
                                return WM_NOMATCH;
                        continue;
                case '*':
                        if (*++p == '*') {
                                const uchar *prev_p = p - 2;
                                while (*++p == '*') {}
-                               if ((prev_p < pattern || *prev_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] == '/'))) {
                                        /*
@@ -98,13 +101,14 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                                         * both foo/bar and foo/a/bar.
                                         */
                                        if (p[0] == '/' &&
-                                           dowild(p + 1, text, force_lower_case) == WM_MATCH)
+                                           dowild(p + 1, text, flags) == WM_MATCH)
                                                return WM_MATCH;
                                        match_slash = 1;
                                } else
                                        return WM_ABORT_MALFORMED;
                        } else
-                               match_slash = 0;
+                               /* 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. */
@@ -113,11 +117,46 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                                                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;
-                               if ((matched = dowild(p, text,  force_lower_case)) != WM_NOMATCH) {
+                               /*
+                                * Try to advance faster when an asterisk is
+                                * followed by a literal. We know in this case
+                                * that the 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 == '/')
@@ -157,6 +196,11 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                                        }
                                        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;
@@ -206,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                                        } 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;
@@ -215,7 +261,8 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
                                } else if (t_ch == p_ch)
                                        matched = 1;
                        } while (prev_ch = p_ch, (p_ch = *++p) != ']');
-                       if (matched == negated || t_ch == '/')
+                       if (matched == negated ||
+                           ((flags & WM_PATHNAME) && t_ch == '/'))
                                return WM_NOMATCH;
                        continue;
                }
@@ -228,6 +275,5 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
 int wildmatch(const char *pattern, const char *text,
              unsigned int flags, struct wildopts *wo)
 {
-       return dowild((const uchar*)pattern, (const uchar*)text,
-                     flags & WM_CASEFOLD ? 1 :0);
+       return dowild((const uchar*)pattern, (const uchar*)text, flags);
 }