PostgreSQL Source Code git master
like_match.c File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GETCHAR(t)   (t)
 

Functions

static int MatchText (const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
 

Macro Definition Documentation

◆ GETCHAR

#define GETCHAR (   t)    (t)

Definition at line 80 of file like_match.c.

Function Documentation

◆ MatchText()

static int MatchText ( const char *  t,
int  tlen,
const char *  p,
int  plen,
pg_locale_t  locale 
)
static

Definition at line 84 of file like_match.c.

85{
86 /* Fast path for match-everything pattern */
87 if (plen == 1 && *p == '%')
88 return LIKE_TRUE;
89
90 /* Since this function recurses, it could be driven to stack overflow */
92
93 /*
94 * In this loop, we advance by char when matching wildcards (and thus on
95 * recursive entry to this function we are properly char-synced). On other
96 * occasions it is safe to advance by byte, as the text and pattern will
97 * be in lockstep. This allows us to perform all comparisons between the
98 * text and pattern on a byte by byte basis, even for multi-byte
99 * encodings.
100 */
101 while (tlen > 0 && plen > 0)
102 {
103 if (*p == '\\')
104 {
105 /* Next pattern byte must match literally, whatever it is */
106 NextByte(p, plen);
107 /* ... and there had better be one, per SQL standard */
108 if (plen <= 0)
110 (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
111 errmsg("LIKE pattern must not end with escape character")));
112 if (GETCHAR(*p) != GETCHAR(*t))
113 return LIKE_FALSE;
114 }
115 else if (*p == '%')
116 {
117 char firstpat;
118
119 /*
120 * % processing is essentially a search for a text position at
121 * which the remainder of the text matches the remainder of the
122 * pattern, using a recursive call to check each potential match.
123 *
124 * If there are wildcards immediately following the %, we can skip
125 * over them first, using the idea that any sequence of N _'s and
126 * one or more %'s is equivalent to N _'s and one % (ie, it will
127 * match any sequence of at least N text characters). In this way
128 * we will always run the recursive search loop using a pattern
129 * fragment that begins with a literal character-to-match, thereby
130 * not recursing more than we have to.
131 */
132 NextByte(p, plen);
133
134 while (plen > 0)
135 {
136 if (*p == '%')
137 NextByte(p, plen);
138 else if (*p == '_')
139 {
140 /* If not enough text left to match the pattern, ABORT */
141 if (tlen <= 0)
142 return LIKE_ABORT;
143 NextChar(t, tlen);
144 NextByte(p, plen);
145 }
146 else
147 break; /* Reached a non-wildcard pattern char */
148 }
149
150 /*
151 * If we're at end of pattern, match: we have a trailing % which
152 * matches any remaining text string.
153 */
154 if (plen <= 0)
155 return LIKE_TRUE;
156
157 /*
158 * Otherwise, scan for a text position at which we can match the
159 * rest of the pattern. The first remaining pattern char is known
160 * to be a regular or escaped literal character, so we can compare
161 * the first pattern byte to each text byte to avoid recursing
162 * more than we have to. This fact also guarantees that we don't
163 * have to consider a match to the zero-length substring at the
164 * end of the text. With a nondeterministic collation, we can't
165 * rely on the first bytes being equal, so we have to recurse in
166 * any case.
167 */
168 if (*p == '\\')
169 {
170 if (plen < 2)
172 (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
173 errmsg("LIKE pattern must not end with escape character")));
174 firstpat = GETCHAR(p[1]);
175 }
176 else
177 firstpat = GETCHAR(*p);
178
179 while (tlen > 0)
180 {
181 if (GETCHAR(*t) == firstpat || (locale && !locale->deterministic))
182 {
183 int matched = MatchText(t, tlen, p, plen, locale);
184
185 if (matched != LIKE_FALSE)
186 return matched; /* TRUE or ABORT */
187 }
188
189 NextChar(t, tlen);
190 }
191
192 /*
193 * End of text with no match, so no point in trying later places
194 * to start matching this pattern.
195 */
196 return LIKE_ABORT;
197 }
198 else if (*p == '_')
199 {
200 /* _ matches any single character, and we know there is one */
201 NextChar(t, tlen);
202 NextByte(p, plen);
203 continue;
204 }
205 else if (locale && !locale->deterministic)
206 {
207 /*
208 * For nondeterministic locales, we find the next substring of the
209 * pattern that does not contain wildcards and try to find a
210 * matching substring in the text. Crucially, we cannot do this
211 * character by character, as in the normal case, but must do it
212 * substring by substring, partitioned by the wildcard characters.
213 * (This is per SQL standard.)
214 */
215 const char *p1;
216 size_t p1len;
217 const char *t1;
218 size_t t1len;
219 bool found_escape;
220 const char *subpat;
221 size_t subpatlen;
222 char *buf = NULL;
223
224 /*
225 * Determine next substring of pattern without wildcards. p is
226 * the start of the subpattern, p1 is one past the last byte. Also
227 * track if we found an escape character.
228 */
229 p1 = p;
230 p1len = plen;
231 found_escape = false;
232 while (p1len > 0)
233 {
234 if (*p1 == '\\')
235 {
236 found_escape = true;
237 NextByte(p1, p1len);
238 if (p1len == 0)
240 (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
241 errmsg("LIKE pattern must not end with escape character")));
242 }
243 else if (*p1 == '_' || *p1 == '%')
244 break;
245 NextByte(p1, p1len);
246 }
247
248 /*
249 * If we found an escape character, then make an unescaped copy of
250 * the subpattern.
251 */
252 if (found_escape)
253 {
254 char *b;
255
256 b = buf = palloc(p1 - p);
257 for (const char *c = p; c < p1; c++)
258 {
259 if (*c == '\\')
260 ;
261 else
262 *(b++) = *c;
263 }
264
265 subpat = buf;
266 subpatlen = b - buf;
267 }
268 else
269 {
270 subpat = p;
271 subpatlen = p1 - p;
272 }
273
274 /*
275 * Shortcut: If this is the end of the pattern, then the rest of
276 * the text has to match the rest of the pattern.
277 */
278 if (p1len == 0)
279 {
280 int cmp;
281
282 cmp = pg_strncoll(subpat, subpatlen, t, tlen, locale);
283
284 if (buf)
285 pfree(buf);
286 if (cmp == 0)
287 return LIKE_TRUE;
288 else
289 return LIKE_FALSE;
290 }
291
292 /*
293 * Now build a substring of the text and try to match it against
294 * the subpattern. t is the start of the text, t1 is one past the
295 * last byte. We start with a zero-length string.
296 */
297 t1 = t;
298 t1len = tlen;
299 for (;;)
300 {
301 int cmp;
302
304
305 cmp = pg_strncoll(subpat, subpatlen, t, (t1 - t), locale);
306
307 /*
308 * If we found a match, we have to test if the rest of pattern
309 * can match against the rest of the string. Otherwise we
310 * have to continue here try matching with a longer substring.
311 * (This is similar to the recursion for the '%' wildcard
312 * above.)
313 *
314 * Note that we can't just wind forward p and t and continue
315 * with the main loop. This would fail for example with
316 *
317 * U&'\0061\0308bc' LIKE U&'\00E4_c' COLLATE ignore_accents
318 *
319 * You'd find that t=\0061 matches p=\00E4, but then the rest
320 * won't match; but t=\0061\0308 also matches p=\00E4, and
321 * then the rest will match.
322 */
323 if (cmp == 0)
324 {
325 int matched = MatchText(t1, t1len, p1, p1len, locale);
326
327 if (matched == LIKE_TRUE)
328 {
329 if (buf)
330 pfree(buf);
331 return matched;
332 }
333 }
334
335 /*
336 * Didn't match. If we used up the whole text, then the match
337 * fails. Otherwise, try again with a longer substring.
338 */
339 if (t1len == 0)
340 {
341 if (buf)
342 pfree(buf);
343 return LIKE_FALSE;
344 }
345 else
346 NextChar(t1, t1len);
347 }
348 }
349 else if (GETCHAR(*p) != GETCHAR(*t))
350 {
351 /* non-wildcard pattern char fails to match text char */
352 return LIKE_FALSE;
353 }
354
355 /*
356 * Pattern and text match, so advance.
357 *
358 * It is safe to use NextByte instead of NextChar here, even for
359 * multi-byte character sets, because we are not following immediately
360 * after a wildcard character. If we are in the middle of a multibyte
361 * character, we must already have matched at least one byte of the
362 * character from both text and pattern; so we cannot get out-of-sync
363 * on character boundaries. And we know that no backend-legal
364 * encoding allows ASCII characters such as '%' to appear as non-first
365 * bytes of characters, so we won't mistakenly detect a new wildcard.
366 */
367 NextByte(t, tlen);
368 NextByte(p, plen);
369 }
370
371 if (tlen > 0)
372 return LIKE_FALSE; /* end of pattern, but not of text */
373
374 /*
375 * End of text, but perhaps not of pattern. Match iff the remaining
376 * pattern can match a zero-length string, ie, it's zero or more %'s.
377 */
378 while (plen > 0 && *p == '%')
379 NextByte(p, plen);
380 if (plen <= 0)
381 return LIKE_TRUE;
382
383 /*
384 * End of text with no match, so no point in trying later places to start
385 * matching this pattern.
386 */
387 return LIKE_ABORT;
388} /* MatchText() */
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
static char * locale
Definition: initdb.c:140
int b
Definition: isn.c:74
#define LIKE_ABORT
Definition: like.c:32
#define LIKE_TRUE
Definition: like.c:30
#define LIKE_FALSE
Definition: like.c:31
#define NextByte(p, plen)
Definition: like.c:93
#define NextChar(p, plen)
Definition: like.c:130
static int MatchText(const char *t, int tlen, const char *p, int plen, pg_locale_t locale)
Definition: like_match.c:84
#define GETCHAR(t)
Definition: like_match.c:80
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc(Size size)
Definition: mcxt.c:1365
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
int pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
Definition: pg_locale.c:1381
static char buf[DEFAULT_XLOG_SEG_SIZE]
Definition: pg_test_fsync.c:71
char * c
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
void check_stack_depth(void)
Definition: stack_depth.c:95

References b, buf, CHECK_FOR_INTERRUPTS, check_stack_depth(), cmp(), ereport, errcode(), errmsg(), ERROR, GETCHAR, LIKE_ABORT, LIKE_FALSE, LIKE_TRUE, locale, MatchText(), NextByte, NextChar, palloc(), pfree(), and pg_strncoll().

Referenced by MatchText().