File: ctype-wildcmp.inl

package info (click to toggle)
mariadb 1%3A11.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 772,520 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (177 lines) | stat: -rw-r--r-- 5,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
   Copyright (c) 2024, MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA
*/


#ifndef MY_FUNCTION_NAME
#error MY_FUNCTION_NAME is not defined
#endif

#ifndef MY_MB_WC
#error MY_MB_WC is not defined
#endif

#ifndef MY_CHAR_EQ
#error MY_CHAR_EQ is not defined
#endif

/*
** Compare string against string with wildcard
**
**	0 if matched
**	-1 if not matched with wildcard
**	 1 if matched with wildcard
*/

static int
MY_FUNCTION_NAME(wildcmp)(CHARSET_INFO *cs,
                          const char *str, const char *str_end,
                          const char *wildstr,const char *wildend,
                          int escape, int w_one, int w_many,
                          int recurse_level)
{
  int result= -1;                             /* Not found, using wildcards */
  my_wc_t s_wc, w_wc;
  int scan;

  if (my_string_stack_guard && my_string_stack_guard(recurse_level))
    return 1;
  while (wildstr != wildend)
  {
    while (1)
    {
      my_bool escaped= 0;
      if ((scan= MY_MB_WC(cs, &w_wc, (const uchar*) wildstr,
                          (const uchar*) wildend)) <= 0)
        return 1;

      if (w_wc == (my_wc_t) w_many)
      {
        result= 1;                                /* Found an anchor char */
        break;
      }

      wildstr+= scan;
      if (w_wc ==  (my_wc_t) escape && wildstr < wildend)
      {
        if ((scan= MY_MB_WC(cs, &w_wc, (const uchar*) wildstr,
                            (const uchar*) wildend)) <= 0)
          return 1;
        wildstr+= scan;
        escaped= 1;
      }

      if ((scan= MY_MB_WC(cs, &s_wc, (const uchar*) str,
                          (const uchar*) str_end)) <= 0)
        return 1;
      str+= scan;

      if (!escaped && w_wc == (my_wc_t) w_one)
      {
        result= 1;                                /* Found an anchor char */
      }
      else
      {
        if (!MY_CHAR_EQ(cs, s_wc, w_wc))
          return 1;                               /* No match */
      }
      if (wildstr == wildend)
        return (str != str_end);                  /* Match if both are at end */
    }

    if (w_wc == (my_wc_t) w_many)
    {                                             /* Found w_many */
      /* Remove any '%' and '_' from the wild search string */
      for ( ; wildstr != wildend ; )
      {
        if ((scan= MY_MB_WC(cs, &w_wc, (const uchar*) wildstr,
                            (const uchar*) wildend)) <= 0)
          return 1;

        if (w_wc == (my_wc_t) w_many)
        {
          wildstr+= scan;
          continue;
        }

        if (w_wc == (my_wc_t) w_one)
        {
          wildstr+= scan;
          if ((scan= MY_MB_WC(cs, &s_wc, (const uchar*) str,
                              (const uchar*) str_end)) <= 0)
            return 1;
          str+= scan;
          continue;
        }
        break;                                        /* Not a wild character */
      }

      if (wildstr == wildend)
        return 0;                                /* Ok if w_many is last */

      if (str == str_end)
        return -1;

      if ((scan= MY_MB_WC(cs, &w_wc, (const uchar*) wildstr,
                          (const uchar*) wildend)) <= 0)
        return 1;
      wildstr+= scan;

      if (w_wc ==  (my_wc_t) escape)
      {
        if (wildstr < wildend)
        {
          if ((scan= MY_MB_WC(cs, &w_wc, (const uchar*) wildstr,
                              (const uchar*) wildend)) <= 0)
            return 1;
          wildstr+= scan;
        }
      }

      while (1)
      {
        /* Skip until the first character from wildstr is found */
        while (str != str_end)
        {
          if ((scan= MY_MB_WC(cs, &s_wc, (const uchar*) str,
                              (const uchar*) str_end)) <= 0)
            return 1;

          if (MY_CHAR_EQ(cs, s_wc, w_wc))
            break;
          str+= scan;
        }
        if (str == str_end)
          return -1;

        str+= scan;
        result= MY_FUNCTION_NAME(wildcmp)(cs,
                                          str, str_end,
                                          wildstr, wildend,
                                          escape, w_one, w_many,
                                          recurse_level + 1);
        if (result <= 0)
          return result;
      }
    }
  }
  return (str != str_end ? 1 : 0);
}


#undef MY_FUNCTION_NAME
#undef MY_MB_WC
#undef MY_CHAR_EQ