1/* Copyright (c) 2000, 2003, 2004 MySQL AB
2 Use is subject to license terms
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16
17/* Funktions for comparing with wild-cards */
18
19#include "mysys_priv.h"
20
21 /* Test if a string is "comparable" to a wild-card string */
22 /* returns 0 if the strings are "comparable" */
23
24char wild_many='*';
25char wild_one='?';
26char wild_prefix=0; /* QQ this can potentially cause a SIGSEGV */
27
28int wild_compare(register const char *str, register const char *wildstr,
29 pbool str_is_pattern)
30{
31 char cmp;
32 DBUG_ENTER("wild_compare");
33
34 while (*wildstr)
35 {
36 while (*wildstr && *wildstr != wild_many && *wildstr != wild_one)
37 {
38 if (*wildstr == wild_prefix && wildstr[1])
39 {
40 wildstr++;
41 if (str_is_pattern && *str++ != wild_prefix)
42 DBUG_RETURN(1);
43 }
44 if (*wildstr++ != *str++)
45 DBUG_RETURN(1);
46 }
47 if (! *wildstr )
48 DBUG_RETURN(*str != 0);
49 if (*wildstr++ == wild_one)
50 {
51 if (! *str || (str_is_pattern && *str == wild_many))
52 DBUG_RETURN(1); /* One char; skip */
53 if (*str++ == wild_prefix && str_is_pattern && *str)
54 str++;
55 }
56 else
57 { /* Found '*' */
58 while (str_is_pattern && *str == wild_many)
59 str++;
60 for (; *wildstr == wild_many || *wildstr == wild_one; wildstr++)
61 if (*wildstr == wild_many)
62 {
63 while (str_is_pattern && *str == wild_many)
64 str++;
65 }
66 else
67 {
68 if (str_is_pattern && *str == wild_prefix && str[1])
69 str+=2;
70 else if (! *str++)
71 DBUG_RETURN (1);
72 }
73 if (!*wildstr)
74 DBUG_RETURN(0); /* '*' as last char: OK */
75 if ((cmp= *wildstr) == wild_prefix && wildstr[1] && !str_is_pattern)
76 cmp=wildstr[1];
77 for (;;str++)
78 {
79 while (*str && *str != cmp)
80 str++;
81 if (!*str)
82 DBUG_RETURN (1);
83 if (wild_compare(str,wildstr,str_is_pattern) == 0)
84 DBUG_RETURN (0);
85 }
86 /* We will never come here */
87 }
88 }
89 DBUG_RETURN (*str != 0);
90} /* wild_compare */
91