]> mj.ucw.cz Git - libucw.git/blob - lib/patmatch.h
New pattern matching code -- simplified and debugged.
[libucw.git] / lib / patmatch.h
1 /*
2  *      Sherlock Library -- Generic Shell-Like Pattern Matching (currently only '?' and '*')
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 int
8 MATCH_FUNC_NAME(byte *p, byte *s)
9 {
10   while (*p)
11     {
12       if (*p == '?' && *s)
13         p++, s++;
14       else if (*p == '*')
15         {
16           int z = p[1];
17
18           if (!z)
19             return 1;
20           if (z == '\\' && p[2])
21             z = p[2];
22           z = Convert(z);
23           while (*s)
24             {
25               while (*s && Convert(*s) != z)
26                 s++;
27               if (*s && match_pattern(p+1, s))
28                 return 1;
29               s++;
30             }
31           return 0;
32         }
33       else
34         {
35           if (*p == '\\' && p[1])
36             p++;
37           if (Convert(*p++) != Convert(*s++))
38             return 0;
39         }
40     }
41   return !*s;
42 }