2001-10-04 04:15:47 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* like_match.c
|
2008-09-26 04:16:40 +02:00
|
|
|
* LIKE pattern matching internal code.
|
2001-10-04 04:15:47 +02:00
|
|
|
*
|
2008-09-26 04:16:40 +02:00
|
|
|
* This file is included by like.c four times, to provide matching code for
|
|
|
|
* (1) single-byte encodings, (2) UTF8, (3) other multi-byte encodings,
|
2011-04-09 23:12:39 +02:00
|
|
|
* and (4) case insensitive matches in single-byte encodings.
|
2008-09-26 04:16:40 +02:00
|
|
|
* (UTF8 is a special case because we can use a much more efficient version
|
|
|
|
* of NextChar than can be used for general multi-byte encodings.)
|
2002-09-03 23:45:44 +02:00
|
|
|
*
|
2009-05-24 20:10:38 +02:00
|
|
|
* Before the inclusion, we need to define the following macros:
|
2001-10-04 04:15:47 +02:00
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* NextChar
|
2007-06-02 04:03:42 +02:00
|
|
|
* MatchText - to name of function wanted
|
|
|
|
* do_like_escape - name of function if wanted - needs CHAREQ and CopyAdvChar
|
2011-04-09 23:12:39 +02:00
|
|
|
* MATCH_LOWER - define for case (4) to specify case folding for 1-byte chars
|
2001-10-04 04:15:47 +02:00
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
2001-10-04 04:15:47 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/like_match.c
|
2001-10-04 04:15:47 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-05-24 20:10:38 +02:00
|
|
|
* Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
|
|
|
|
* Rich $alz is now <rsalz@bbn.com>.
|
|
|
|
* Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
|
|
|
|
*
|
|
|
|
* This code was shamelessly stolen from the "pql" code by myself and
|
|
|
|
* slightly modified :)
|
|
|
|
*
|
|
|
|
* All references to the word "star" were replaced by "percent"
|
|
|
|
* All references to the word "wild" were replaced by "like"
|
|
|
|
*
|
|
|
|
* All the nice shell RE matching stuff was replaced by just "_" and "%"
|
|
|
|
*
|
|
|
|
* As I don't have a copy of the SQL standard handy I wasn't sure whether
|
|
|
|
* to leave in the '\' escape character handling.
|
|
|
|
*
|
|
|
|
* Keith Parks. <keith@mtcc.demon.co.uk>
|
|
|
|
*
|
2013-04-20 17:04:41 +02:00
|
|
|
* SQL lets you specify the escape character by saying
|
2009-05-24 20:10:38 +02:00
|
|
|
* LIKE <pattern> ESCAPE <escape character>. We are a small operation
|
|
|
|
* so we force you to use '\'. - ay 7/95
|
|
|
|
*
|
|
|
|
* Now we have the like_escape() function that converts patterns with
|
|
|
|
* any specified escape character (or none at all) to the internal
|
|
|
|
* default escape character, which is still '\'. - tgl 9/2000
|
|
|
|
*
|
|
|
|
* The code is rewritten to avoid requiring null-terminated strings,
|
|
|
|
* which in turn allows us to leave out some memcpy() operations.
|
|
|
|
* This code should be faster and take less memory, but no promises...
|
|
|
|
* - thomas 2000-08-06
|
|
|
|
*/
|
2001-10-04 04:15:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*--------------------
|
2009-05-24 20:10:38 +02:00
|
|
|
* Match text and pattern, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
|
2001-10-04 04:15:47 +02:00
|
|
|
*
|
|
|
|
* LIKE_TRUE: they match
|
|
|
|
* LIKE_FALSE: they don't match
|
|
|
|
* LIKE_ABORT: not only don't they match, but the text is too short.
|
|
|
|
*
|
|
|
|
* If LIKE_ABORT is returned, then no suffix of the text can match the
|
|
|
|
* pattern either, so an upper-level % scan can stop scanning now.
|
|
|
|
*--------------------
|
|
|
|
*/
|
|
|
|
|
2007-09-22 05:58:34 +02:00
|
|
|
#ifdef MATCH_LOWER
|
2011-04-09 23:12:39 +02:00
|
|
|
#define GETCHAR(t) MATCH_LOWER(t)
|
2007-09-22 05:58:34 +02:00
|
|
|
#else
|
2010-05-28 19:35:23 +02:00
|
|
|
#define GETCHAR(t) (t)
|
2007-09-22 05:58:34 +02:00
|
|
|
#endif
|
|
|
|
|
2001-10-04 04:15:47 +02:00
|
|
|
static int
|
2011-04-09 23:12:39 +02:00
|
|
|
MatchText(char *t, int tlen, char *p, int plen,
|
|
|
|
pg_locale_t locale, bool locale_is_c)
|
2001-10-04 04:15:47 +02:00
|
|
|
{
|
|
|
|
/* Fast path for match-everything pattern */
|
2009-05-24 20:10:38 +02:00
|
|
|
if (plen == 1 && *p == '%')
|
2001-10-04 04:15:47 +02:00
|
|
|
return LIKE_TRUE;
|
|
|
|
|
2015-10-02 21:00:51 +02:00
|
|
|
/* Since this function recurses, it could be driven to stack overflow */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2007-06-02 04:03:42 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* In this loop, we advance by char when matching wildcards (and thus on
|
|
|
|
* recursive entry to this function we are properly char-synced). On other
|
|
|
|
* occasions it is safe to advance by byte, as the text and pattern will
|
2009-05-24 20:10:38 +02:00
|
|
|
* be in lockstep. This allows us to perform all comparisons between the
|
2007-11-15 22:14:46 +01:00
|
|
|
* text and pattern on a byte by byte basis, even for multi-byte
|
2007-06-02 04:03:42 +02:00
|
|
|
* encodings.
|
|
|
|
*/
|
2009-05-24 20:10:38 +02:00
|
|
|
while (tlen > 0 && plen > 0)
|
2001-10-04 04:15:47 +02:00
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
{
|
2008-09-26 04:16:40 +02:00
|
|
|
/* Next pattern byte must match literally, whatever it is */
|
2007-06-02 04:03:42 +02:00
|
|
|
NextByte(p, plen);
|
2008-09-26 04:16:40 +02:00
|
|
|
/* ... and there had better be one, per SQL standard */
|
|
|
|
if (plen <= 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
|
2009-06-11 16:49:15 +02:00
|
|
|
errmsg("LIKE pattern must not end with escape character")));
|
2010-05-28 19:35:23 +02:00
|
|
|
if (GETCHAR(*p) != GETCHAR(*t))
|
2001-10-04 04:15:47 +02:00
|
|
|
return LIKE_FALSE;
|
|
|
|
}
|
|
|
|
else if (*p == '%')
|
|
|
|
{
|
2010-05-28 19:35:23 +02:00
|
|
|
char firstpat;
|
|
|
|
|
2007-06-02 04:03:42 +02:00
|
|
|
/*
|
2010-05-28 19:35:23 +02:00
|
|
|
* % processing is essentially a search for a text position at
|
|
|
|
* which the remainder of the text matches the remainder of the
|
|
|
|
* pattern, using a recursive call to check each potential match.
|
|
|
|
*
|
|
|
|
* If there are wildcards immediately following the %, we can skip
|
|
|
|
* over them first, using the idea that any sequence of N _'s and
|
|
|
|
* one or more %'s is equivalent to N _'s and one % (ie, it will
|
2010-07-06 21:19:02 +02:00
|
|
|
* match any sequence of at least N text characters). In this way
|
|
|
|
* we will always run the recursive search loop using a pattern
|
|
|
|
* fragment that begins with a literal character-to-match, thereby
|
|
|
|
* not recursing more than we have to.
|
2007-06-02 04:03:42 +02:00
|
|
|
*/
|
2010-05-28 19:35:23 +02:00
|
|
|
NextByte(p, plen);
|
2007-06-02 04:03:42 +02:00
|
|
|
|
2010-05-28 19:35:23 +02:00
|
|
|
while (plen > 0)
|
|
|
|
{
|
|
|
|
if (*p == '%')
|
|
|
|
NextByte(p, plen);
|
|
|
|
else if (*p == '_')
|
|
|
|
{
|
|
|
|
/* If not enough text left to match the pattern, ABORT */
|
|
|
|
if (tlen <= 0)
|
|
|
|
return LIKE_ABORT;
|
|
|
|
NextChar(t, tlen);
|
|
|
|
NextByte(p, plen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break; /* Reached a non-wildcard pattern char */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're at end of pattern, match: we have a trailing % which
|
|
|
|
* matches any remaining text string.
|
|
|
|
*/
|
2001-10-04 04:15:47 +02:00
|
|
|
if (plen <= 0)
|
|
|
|
return LIKE_TRUE;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Otherwise, scan for a text position at which we can match the
|
2010-05-28 19:35:23 +02:00
|
|
|
* rest of the pattern. The first remaining pattern char is known
|
|
|
|
* to be a regular or escaped literal character, so we can compare
|
|
|
|
* the first pattern byte to each text byte to avoid recursing
|
|
|
|
* more than we have to. This fact also guarantees that we don't
|
|
|
|
* have to consider a match to the zero-length substring at the
|
|
|
|
* end of the text.
|
2001-10-04 04:15:47 +02:00
|
|
|
*/
|
2010-05-28 19:35:23 +02:00
|
|
|
if (*p == '\\')
|
2007-06-02 04:03:42 +02:00
|
|
|
{
|
2010-05-28 19:35:23 +02:00
|
|
|
if (plen < 2)
|
2010-05-28 20:18:19 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
|
|
|
|
errmsg("LIKE pattern must not end with escape character")));
|
2010-05-28 19:35:23 +02:00
|
|
|
firstpat = GETCHAR(p[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
firstpat = GETCHAR(*p);
|
2001-10-04 04:15:47 +02:00
|
|
|
|
2010-05-28 19:35:23 +02:00
|
|
|
while (tlen > 0)
|
|
|
|
{
|
|
|
|
if (GETCHAR(*t) == firstpat)
|
2007-06-02 04:03:42 +02:00
|
|
|
{
|
2011-04-09 23:12:39 +02:00
|
|
|
int matched = MatchText(t, tlen, p, plen,
|
|
|
|
locale, locale_is_c);
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-06-02 04:03:42 +02:00
|
|
|
if (matched != LIKE_FALSE)
|
2010-07-06 21:19:02 +02:00
|
|
|
return matched; /* TRUE or ABORT */
|
2007-06-02 04:03:42 +02:00
|
|
|
}
|
2001-10-04 04:15:47 +02:00
|
|
|
|
2010-05-28 19:35:23 +02:00
|
|
|
NextChar(t, tlen);
|
2001-10-04 04:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* End of text with no match, so no point in trying later places
|
|
|
|
* to start matching this pattern.
|
2001-10-04 04:15:47 +02:00
|
|
|
*/
|
|
|
|
return LIKE_ABORT;
|
|
|
|
}
|
2007-06-02 04:03:42 +02:00
|
|
|
else if (*p == '_')
|
|
|
|
{
|
2009-05-24 20:10:38 +02:00
|
|
|
/* _ matches any single character, and we know there is one */
|
2007-06-02 04:03:42 +02:00
|
|
|
NextChar(t, tlen);
|
|
|
|
NextByte(p, plen);
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-28 19:35:23 +02:00
|
|
|
else if (GETCHAR(*p) != GETCHAR(*t))
|
2001-10-04 04:15:47 +02:00
|
|
|
{
|
2009-05-24 20:10:38 +02:00
|
|
|
/* non-wildcard pattern char fails to match text char */
|
2001-10-04 04:15:47 +02:00
|
|
|
return LIKE_FALSE;
|
|
|
|
}
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-06-02 04:03:42 +02:00
|
|
|
/*
|
2009-05-24 20:10:38 +02:00
|
|
|
* Pattern and text match, so advance.
|
|
|
|
*
|
2007-06-02 04:03:42 +02:00
|
|
|
* It is safe to use NextByte instead of NextChar here, even for
|
2007-11-15 22:14:46 +01:00
|
|
|
* multi-byte character sets, because we are not following immediately
|
|
|
|
* after a wildcard character. If we are in the middle of a multibyte
|
|
|
|
* character, we must already have matched at least one byte of the
|
|
|
|
* character from both text and pattern; so we cannot get out-of-sync
|
|
|
|
* on character boundaries. And we know that no backend-legal
|
|
|
|
* encoding allows ASCII characters such as '%' to appear as non-first
|
|
|
|
* bytes of characters, so we won't mistakenly detect a new wildcard.
|
2007-06-02 04:03:42 +02:00
|
|
|
*/
|
|
|
|
NextByte(t, tlen);
|
|
|
|
NextByte(p, plen);
|
2001-10-04 04:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tlen > 0)
|
|
|
|
return LIKE_FALSE; /* end of pattern, but not of text */
|
|
|
|
|
2010-05-28 19:35:23 +02:00
|
|
|
/*
|
|
|
|
* End of text, but perhaps not of pattern. Match iff the remaining
|
|
|
|
* pattern can match a zero-length string, ie, it's zero or more %'s.
|
|
|
|
*/
|
|
|
|
while (plen > 0 && *p == '%')
|
2007-06-02 04:03:42 +02:00
|
|
|
NextByte(p, plen);
|
2001-10-04 04:15:47 +02:00
|
|
|
if (plen <= 0)
|
|
|
|
return LIKE_TRUE;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* End of text with no match, so no point in trying later places to start
|
|
|
|
* matching this pattern.
|
2001-10-04 04:15:47 +02:00
|
|
|
*/
|
|
|
|
return LIKE_ABORT;
|
2007-06-02 04:03:42 +02:00
|
|
|
} /* MatchText() */
|
2001-10-04 04:15:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* like_escape() --- given a pattern and an ESCAPE string,
|
|
|
|
* convert the pattern to use Postgres' standard backslash escape convention.
|
|
|
|
*/
|
2007-06-02 04:03:42 +02:00
|
|
|
#ifdef do_like_escape
|
|
|
|
|
2001-10-04 04:15:47 +02:00
|
|
|
static text *
|
|
|
|
do_like_escape(text *pat, text *esc)
|
|
|
|
{
|
|
|
|
text *result;
|
2005-09-24 19:53:28 +02:00
|
|
|
char *p,
|
2001-10-04 04:15:47 +02:00
|
|
|
*e,
|
|
|
|
*r;
|
|
|
|
int plen,
|
|
|
|
elen;
|
|
|
|
bool afterescape;
|
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
p = VARDATA_ANY(pat);
|
|
|
|
plen = VARSIZE_ANY_EXHDR(pat);
|
|
|
|
e = VARDATA_ANY(esc);
|
|
|
|
elen = VARSIZE_ANY_EXHDR(esc);
|
2001-10-04 04:15:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Worst-case pattern growth is 2x --- unlikely, but it's hardly worth
|
|
|
|
* trying to calculate the size more accurately than that.
|
|
|
|
*/
|
|
|
|
result = (text *) palloc(plen * 2 + VARHDRSZ);
|
|
|
|
r = VARDATA(result);
|
|
|
|
|
|
|
|
if (elen == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No escape character is wanted. Double any backslashes in the
|
|
|
|
* pattern to make them act like ordinary characters.
|
|
|
|
*/
|
|
|
|
while (plen > 0)
|
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
*r++ = '\\';
|
|
|
|
CopyAdvChar(r, p, plen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The specified escape must be only a single character.
|
|
|
|
*/
|
|
|
|
NextChar(e, elen);
|
|
|
|
if (elen != 0)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
|
|
|
|
errmsg("invalid escape string"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("Escape string must be empty or one character.")));
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2007-09-22 00:52:52 +02:00
|
|
|
e = VARDATA_ANY(esc);
|
2001-10-04 04:15:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If specified escape is '\', just copy the pattern as-is.
|
|
|
|
*/
|
|
|
|
if (*e == '\\')
|
|
|
|
{
|
2007-09-22 00:52:52 +02:00
|
|
|
memcpy(result, pat, VARSIZE_ANY(pat));
|
2001-10-04 04:15:47 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Otherwise, convert occurrences of the specified escape character to
|
|
|
|
* '\', and double occurrences of '\' --- unless they immediately
|
|
|
|
* follow an escape character!
|
2001-10-04 04:15:47 +02:00
|
|
|
*/
|
|
|
|
afterescape = false;
|
|
|
|
while (plen > 0)
|
|
|
|
{
|
|
|
|
if (CHAREQ(p, e) && !afterescape)
|
|
|
|
{
|
|
|
|
*r++ = '\\';
|
|
|
|
NextChar(p, plen);
|
|
|
|
afterescape = true;
|
|
|
|
}
|
|
|
|
else if (*p == '\\')
|
|
|
|
{
|
|
|
|
*r++ = '\\';
|
|
|
|
if (!afterescape)
|
|
|
|
*r++ = '\\';
|
|
|
|
NextChar(p, plen);
|
|
|
|
afterescape = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CopyAdvChar(r, p, plen);
|
|
|
|
afterescape = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(result, r - ((char *) result));
|
2001-10-04 04:15:47 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2007-11-15 22:14:46 +01:00
|
|
|
#endif /* do_like_escape */
|
2007-06-02 04:03:42 +02:00
|
|
|
|
|
|
|
#ifdef CHAREQ
|
|
|
|
#undef CHAREQ
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef NextChar
|
|
|
|
#undef CopyAdvChar
|
|
|
|
#undef MatchText
|
|
|
|
|
|
|
|
#ifdef do_like_escape
|
|
|
|
#undef do_like_escape
|
|
|
|
#endif
|
|
|
|
|
2010-05-28 19:35:23 +02:00
|
|
|
#undef GETCHAR
|
2007-09-22 05:58:34 +02:00
|
|
|
|
|
|
|
#ifdef MATCH_LOWER
|
|
|
|
#undef MATCH_LOWER
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2007-09-22 05:58:34 +02:00
|
|
|
#endif
|