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