index-pack: fix compilation with NO_PTHREADS
[gitweb.git] / wildmatch.c
index 1b5bbacf1afc5689864f67a99a66555e5730ef56..f91ba99f32c047e5f3238668ae83de647ab92df2 100644 (file)
@@ -78,14 +78,17 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                        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] == '/'))) {
                                        /*
@@ -104,7 +107,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                } 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,10 +117,45 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                                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 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;
@@ -157,6 +196,11 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                        }
                                        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, unsigned int flags)
                                        } 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, unsigned int flags)
                                } 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;
                }