1 /*-------------------------------------------------------------------------
4 * like expression handling code.
7 * A big hack of the regexp.c code!! Contributed by
8 * Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
10 * Copyright (c) 1994, Regents of the University of California
13 * $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.32 1999/11/07 23:08:23 momjian Exp $
15 *-------------------------------------------------------------------------
18 #include "mb/pg_wchar.h"
19 #include "utils/builtins.h"
21 static int like(pg_wchar * text, pg_wchar * p);
24 * interface routines called by the function manager
30 a generic fixed length like routine
31 s - the string to match against (not necessarily null-terminated)
33 charlen - the length of the string
36 fixedlen_like(char *s, struct varlena * p, int charlen)
46 /* be sure sterm is null-terminated */
48 sterm = (pg_wchar *) palloc((charlen + 1) * sizeof(pg_wchar));
49 (void) pg_mb2wchar_with_len((unsigned char *) s, sterm, charlen);
51 sterm = (char *) palloc(charlen + 1);
52 StrNCpy(sterm, s, charlen + 1);
56 * p is a text = varlena, not a string so we have to make a string
57 * from the vl_data field of the struct.
60 /* palloc the length of the text + the null character */
61 len = VARSIZE(p) - VARHDRSZ;
63 pterm = (pg_wchar *) palloc((len + 1) * sizeof(pg_wchar));
64 (void) pg_mb2wchar_with_len((unsigned char *) VARDATA(p), pterm, len);
66 pterm = (char *) palloc(len + 1);
67 memmove(pterm, VARDATA(p), len);
68 *(pterm + len) = (char) NULL;
71 /* do the regexp matching */
72 result = like(sterm, pterm);
81 namelike(NameData *n, struct varlena * p)
85 return fixedlen_like(NameStr(*n), p, NAMEDATALEN);
89 namenlike(NameData *s, struct varlena * p)
91 return !namelike(s, p);
95 textlike(struct varlena * s, struct varlena * p)
99 return fixedlen_like(VARDATA(s), p, VARSIZE(s) - VARHDRSZ);
103 textnlike(struct varlena * s, struct varlena * p)
105 return !textlike(s, p);
110 ** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
111 ** Rich $alz is now <rsalz@bbn.com>.
112 ** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
114 ** This code was shamelessly stolen from the "pql" code by myself and
115 ** slightly modified :)
117 ** All references to the word "star" were replaced by "percent"
118 ** All references to the word "wild" were replaced by "like"
120 ** All the nice shell RE matching stuff was replaced by just "_" and "%"
122 ** As I don't have a copy of the SQL standard handy I wasn't sure whether
123 ** to leave in the '\' escape character handling.
125 ** Keith Parks. <keith@mtcc.demon.co.uk>
127 ** [SQL92 lets you specify the escape character by saying
128 ** LIKE <pattern> ESCAPE <escape character>. We are a small operation
129 ** so we force you to use '\'. - ay 7/95]
135 #define LIKE_ABORT -1
137 /*--------------------
138 * Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
140 * LIKE_TRUE: they match
141 * LIKE_FALSE: they don't match
142 * LIKE_ABORT: not only don't they match, but the text is too short.
144 * If LIKE_ABORT is returned, then no suffix of the text can match the
145 * pattern either, so an upper-level % scan can stop scanning now.
146 *--------------------
149 DoMatch(pg_wchar * text, pg_wchar * p)
151 for (; *p && *text; text++, p++)
156 /* Literal match with following character. */
164 /* Match any single character. */
167 /* %% is the same as % according to the SQL standard */
168 /* Advance past all %'s */
171 /* Trailing percent matches everything. */
174 /* Otherwise, scan for a text position at which we
175 * can match the rest of the pattern.
177 for (; *text; text++)
179 /* Optimization to prevent most recursion: don't recurse
180 * unless first pattern char might match this text char.
182 if (*text == *p || *p == '\\' || *p == '_')
184 int matched = DoMatch(text, p);
185 if (matched != LIKE_FALSE)
186 return matched; /* TRUE or ABORT */
189 /* End of text with no match, so no point in trying later
190 * places to start matching this pattern.
197 return LIKE_FALSE; /* end of pattern, but not of text */
199 /* End of input string. Do we have matching pattern remaining? */
200 while (*p == '%') /* allow multiple %'s at end of pattern */
204 /* End of text with no match, so no point in trying later
205 * places to start matching this pattern.
211 ** User-level routine. Returns TRUE or FALSE.
214 like(pg_wchar * text, pg_wchar * p)
216 /* Fast path for match-everything pattern */
217 if (p[0] == '%' && p[1] == '\0')
219 return DoMatch(text, p) == LIKE_TRUE;