* A big hack of the regexp.c code!! Contributed by
* Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
*
- * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
+ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.41 2000/08/22 06:33:57 ishii Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.54 2003/07/27 04:53:06 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
+
#include <ctype.h>
-#ifdef MULTIBYTE
+
#include "mb/pg_wchar.h"
-#endif
#include "utils/builtins.h"
#define LIKE_ABORT (-1)
-static int MatchText(unsigned char * t, int tlen, unsigned char * p, int plen, char *e);
-static int MatchTextLower(unsigned char * t, int tlen, unsigned char * p, int plen, char *e);
+static int MatchText(unsigned char *t, int tlen,
+ unsigned char *p, int plen);
+static int MatchTextIC(unsigned char *t, int tlen,
+ unsigned char *p, int plen);
+static int MatchBytea(unsigned char *t, int tlen,
+ unsigned char *p, int plen);
+static text *do_like_escape(text *, text *);
+static int MBMatchText(unsigned char *t, int tlen,
+ unsigned char *p, int plen);
+static int MBMatchTextIC(unsigned char *t, int tlen,
+ unsigned char *p, int plen);
+static text *MB_do_like_escape(text *, text *);
-/*
- * interface routines called by the function manager
+/*--------------------
+ * Support routine for MatchText. Compares given multibyte streams
+ * as wide characters. If they match, returns 1 otherwise returns 0.
+ *--------------------
*/
+static int
+wchareq(unsigned char *p1, unsigned char *p2)
+{
+ int l;
-Datum
-namelike(PG_FUNCTION_ARGS)
+ l = pg_mblen(p1);
+ if (pg_mblen(p2) != l)
+ return (0);
+ while (l--)
+ {
+ if (*p1++ != *p2++)
+ return (0);
+ }
+ return (1);
+}
+
+/*--------------------
+ * Support routine for MatchTextIC. Compares given multibyte streams
+ * as wide characters ignoring case.
+ * If they match, returns 1 otherwise returns 0.
+ *--------------------
+ */
+#define CHARMAX 0x80
+
+static int
+iwchareq(unsigned char *p1, unsigned char *p2)
{
- bool result;
- Name str = PG_GETARG_NAME(0);
- text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
+ int c1[2],
+ c2[2];
+ int l;
- s = NameStr(*str);
- slen = strlen(s);
- p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
+ /*
+ * short cut. if *p1 and *p2 is lower than CHARMAX, then we could
+ * assume they are ASCII
+ */
+ if (*p1 < CHARMAX && *p2 < CHARMAX)
+ return (tolower(*p1) == tolower(*p2));
- result = (MatchText(s, slen, p, plen, "\\") == LIKE_TRUE);
+ /*
+ * if one of them is an ASCII while the other is not, then they must
+ * be different characters
+ */
+ else if (*p1 < CHARMAX || *p2 < CHARMAX)
+ return (0);
- PG_RETURN_BOOL(result);
+ /*
+ * ok, p1 and p2 are both > CHARMAX, then they must be multibyte
+ * characters
+ */
+ l = pg_mblen(p1);
+ (void) pg_mb2wchar_with_len(p1, (pg_wchar *) c1, l);
+ c1[0] = tolower(c1[0]);
+ l = pg_mblen(p2);
+ (void) pg_mb2wchar_with_len(p2, (pg_wchar *) c2, l);
+ c2[0] = tolower(c2[0]);
+ return (c1[0] == c2[0]);
}
-Datum
-namenlike(PG_FUNCTION_ARGS)
-{
- bool result;
- Name str = PG_GETARG_NAME(0);
- text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
+#define CHAREQ(p1, p2) wchareq(p1, p2)
+#define ICHAREQ(p1, p2) iwchareq(p1, p2)
+#define NextChar(p, plen) \
+ do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)
+#define CopyAdvChar(dst, src, srclen) \
+ do { int __l = pg_mblen(src); \
+ (srclen) -= __l; \
+ while (__l-- > 0) \
+ *(dst)++ = *(src)++; \
+ } while (0)
+
+#define MatchText MBMatchText
+#define MatchTextIC MBMatchTextIC
+#define do_like_escape MB_do_like_escape
+#include "like_match.c"
+#undef CHAREQ
+#undef ICHAREQ
+#undef NextChar
+#undef CopyAdvChar
+#undef MatchText
+#undef MatchTextIC
+#undef do_like_escape
- s = NameStr(*str);
- slen = strlen(s);
- p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
+#define CHAREQ(p1, p2) (*(p1) == *(p2))
+#define ICHAREQ(p1, p2) (tolower(*(p1)) == tolower(*(p2)))
+#define NextChar(p, plen) ((p)++, (plen)--)
+#define CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
- result = (MatchText(s, slen, p, plen, "\\") != LIKE_TRUE);
+#define BYTEA_CHAREQ(p1, p2) (*(p1) == *(p2))
+#define BYTEA_NextChar(p, plen) ((p)++, (plen)--)
+#define BYTEA_CopyAdvChar(dst, src, srclen) (*(dst)++ = *(src)++, (srclen)--)
+#include "like_match.c"
- PG_RETURN_BOOL(result);
-}
+/*
+ * interface routines called by the function manager
+ */
Datum
-namelike_escape(PG_FUNCTION_ARGS)
+namelike(PG_FUNCTION_ARGS)
{
- bool result;
Name str = PG_GETARG_NAME(0);
text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = NameStr(*str);
slen = strlen(s);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchText(s, slen, p, plen, e) == LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchText(s, slen, p, plen) == LIKE_TRUE);
+ else
+ result = (MBMatchText(s, slen, p, plen) == LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
-namenlike_escape(PG_FUNCTION_ARGS)
+namenlike(PG_FUNCTION_ARGS)
{
- bool result;
Name str = PG_GETARG_NAME(0);
text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = NameStr(*str);
slen = strlen(s);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchText(s, slen, p, plen, e) != LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchText(s, slen, p, plen) != LIKE_TRUE);
+ else
+ result = (MBMatchText(s, slen, p, plen) != LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
textlike(PG_FUNCTION_ARGS)
{
- bool result;
text *str = PG_GETARG_TEXT_P(0);
text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
+ slen = (VARSIZE(str) - VARHDRSZ);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchText(s, slen, p, plen, NULL) == LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchText(s, slen, p, plen) == LIKE_TRUE);
+ else
+ result = (MBMatchText(s, slen, p, plen) == LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
textnlike(PG_FUNCTION_ARGS)
{
- bool result;
text *str = PG_GETARG_TEXT_P(0);
text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
+ slen = (VARSIZE(str) - VARHDRSZ);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchText(s, slen, p, plen, "\\") != LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchText(s, slen, p, plen) != LIKE_TRUE);
+ else
+ result = (MBMatchText(s, slen, p, plen) != LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
-textlike_escape(PG_FUNCTION_ARGS)
+bytealike(PG_FUNCTION_ARGS)
{
+ bytea *str = PG_GETARG_BYTEA_P(0);
+ bytea *pat = PG_GETARG_BYTEA_P(1);
bool result;
- text *str = PG_GETARG_TEXT_P(0);
- text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
+ slen = (VARSIZE(str) - VARHDRSZ);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchText(s, slen, p, plen, e) == LIKE_TRUE);
+ result = (MatchBytea(s, slen, p, plen) == LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
-textnlike_escape(PG_FUNCTION_ARGS)
+byteanlike(PG_FUNCTION_ARGS)
{
+ bytea *str = PG_GETARG_BYTEA_P(0);
+ bytea *pat = PG_GETARG_BYTEA_P(1);
bool result;
- text *str = PG_GETARG_TEXT_P(0);
- text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
+ slen = (VARSIZE(str) - VARHDRSZ);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchText(s, slen, p, plen, e) != LIKE_TRUE);
+ result = (MatchBytea(s, slen, p, plen) != LIKE_TRUE);
PG_RETURN_BOOL(result);
}
*/
Datum
-inamelike(PG_FUNCTION_ARGS)
+nameiclike(PG_FUNCTION_ARGS)
{
- bool result;
Name str = PG_GETARG_NAME(0);
text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
-
- s = NameStr(*str);
- slen = strlen(s);
- p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
-
- result = (MatchTextLower(s, slen, p, plen, "\\") == LIKE_TRUE);
-
- PG_RETURN_BOOL(result);
-}
-
-Datum
-inamenlike(PG_FUNCTION_ARGS)
-{
bool result;
- Name str = PG_GETARG_NAME(0);
- text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = NameStr(*str);
slen = strlen(s);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchTextLower(s, slen, p, plen, "\\") != LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchTextIC(s, slen, p, plen) == LIKE_TRUE);
+ else
+ result = (MBMatchTextIC(s, slen, p, plen) == LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
-inamelike_escape(PG_FUNCTION_ARGS)
+nameicnlike(PG_FUNCTION_ARGS)
{
- bool result;
Name str = PG_GETARG_NAME(0);
text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
-
- s = NameStr(*str);
- slen = strlen(s);
- p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
-
- result = (MatchTextLower(s, slen, p, plen, e) == LIKE_TRUE);
-
- PG_RETURN_BOOL(result);
-}
-
-Datum
-inamenlike_escape(PG_FUNCTION_ARGS)
-{
bool result;
- Name str = PG_GETARG_NAME(0);
- text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = NameStr(*str);
slen = strlen(s);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchTextLower(s, slen, p, plen, e) != LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchTextIC(s, slen, p, plen) != LIKE_TRUE);
+ else
+ result = (MBMatchTextIC(s, slen, p, plen) != LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
-itextlike(PG_FUNCTION_ARGS)
+texticlike(PG_FUNCTION_ARGS)
{
- bool result;
text *str = PG_GETARG_TEXT_P(0);
text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
-
- s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
- p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
-
- result = (MatchTextLower(s, slen, p, plen, "\\") == LIKE_TRUE);
-
- PG_RETURN_BOOL(result);
-}
-
-Datum
-itextnlike(PG_FUNCTION_ARGS)
-{
bool result;
- text *str = PG_GETARG_TEXT_P(0);
- text *pat = PG_GETARG_TEXT_P(1);
- unsigned char *s, *p;
- int slen, plen;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
+ slen = (VARSIZE(str) - VARHDRSZ);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchTextLower(s, slen, p, plen, "\\") != LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchTextIC(s, slen, p, plen) == LIKE_TRUE);
+ else
+ result = (MBMatchTextIC(s, slen, p, plen) == LIKE_TRUE);
PG_RETURN_BOOL(result);
}
Datum
-itextlike_escape(PG_FUNCTION_ARGS)
+texticnlike(PG_FUNCTION_ARGS)
{
- bool result;
text *str = PG_GETARG_TEXT_P(0);
text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
+ bool result;
+ unsigned char *s,
+ *p;
+ int slen,
+ plen;
s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
+ slen = (VARSIZE(str) - VARHDRSZ);
p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ plen = (VARSIZE(pat) - VARHDRSZ);
- result = (MatchTextLower(s, slen, p, plen, e) == LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = (MatchTextIC(s, slen, p, plen) != LIKE_TRUE);
+ else
+ result = (MBMatchTextIC(s, slen, p, plen) != LIKE_TRUE);
PG_RETURN_BOOL(result);
}
+/*
+ * like_escape() --- given a pattern and an ESCAPE string,
+ * convert the pattern to use Postgres' standard backslash escape convention.
+ */
Datum
-itextnlike_escape(PG_FUNCTION_ARGS)
+like_escape(PG_FUNCTION_ARGS)
{
- bool result;
- text *str = PG_GETARG_TEXT_P(0);
- text *pat = PG_GETARG_TEXT_P(1);
- text *esc = PG_GETARG_TEXT_P(2);
- unsigned char *s, *p;
- int slen, plen;
- char *e;
-
- s = VARDATA(str);
- slen = (VARSIZE(str)-VARHDRSZ);
- p = VARDATA(pat);
- plen = (VARSIZE(pat)-VARHDRSZ);
- e = ((VARSIZE(esc)-VARHDRSZ) > 0? VARDATA(esc): NULL);
+ text *pat = PG_GETARG_TEXT_P(0);
+ text *esc = PG_GETARG_TEXT_P(1);
+ text *result;
- result = (MatchTextLower(s, slen, p, plen, e) != LIKE_TRUE);
+ if (pg_database_encoding_max_length() == 1)
+ result = do_like_escape(pat, esc);
+ else
+ result = MB_do_like_escape(pat, esc);
- PG_RETURN_BOOL(result);
+ PG_RETURN_TEXT_P(result);
}
-
/*
-** 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>
-**
-** [SQL92 lets you specify the escape character by saying
-** LIKE <pattern> ESCAPE <escape character>. We are a small operation
-** so we force you to use '\'. - ay 7/95]
-**
-** OK, we now support the SQL9x LIKE <pattern> ESCAPE <char> syntax.
-** We should kill the backslash escaping mechanism since it is non-standard
-** and undocumented afaik.
-** 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
-**
-*/
-
-/*--------------------
- * Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
- *
- * 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.
- *--------------------
+ * like_escape_bytea() --- given a pattern and an ESCAPE string,
+ * convert the pattern to use Postgres' standard backslash escape convention.
*/
-
-#ifdef MULTIBYTE
-/*--------------------
- * Support routine for MatchText. Compares given multibyte streams
- * as wide characters. If they match, returns 1 otherwise returns 0.
- *--------------------
- */
-static int wchareq(unsigned char *p1, unsigned char *p2)
+Datum
+like_escape_bytea(PG_FUNCTION_ARGS)
{
- int l;
+ bytea *pat = PG_GETARG_BYTEA_P(0);
+ bytea *esc = PG_GETARG_BYTEA_P(1);
+ bytea *result;
+ unsigned char *p,
+ *e,
+ *r;
+ int plen,
+ elen;
+ bool afterescape;
- l = pg_mblen(p1);
- if (pg_mblen(p2) != l) {
- return(0);
- }
- while (l--) {
- if (*p1++ != *p2++)
- return(0);
- }
- return(1);
-}
-
-/*--------------------
- * Support routine for MatchTextLower. Compares given multibyte streams
- * as wide characters ignoring case.
- * If they match, returns 1 otherwise returns 0.
- *--------------------
- */
-#define UCHARMAX 0xff
-
-static int iwchareq(unsigned char *p1, unsigned char *p2)
-{
- int c1, c2;
- int l;
+ p = VARDATA(pat);
+ plen = (VARSIZE(pat) - VARHDRSZ);
+ e = VARDATA(esc);
+ elen = (VARSIZE(esc) - VARHDRSZ);
- /* short cut. if *p1 and *p2 is lower than UCHARMAX, then
- we assume they are ASCII */
- if (*p1 < UCHARMAX && *p2 < UCHARMAX)
- return(tolower(*p1) == tolower(*p2));
+ /*
+ * 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 (*p1 < UCHARMAX)
- c1 = tolower(*p1);
- else
+ if (elen == 0)
{
- l = pg_mblen(p1);
- (void)pg_mb2wchar_with_len(p1, (pg_wchar *)&c1, l);
- c1 = tolower(c1);
+ /*
+ * No escape character is wanted. Double any backslashes in the
+ * pattern to make them act like ordinary characters.
+ */
+ while (plen > 0)
+ {
+ if (*p == '\\')
+ *r++ = '\\';
+ BYTEA_CopyAdvChar(r, p, plen);
+ }
}
- if (*p2 < UCHARMAX)
- c2 = tolower(*p2);
else
{
- l = pg_mblen(p2);
- (void)pg_mb2wchar_with_len(p2, (pg_wchar *)&c2, l);
- c2 = tolower(c2);
- }
- return(c1 == c2);
-}
-#endif
-
-#ifdef MULTIBYTE
-#define CHAREQ(p1, p2) wchareq(p1, p2)
-#define ICHAREQ(p1, p2) iwchareq(p1, p2)
-#define NextChar(p, plen) {int __l = pg_mblen(p); (p) +=__l; (plen) -=__l;}
-#else
-#define CHAREQ(p1, p2) (*(p1) == *(p2))
-#define ICHAREQ(p1, p2) (tolower(*(p1)) == tolower(*(p2)))
-#define NextChar(p, plen) (p)++, (plen)--
-#endif
+ /*
+ * The specified escape must be only a single character.
+ */
+ BYTEA_NextChar(e, elen);
+ if (elen != 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
+ errmsg("invalid escape string"),
+ errhint("Escape string must be empty or one character.")));
-static int
-MatchText(unsigned char * t, int tlen, unsigned char * p, int plen, char *e)
-{
- /* Fast path for match-everything pattern
- * Include weird case of escape character as a percent sign or underscore,
- * when presumably that wildcard character becomes a literal.
- */
- if ((plen == 1) && (*p == '%')
- && ! ((e != NULL) && (*e == '%')))
- return LIKE_TRUE;
+ e = VARDATA(esc);
- while ((tlen > 0) && (plen > 0))
- {
- /* If an escape character was specified and we find it here in the pattern,
- * then we'd better have an exact match for the next character.
+ /*
+ * If specified escape is '\', just copy the pattern as-is.
*/
- if ((e != NULL) && CHAREQ(p,e))
+ if (*e == '\\')
{
- NextChar(p, plen);
- if ((plen <= 0) || !CHAREQ(t,p))
- return LIKE_FALSE;
+ memcpy(result, pat, VARSIZE(pat));
+ PG_RETURN_BYTEA_P(result);
}
- else if (*p == '%')
- {
- /* %% is the same as % according to the SQL standard */
- /* Advance past all %'s */
- while ((plen > 0) && (*p == '%'))
- NextChar(p, plen);
- /* Trailing percent matches everything. */
- if (plen <= 0)
- return LIKE_TRUE;
- /*
- * Otherwise, scan for a text position at which we can
- * match the rest of the pattern.
- */
- while (tlen > 0)
+ /*
+ * Otherwise, convert occurrences of the specified escape
+ * character to '\', and double occurrences of '\' --- unless they
+ * immediately follow an escape character!
+ */
+ afterescape = false;
+ while (plen > 0)
+ {
+ if (BYTEA_CHAREQ(p, e) && !afterescape)
{
- /*
- * Optimization to prevent most recursion: don't
- * recurse unless first pattern char might match this
- * text char.
- */
- if (CHAREQ(t,p) || (*p == '_')
- || ((e != NULL) && CHAREQ(p,e)))
- {
- int matched = MatchText(t, tlen, p, plen, e);
-
- if (matched != LIKE_FALSE)
- return matched; /* TRUE or ABORT */
- }
-
- NextChar(t, tlen);
+ *r++ = '\\';
+ BYTEA_NextChar(p, plen);
+ afterescape = true;
+ }
+ else if (*p == '\\')
+ {
+ *r++ = '\\';
+ if (!afterescape)
+ *r++ = '\\';
+ BYTEA_NextChar(p, plen);
+ afterescape = false;
+ }
+ else
+ {
+ BYTEA_CopyAdvChar(r, p, plen);
+ afterescape = false;
}
-
- /*
- * End of text with no match, so no point in trying later
- * places to start matching this pattern.
- */
- return LIKE_ABORT;
- }
- else if ((*p != '_') && !CHAREQ(t,p))
- {
- /* Not the single-character wildcard and no explicit match?
- * Then time to quit...
- */
- return LIKE_FALSE;
}
-
- NextChar(t, tlen);
- NextChar(p, plen);
}
- if (tlen > 0)
- return LIKE_FALSE; /* end of pattern, but not of text */
+ VARATT_SIZEP(result) = r - ((unsigned char *) result);
- /* End of input string. Do we have matching pattern remaining? */
- while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of pattern */
- NextChar(p, plen);
- if (plen <= 0)
- return LIKE_TRUE;
-
- /*
- * End of text with no match, so no point in trying later places to
- * start matching this pattern.
- */
- return LIKE_ABORT;
-} /* MatchText() */
+ PG_RETURN_BYTEA_P(result);
+}
+/*
+ * Same as above, but specifically for bytea (binary) datatype
+ */
static int
-MatchTextLower(unsigned char * t, int tlen, unsigned char * p, int plen, char *e)
+MatchBytea(unsigned char *t, int tlen, unsigned char *p, int plen)
{
- /* Fast path for match-everything pattern
- * Include weird case of escape character as a percent sign or underscore,
- * when presumably that wildcard character becomes a literal.
- */
- if ((plen == 1) && (*p == '%')
- && ! ((e != NULL) && (*e == '%')))
+ /* Fast path for match-everything pattern */
+ if ((plen == 1) && (*p == '%'))
return LIKE_TRUE;
while ((tlen > 0) && (plen > 0))
{
- /* If an escape character was specified and we find it here in the pattern,
- * then we'd better have an exact match for the next character.
- */
- if ((e != NULL) && ICHAREQ(p,e))
+ if (*p == '\\')
{
- NextChar(p, plen);
- if ((plen <= 0) || !ICHAREQ(t,p))
+ /* Next pattern char must match literally, whatever it is */
+ BYTEA_NextChar(p, plen);
+ if ((plen <= 0) || !BYTEA_CHAREQ(t, p))
return LIKE_FALSE;
}
else if (*p == '%')
/* %% is the same as % according to the SQL standard */
/* Advance past all %'s */
while ((plen > 0) && (*p == '%'))
- NextChar(p, plen);
+ BYTEA_NextChar(p, plen);
/* Trailing percent matches everything. */
if (plen <= 0)
return LIKE_TRUE;
/*
- * Otherwise, scan for a text position at which we can
- * match the rest of the pattern.
+ * Otherwise, scan for a text position at which we can match
+ * the rest of the pattern.
*/
while (tlen > 0)
{
/*
- * Optimization to prevent most recursion: don't
- * recurse unless first pattern char might match this
- * text char.
+ * Optimization to prevent most recursion: don't recurse
+ * unless first pattern char might match this text char.
*/
- if (ICHAREQ(t,p) || (*p == '_')
- || ((e != NULL) && ICHAREQ(p,e)))
+ if (BYTEA_CHAREQ(t, p) || (*p == '\\') || (*p == '_'))
{
- int matched = MatchText(t, tlen, p, plen, e);
+ int matched = MatchBytea(t, tlen, p, plen);
if (matched != LIKE_FALSE)
- return matched; /* TRUE or ABORT */
+ return matched; /* TRUE or ABORT */
}
- NextChar(t, tlen);
+ BYTEA_NextChar(t, tlen);
}
/*
*/
return LIKE_ABORT;
}
- else if ((*p != '_') && !ICHAREQ(t,p))
+ else if ((*p != '_') && !BYTEA_CHAREQ(t, p))
{
+ /*
+ * Not the single-character wildcard and no explicit match?
+ * Then time to quit...
+ */
return LIKE_FALSE;
}
- NextChar(t, tlen);
- NextChar(p, plen);
+ BYTEA_NextChar(t, tlen);
+ BYTEA_NextChar(p, plen);
}
if (tlen > 0)
return LIKE_FALSE; /* end of pattern, but not of text */
/* End of input string. Do we have matching pattern remaining? */
- while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of pattern */
- NextChar(p, plen);
+ while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of
+ * pattern */
+ BYTEA_NextChar(p, plen);
if (plen <= 0)
return LIKE_TRUE;
* start matching this pattern.
*/
return LIKE_ABORT;
-} /* MatchTextLower() */
+} /* MatchBytea() */