1 /*-------------------------------------------------------------------------
4 * like expression handling internal code.
6 * This file is included by like.c *twice* if multibyte is enabled.
7 * This is for an optimization of single byte encodings.
8 * Before the inclusion, we need to define following macros:
14 * MatchText (MBMatchText)
15 * MatchTextIC (MBMatchTextIC)
16 * do_like_escape (MB_do_like_escape)
18 * Copyright (c) 1996-2001, PostgreSQL Global Development Group
21 * $Header: /cvsroot/pgsql/src/backend/utils/adt/like_match.c,v 1.2 2001/10/25 05:49:44 momjian Exp $
23 *-------------------------------------------------------------------------
27 ** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
28 ** Rich $alz is now <rsalz@bbn.com>.
29 ** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
31 ** This code was shamelessly stolen from the "pql" code by myself and
32 ** slightly modified :)
34 ** All references to the word "star" were replaced by "percent"
35 ** All references to the word "wild" were replaced by "like"
37 ** All the nice shell RE matching stuff was replaced by just "_" and "%"
39 ** As I don't have a copy of the SQL standard handy I wasn't sure whether
40 ** to leave in the '\' escape character handling.
42 ** Keith Parks. <keith@mtcc.demon.co.uk>
44 ** SQL92 lets you specify the escape character by saying
45 ** LIKE <pattern> ESCAPE <escape character>. We are a small operation
46 ** so we force you to use '\'. - ay 7/95
48 ** Now we have the like_escape() function that converts patterns with
49 ** any specified escape character (or none at all) to the internal
50 ** default escape character, which is still '\'. - tgl 9/2000
52 ** The code is rewritten to avoid requiring null-terminated strings,
53 ** which in turn allows us to leave out some memcpy() operations.
54 ** This code should be faster and take less memory, but no promises...
55 ** - thomas 2000-08-06
60 /*--------------------
61 * Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
63 * LIKE_TRUE: they match
64 * LIKE_FALSE: they don't match
65 * LIKE_ABORT: not only don't they match, but the text is too short.
67 * If LIKE_ABORT is returned, then no suffix of the text can match the
68 * pattern either, so an upper-level % scan can stop scanning now.
73 MatchText(unsigned char *t, int tlen, unsigned char *p, int plen)
75 /* Fast path for match-everything pattern */
76 if ((plen == 1) && (*p == '%'))
79 while ((tlen > 0) && (plen > 0))
83 /* Next pattern char must match literally, whatever it is */
85 if ((plen <= 0) || !CHAREQ(t, p))
90 /* %% is the same as % according to the SQL standard */
91 /* Advance past all %'s */
92 while ((plen > 0) && (*p == '%'))
94 /* Trailing percent matches everything. */
99 * Otherwise, scan for a text position at which we can match
100 * the rest of the pattern.
105 * Optimization to prevent most recursion: don't recurse
106 * unless first pattern char might match this text char.
108 if (CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
110 int matched = MatchText(t, tlen, p, plen);
112 if (matched != LIKE_FALSE)
113 return matched; /* TRUE or ABORT */
120 * End of text with no match, so no point in trying later
121 * places to start matching this pattern.
125 else if ((*p != '_') && !CHAREQ(t, p))
128 * Not the single-character wildcard and no explicit match?
129 * Then time to quit...
139 return LIKE_FALSE; /* end of pattern, but not of text */
141 /* End of input string. Do we have matching pattern remaining? */
142 while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
149 * End of text with no match, so no point in trying later places to
150 * start matching this pattern.
156 * Same as above, but ignore case
159 MatchTextIC(unsigned char *t, int tlen, unsigned char *p, int plen)
161 /* Fast path for match-everything pattern */
162 if ((plen == 1) && (*p == '%'))
165 while ((tlen > 0) && (plen > 0))
169 /* Next pattern char must match literally, whatever it is */
171 if ((plen <= 0) || !ICHAREQ(t, p))
176 /* %% is the same as % according to the SQL standard */
177 /* Advance past all %'s */
178 while ((plen > 0) && (*p == '%'))
180 /* Trailing percent matches everything. */
185 * Otherwise, scan for a text position at which we can match
186 * the rest of the pattern.
191 * Optimization to prevent most recursion: don't recurse
192 * unless first pattern char might match this text char.
194 if (ICHAREQ(t, p) || (*p == '\\') || (*p == '_'))
196 int matched = MatchTextIC(t, tlen, p, plen);
198 if (matched != LIKE_FALSE)
199 return matched; /* TRUE or ABORT */
206 * End of text with no match, so no point in trying later
207 * places to start matching this pattern.
211 else if ((*p != '_') && !ICHAREQ(t, p))
214 * Not the single-character wildcard and no explicit match?
215 * Then time to quit...
225 return LIKE_FALSE; /* end of pattern, but not of text */
227 /* End of input string. Do we have matching pattern remaining? */
228 while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
235 * End of text with no match, so no point in trying later places to
236 * start matching this pattern.
239 } /* MatchTextIC() */
242 * like_escape() --- given a pattern and an ESCAPE string,
243 * convert the pattern to use Postgres' standard backslash escape convention.
246 do_like_escape(text *pat, text *esc)
257 plen = (VARSIZE(pat) - VARHDRSZ);
259 elen = (VARSIZE(esc) - VARHDRSZ);
262 * Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
263 * trying to calculate the size more accurately than that.
265 result = (text *) palloc(plen * 2 + VARHDRSZ);
271 * No escape character is wanted. Double any backslashes in the
272 * pattern to make them act like ordinary characters.
278 CopyAdvChar(r, p, plen);
284 * The specified escape must be only a single character.
288 elog(ERROR, "ESCAPE string must be empty or one character");
292 * If specified escape is '\', just copy the pattern as-is.
296 memcpy(result, pat, VARSIZE(pat));
301 * Otherwise, convert occurrences of the specified escape
302 * character to '\', and double occurrences of '\' --- unless they
303 * immediately follow an escape character!
308 if (CHAREQ(p, e) && !afterescape)
324 CopyAdvChar(r, p, plen);
330 VARATT_SIZEP(result) = r - ((unsigned char *) result);