]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/like_match.c
Tag appropriate files for rc3
[postgresql] / src / backend / utils / adt / like_match.c
1 /*-------------------------------------------------------------------------
2  *
3  * like_match.c
4  *        like expression handling internal code.
5  *
6  * This file is included by like.c *twice*, to provide an optimization
7  * for single-byte encodings.
8  *
9  * Before the inclusion, we need to define following macros:
10  *
11  * CHAREQ
12  * ICHAREQ
13  * NextChar
14  * CopyAdvChar
15  * MatchText (MBMatchText)
16  * MatchTextIC (MBMatchTextIC)
17  * do_like_escape (MB_do_like_escape)
18  *
19  * Copyright (c) 1996-2005, PostgreSQL Global Development Group
20  *
21  * IDENTIFICATION
22  *      $PostgreSQL: pgsql/src/backend/utils/adt/like_match.c,v 1.10 2004/12/31 22:01:22 pgsql Exp $
23  *
24  *-------------------------------------------------------------------------
25  */
26
27 /*
28 **      Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
29 **      Rich $alz is now <rsalz@bbn.com>.
30 **      Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
31 **
32 **      This code was shamelessly stolen from the "pql" code by myself and
33 **      slightly modified :)
34 **
35 **      All references to the word "star" were replaced by "percent"
36 **      All references to the word "wild" were replaced by "like"
37 **
38 **      All the nice shell RE matching stuff was replaced by just "_" and "%"
39 **
40 **      As I don't have a copy of the SQL standard handy I wasn't sure whether
41 **      to leave in the '\' escape character handling.
42 **
43 **      Keith Parks. <keith@mtcc.demon.co.uk>
44 **
45 **      SQL92 lets you specify the escape character by saying
46 **      LIKE <pattern> ESCAPE <escape character>. We are a small operation
47 **      so we force you to use '\'. - ay 7/95
48 **
49 **      Now we have the like_escape() function that converts patterns with
50 **      any specified escape character (or none at all) to the internal
51 **      default escape character, which is still '\'. - tgl 9/2000
52 **
53 ** The code is rewritten to avoid requiring null-terminated strings,
54 ** which in turn allows us to leave out some memcpy() operations.
55 ** This code should be faster and take less memory, but no promises...
56 ** - thomas 2000-08-06
57 **
58 */
59
60
61 /*--------------------
62  *      Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
63  *
64  *      LIKE_TRUE: they match
65  *      LIKE_FALSE: they don't match
66  *      LIKE_ABORT: not only don't they match, but the text is too short.
67  *
68  * If LIKE_ABORT is returned, then no suffix of the text can match the
69  * pattern either, so an upper-level % scan can stop scanning now.
70  *--------------------
71  */
72
73 static int
74 MatchText(unsigned char *t, int tlen, unsigned char *p, int plen)
75 {
76         /* Fast path for match-everything pattern */
77         if ((plen == 1) && (*p == '%'))
78                 return LIKE_TRUE;
79
80         while ((tlen > 0) && (plen > 0))
81         {
82                 if (*p == '\\')
83                 {
84                         /* Next pattern char must match literally, whatever it is */
85                         NextChar(p, plen);
86                         if ((plen <= 0) || !CHAREQ(t, p))
87                                 return LIKE_FALSE;
88                 }
89                 else if (*p == '%')
90                 {
91                         /* %% is the same as % according to the SQL standard */
92                         /* Advance past all %'s */
93                         while ((plen > 0) && (*p == '%'))
94                                 NextChar(p, plen);
95                         /* Trailing percent matches everything. */
96                         if (plen <= 0)
97                                 return LIKE_TRUE;
98
99                         /*
100                          * Otherwise, scan for a text position at which we can match
101                          * the rest of the pattern.
102                          */
103                         while (tlen > 0)
104                         {
105                                 /*
106                                  * Optimization to prevent most recursion: don't recurse
107                                  * unless first pattern char might match this text char.
108                                  */
109                                 if (CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
110                                 {
111                                         int                     matched = MatchText(t, tlen, p, plen);
112
113                                         if (matched != LIKE_FALSE)
114                                                 return matched; /* TRUE or ABORT */
115                                 }
116
117                                 NextChar(t, tlen);
118                         }
119
120                         /*
121                          * End of text with no match, so no point in trying later
122                          * places to start matching this pattern.
123                          */
124                         return LIKE_ABORT;
125                 }
126                 else if ((*p != '_') && !CHAREQ(t, p))
127                 {
128                         /*
129                          * Not the single-character wildcard and no explicit match?
130                          * Then time to quit...
131                          */
132                         return LIKE_FALSE;
133                 }
134
135                 NextChar(t, tlen);
136                 NextChar(p, plen);
137         }
138
139         if (tlen > 0)
140                 return LIKE_FALSE;              /* end of pattern, but not of text */
141
142         /* End of input string.  Do we have matching pattern remaining? */
143         while ((plen > 0) && (*p == '%'))       /* allow multiple %'s at end of
144                                                                                  * pattern */
145                 NextChar(p, plen);
146         if (plen <= 0)
147                 return LIKE_TRUE;
148
149         /*
150          * End of text with no match, so no point in trying later places to
151          * start matching this pattern.
152          */
153         return LIKE_ABORT;
154 }       /* MatchText() */
155
156 /*
157  * Same as above, but ignore case
158  */
159 static int
160 MatchTextIC(unsigned char *t, int tlen, unsigned char *p, int plen)
161 {
162         /* Fast path for match-everything pattern */
163         if ((plen == 1) && (*p == '%'))
164                 return LIKE_TRUE;
165
166         while ((tlen > 0) && (plen > 0))
167         {
168                 if (*p == '\\')
169                 {
170                         /* Next pattern char must match literally, whatever it is */
171                         NextChar(p, plen);
172                         if ((plen <= 0) || !ICHAREQ(t, p))
173                                 return LIKE_FALSE;
174                 }
175                 else if (*p == '%')
176                 {
177                         /* %% is the same as % according to the SQL standard */
178                         /* Advance past all %'s */
179                         while ((plen > 0) && (*p == '%'))
180                                 NextChar(p, plen);
181                         /* Trailing percent matches everything. */
182                         if (plen <= 0)
183                                 return LIKE_TRUE;
184
185                         /*
186                          * Otherwise, scan for a text position at which we can match
187                          * the rest of the pattern.
188                          */
189                         while (tlen > 0)
190                         {
191                                 /*
192                                  * Optimization to prevent most recursion: don't recurse
193                                  * unless first pattern char might match this text char.
194                                  */
195                                 if (ICHAREQ(t, p) || (*p == '\\') || (*p == '_'))
196                                 {
197                                         int                     matched = MatchTextIC(t, tlen, p, plen);
198
199                                         if (matched != LIKE_FALSE)
200                                                 return matched; /* TRUE or ABORT */
201                                 }
202
203                                 NextChar(t, tlen);
204                         }
205
206                         /*
207                          * End of text with no match, so no point in trying later
208                          * places to start matching this pattern.
209                          */
210                         return LIKE_ABORT;
211                 }
212                 else if ((*p != '_') && !ICHAREQ(t, p))
213                 {
214                         /*
215                          * Not the single-character wildcard and no explicit match?
216                          * Then time to quit...
217                          */
218                         return LIKE_FALSE;
219                 }
220
221                 NextChar(t, tlen);
222                 NextChar(p, plen);
223         }
224
225         if (tlen > 0)
226                 return LIKE_FALSE;              /* end of pattern, but not of text */
227
228         /* End of input string.  Do we have matching pattern remaining? */
229         while ((plen > 0) && (*p == '%'))       /* allow multiple %'s at end of
230                                                                                  * pattern */
231                 NextChar(p, plen);
232         if (plen <= 0)
233                 return LIKE_TRUE;
234
235         /*
236          * End of text with no match, so no point in trying later places to
237          * start matching this pattern.
238          */
239         return LIKE_ABORT;
240 }       /* MatchTextIC() */
241
242 /*
243  * like_escape() --- given a pattern and an ESCAPE string,
244  * convert the pattern to use Postgres' standard backslash escape convention.
245  */
246 static text *
247 do_like_escape(text *pat, text *esc)
248 {
249         text       *result;
250         unsigned char *p,
251                            *e,
252                            *r;
253         int                     plen,
254                                 elen;
255         bool            afterescape;
256
257         p = VARDATA(pat);
258         plen = (VARSIZE(pat) - VARHDRSZ);
259         e = VARDATA(esc);
260         elen = (VARSIZE(esc) - VARHDRSZ);
261
262         /*
263          * Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
264          * trying to calculate the size more accurately than that.
265          */
266         result = (text *) palloc(plen * 2 + VARHDRSZ);
267         r = VARDATA(result);
268
269         if (elen == 0)
270         {
271                 /*
272                  * No escape character is wanted.  Double any backslashes in the
273                  * pattern to make them act like ordinary characters.
274                  */
275                 while (plen > 0)
276                 {
277                         if (*p == '\\')
278                                 *r++ = '\\';
279                         CopyAdvChar(r, p, plen);
280                 }
281         }
282         else
283         {
284                 /*
285                  * The specified escape must be only a single character.
286                  */
287                 NextChar(e, elen);
288                 if (elen != 0)
289                         ereport(ERROR,
290                                         (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
291                                          errmsg("invalid escape string"),
292                           errhint("Escape string must be empty or one character.")));
293
294                 e = VARDATA(esc);
295
296                 /*
297                  * If specified escape is '\', just copy the pattern as-is.
298                  */
299                 if (*e == '\\')
300                 {
301                         memcpy(result, pat, VARSIZE(pat));
302                         return result;
303                 }
304
305                 /*
306                  * Otherwise, convert occurrences of the specified escape
307                  * character to '\', and double occurrences of '\' --- unless they
308                  * immediately follow an escape character!
309                  */
310                 afterescape = false;
311                 while (plen > 0)
312                 {
313                         if (CHAREQ(p, e) && !afterescape)
314                         {
315                                 *r++ = '\\';
316                                 NextChar(p, plen);
317                                 afterescape = true;
318                         }
319                         else if (*p == '\\')
320                         {
321                                 *r++ = '\\';
322                                 if (!afterescape)
323                                         *r++ = '\\';
324                                 NextChar(p, plen);
325                                 afterescape = false;
326                         }
327                         else
328                         {
329                                 CopyAdvChar(r, p, plen);
330                                 afterescape = false;
331                         }
332                 }
333         }
334
335         VARATT_SIZEP(result) = r - ((unsigned char *) result);
336
337         return result;
338 }