1
/*-------------------------------------------------------------------------
4
* LIKE pattern matching internal code.
6
* This file is included by like.c four times, to provide matching code for
7
* (1) single-byte encodings, (2) UTF8, (3) other multi-byte encodings,
8
* and (4) case insensitive matches in single byte encodings.
9
* (UTF8 is a special case because we can use a much more efficient version
10
* of NextChar than can be used for general multi-byte encodings.)
12
* Before the inclusion, we need to define following macros:
15
* MatchText - to name of function wanted
16
* do_like_escape - name of function if wanted - needs CHAREQ and CopyAdvChar
17
* MATCH_LOWER - define iff using to_lower on text chars
19
* Copyright (c) 1996-2009, PostgreSQL Global Development Group
24
*-------------------------------------------------------------------------
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.
32
** This code was shamelessly stolen from the "pql" code by myself and
33
** slightly modified :)
35
** All references to the word "star" were replaced by "percent"
36
** All references to the word "wild" were replaced by "like"
38
** All the nice shell RE matching stuff was replaced by just "_" and "%"
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.
43
** Keith Parks. <keith@mtcc.demon.co.uk>
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
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
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
61
/*--------------------
62
* Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
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.
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.
74
#define TCHAR(t) ((char) tolower((unsigned char) (t)))
80
MatchText(char *t, int tlen, char *p, int plen)
82
/* Fast path for match-everything pattern */
83
if ((plen == 1) && (*p == '%'))
87
* In this loop, we advance by char when matching wildcards (and thus on
88
* recursive entry to this function we are properly char-synced). On other
89
* occasions it is safe to advance by byte, as the text and pattern will
90
* be in lockstep. This allows us to perform all comparisons between the
91
* text and pattern on a byte by byte basis, even for multi-byte
95
while ((tlen > 0) && (plen > 0))
99
/* Next pattern byte must match literally, whatever it is */
101
/* ... and there had better be one, per SQL standard */
104
(errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
105
errmsg("LIKE pattern must not end with escape character")));
106
if (TCHAR(*p) != TCHAR(*t))
112
* % processing is essentially a search for a match for what
113
* follows the %, plus a recursive match of the remainder. We
114
* succeed if and only if both conditions are met.
117
/* %% is the same as % according to the SQL standard */
118
/* Advance past all %'s */
119
while ((plen > 0) && (*p == '%'))
121
/* Trailing percent matches everything. */
126
* Otherwise, scan for a text position at which we can match the
127
* rest of the pattern.
132
/* %_ is the same as _% - avoid matching _ repeatedly */
139
return (plen <= 0) ? LIKE_TRUE : LIKE_ABORT;
148
int matched = MatchText(t, tlen, p, plen);
150
if (matched != LIKE_FALSE)
151
return matched; /* TRUE or ABORT */
159
char firstpat = TCHAR(*p);
165
firstpat = TCHAR(p[1]);
171
* Optimization to prevent most recursion: don't recurse
172
* unless first pattern byte matches first text byte.
174
if (TCHAR(*t) == firstpat)
176
int matched = MatchText(t, tlen, p, plen);
178
if (matched != LIKE_FALSE)
179
return matched; /* TRUE or ABORT */
188
* End of text with no match, so no point in trying later places
189
* to start matching this pattern.
199
else if (TCHAR(*t) != TCHAR(*p))
202
* Not the single-character wildcard and no explicit match? Then
209
* It is safe to use NextByte instead of NextChar here, even for
210
* multi-byte character sets, because we are not following immediately
211
* after a wildcard character. If we are in the middle of a multibyte
212
* character, we must already have matched at least one byte of the
213
* character from both text and pattern; so we cannot get out-of-sync
214
* on character boundaries. And we know that no backend-legal
215
* encoding allows ASCII characters such as '%' to appear as non-first
216
* bytes of characters, so we won't mistakenly detect a new wildcard.
223
return LIKE_FALSE; /* end of pattern, but not of text */
225
/* End of input string. Do we have matching pattern remaining? */
226
while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
234
* End of text with no match, so no point in trying later places to start
235
* matching this pattern.
241
* like_escape() --- given a pattern and an ESCAPE string,
242
* convert the pattern to use Postgres' standard backslash escape convention.
244
#ifdef do_like_escape
247
do_like_escape(text *pat, text *esc)
257
p = VARDATA_ANY(pat);
258
plen = VARSIZE_ANY_EXHDR(pat);
259
e = VARDATA_ANY(esc);
260
elen = VARSIZE_ANY_EXHDR(esc);
263
* Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
264
* trying to calculate the size more accurately than that.
266
result = (text *) palloc(plen * 2 + VARHDRSZ);
272
* No escape character is wanted. Double any backslashes in the
273
* pattern to make them act like ordinary characters.
279
CopyAdvChar(r, p, plen);
285
* The specified escape must be only a single character.
290
(errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
291
errmsg("invalid escape string"),
292
errhint("Escape string must be empty or one character.")));
294
e = VARDATA_ANY(esc);
297
* If specified escape is '\', just copy the pattern as-is.
301
memcpy(result, pat, VARSIZE_ANY(pat));
306
* Otherwise, convert occurrences of the specified escape character to
307
* '\', and double occurrences of '\' --- unless they immediately
308
* follow an escape character!
313
if (CHAREQ(p, e) && !afterescape)
329
CopyAdvChar(r, p, plen);
335
SET_VARSIZE(result, r - ((char *) result));
339
#endif /* do_like_escape */
349
#ifdef do_like_escape
350
#undef do_like_escape