1 /*-------------------------------------------------------------------------
4 * like expression handling code.
6 * Copyright (c) 1994, Regents of the University of California
10 * /usr/local/devel/pglite/cvs/src/backend/utils/adt/like.c,v 1.1 1995/07/30 23:55:36 emkxp01 Exp
14 * A big hack of the regexp.c code!! Contributed by
15 * Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
18 *-------------------------------------------------------------------------
21 #include "postgres.h" /* postgres system include file */
22 #include "utils/palloc.h"
23 #include "utils/builtins.h" /* where the function declarations go */
25 static int like(char *text, char *p);
28 * interface routines called by the function manager
34 a generic fixed length like routine
35 s - the string to match against (not necessarily null-terminated)
37 charlen - the length of the string
40 fixedlen_like(char *s, struct varlena * p, int charlen)
49 /* be sure sterm is null-terminated */
50 sterm = (char *) palloc(charlen + 1);
51 StrNCpy(sterm, s, charlen + 1);
54 * p is a text = varlena, not a string so we have to make a string
55 * from the vl_data field of the struct.
58 /* palloc the length of the text + the null character */
59 pterm = (char *) palloc(VARSIZE(p) - VARHDRSZ + 1);
60 memmove(pterm, VARDATA(p), VARSIZE(p) - VARHDRSZ);
61 *(pterm + VARSIZE(p) - VARHDRSZ) = (char) NULL;
63 /* do the regexp matching */
64 result = like(sterm, pterm);
69 return ((bool) result);
73 char2like(uint16 arg1, struct varlena * p)
75 char *s = (char *) &arg1;
77 return (fixedlen_like(s, p, 2));
81 char2nlike(uint16 arg1, struct varlena * p)
83 return (!char2like(arg1, p));
87 char4like(uint32 arg1, struct varlena * p)
89 char *s = (char *) &arg1;
91 return (fixedlen_like(s, p, 4));
95 char4nlike(uint32 arg1, struct varlena * p)
97 return (!char4like(arg1, p));
101 char8like(char *s, struct varlena * p)
103 return (fixedlen_like(s, p, 8));
107 char8nlike(char *s, struct varlena * p)
109 return (!char8like(s, p));
113 char16like(char *s, struct varlena * p)
115 return (fixedlen_like(s, p, 16));
119 char16nlike(char *s, struct varlena * p)
121 return (!char16like(s, p));
125 namelike(NameData *n, struct varlena * p)
129 return (fixedlen_like(n->data, p, NAMEDATALEN));
133 namenlike(NameData *s, struct varlena * p)
135 return (!namelike(s, p));
139 textlike(struct varlena * s, struct varlena * p)
143 return (fixedlen_like(VARDATA(s), p, VARSIZE(s) - VARHDRSZ));
147 textnlike(struct varlena * s, struct varlena * p)
149 return (!textlike(s, p));
154 ** "like.c" A first attempt at a LIKE operator for Postgres95.
156 ** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
157 ** Rich $alz is now <rsalz@bbn.com>.
158 ** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
160 ** This code was shamelessly stolen from the "pql" code by myself and
161 ** slightly modified :)
163 ** All references to the word "star" were replaced by "percent"
164 ** All references to the word "wild" were replaced by "like"
166 ** All the nice shell RE matching stuff was replaced by just "_" and "%"
168 ** As I don't have a copy of the SQL standard handy I wasn't sure whether
169 ** to leave in the '\' escape character handling. (I suspect the standard
170 ** handles "%%" as a single literal percent)
172 ** Keith Parks. <keith@mtcc.demon.co.uk>
174 ** [SQL92 lets you specify the escape character by saying
175 ** LIKE <pattern> ESCAPE <escape character>. We are a small operation
176 ** so we force you to use '\'. - ay 7/95]
182 #define LIKE_ABORT -1
185 ** Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
188 DoMatch(char *text, char *p)
192 for (; *p; text ++, p++)
194 if (*text == '\0' && *p != '%')
199 /* Literal match with following character. */
207 /* Match anything. */
211 /* Consecutive percents act just like one. */
214 /* Trailing percent matches everything. */
217 if ((matched = DoMatch(text ++, p)) != LIKE_FALSE)
223 return *text == '\0';
228 ** User-level routine. Returns TRUE or FALSE.
231 like(char *text, char *p)
233 if (p[0] == '%' && p[1] == '\0')
235 return (DoMatch(text, p) == LIKE_TRUE);