1 /*-------------------------------------------------------------------------
4 * like expression handling internal code.
6 * This file is included by like.c *twice*, to provide an optimization
7 * for single-byte encodings.
9 * Before the inclusion, we need to define following macros:
15 * MatchText (MBMatchText)
16 * MatchTextIC (MBMatchTextIC)
17 * do_like_escape (MB_do_like_escape)
19 * Copyright (c) 1996-2006, PostgreSQL Global Development Group
22 * $PostgreSQL: pgsql/src/backend/utils/adt/like_match.c,v 1.13 2006/03/05 15:58:42 momjian Exp $
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 MatchText(char *t, int tlen, char *p, int plen)
76 /* Fast path for match-everything pattern */
77 if ((plen == 1) && (*p == '%'))
80 while ((tlen > 0) && (plen > 0))
84 /* Next pattern char must match literally, whatever it is */
86 if ((plen <= 0) || !CHAREQ(t, p))
91 /* %% is the same as % according to the SQL standard */
92 /* Advance past all %'s */
93 while ((plen > 0) && (*p == '%'))
95 /* Trailing percent matches everything. */
100 * Otherwise, scan for a text position at which we can match the
101 * rest of the pattern.
106 * Optimization to prevent most recursion: don't recurse
107 * unless first pattern char might match this text char.
109 if (CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
111 int matched = MatchText(t, tlen, p, plen);
113 if (matched != LIKE_FALSE)
114 return matched; /* TRUE or ABORT */
121 * End of text with no match, so no point in trying later places
122 * to start matching this pattern.
126 else if ((*p != '_') && !CHAREQ(t, p))
129 * Not the single-character wildcard and no explicit match? Then
140 return LIKE_FALSE; /* end of pattern, but not of text */
142 /* End of input string. Do we have matching pattern remaining? */
143 while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
150 * End of text with no match, so no point in trying later places to start
151 * matching this pattern.
157 * Same as above, but ignore case
160 MatchTextIC(char *t, int tlen, char *p, int plen)
162 /* Fast path for match-everything pattern */
163 if ((plen == 1) && (*p == '%'))
166 while ((tlen > 0) && (plen > 0))
170 /* Next pattern char must match literally, whatever it is */
172 if ((plen <= 0) || !ICHAREQ(t, p))
177 /* %% is the same as % according to the SQL standard */
178 /* Advance past all %'s */
179 while ((plen > 0) && (*p == '%'))
181 /* Trailing percent matches everything. */
186 * Otherwise, scan for a text position at which we can match the
187 * rest of the pattern.
192 * Optimization to prevent most recursion: don't recurse
193 * unless first pattern char might match this text char.
195 if (ICHAREQ(t, p) || (*p == '\\') || (*p == '_'))
197 int matched = MatchTextIC(t, tlen, p, plen);
199 if (matched != LIKE_FALSE)
200 return matched; /* TRUE or ABORT */
207 * End of text with no match, so no point in trying later places
208 * to start matching this pattern.
212 else if ((*p != '_') && !ICHAREQ(t, p))
215 * Not the single-character wildcard and no explicit match? Then
226 return LIKE_FALSE; /* end of pattern, but not of text */
228 /* End of input string. Do we have matching pattern remaining? */
229 while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
236 * End of text with no match, so no point in trying later places to start
237 * matching this pattern.
240 } /* MatchTextIC() */
243 * like_escape() --- given a pattern and an ESCAPE string,
244 * convert the pattern to use Postgres' standard backslash escape convention.
247 do_like_escape(text *pat, text *esc)
258 plen = (VARSIZE(pat) - VARHDRSZ);
260 elen = (VARSIZE(esc) - VARHDRSZ);
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.")));
297 * If specified escape is '\', just copy the pattern as-is.
301 memcpy(result, pat, VARSIZE(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 VARATT_SIZEP(result) = r - ((char *) result);