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 "mb/pg_wchar.h"
22 #include "utils/builtins.h"
24 static int like(pg_wchar * text, pg_wchar * p);
27 * interface routines called by the function manager
33 a generic fixed length like routine
34 s - the string to match against (not necessarily null-terminated)
36 charlen - the length of the string
39 fixedlen_like(char *s, struct varlena * p, int charlen)
49 /* be sure sterm is null-terminated */
51 sterm = (pg_wchar *) palloc((charlen + 1) * sizeof(pg_wchar));
52 (void) pg_mb2wchar_with_len((unsigned char *) s, sterm, charlen);
54 sterm = (char *) palloc(charlen + 1);
55 StrNCpy(sterm, s, charlen + 1);
59 * p is a text = varlena, not a string so we have to make a string
60 * from the vl_data field of the struct.
63 /* palloc the length of the text + the null character */
64 len = VARSIZE(p) - VARHDRSZ;
66 pterm = (pg_wchar *) palloc((len + 1) * sizeof(pg_wchar));
67 (void) pg_mb2wchar_with_len((unsigned char *) VARDATA(p), pterm, len);
69 pterm = (char *) palloc(len + 1);
70 memmove(pterm, VARDATA(p), len);
71 *(pterm + len) = (char) NULL;
74 /* do the regexp matching */
75 result = like(sterm, pterm);
84 namelike(NameData *n, struct varlena * p)
88 return fixedlen_like(n->data, p, NAMEDATALEN);
92 namenlike(NameData *s, struct varlena * p)
94 return !namelike(s, p);
98 textlike(struct varlena * s, struct varlena * p)
102 return fixedlen_like(VARDATA(s), p, VARSIZE(s) - VARHDRSZ);
106 textnlike(struct varlena * s, struct varlena * p)
108 return !textlike(s, p);
113 ** "like.c" A first attempt at a LIKE operator for Postgres95.
115 ** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
116 ** Rich $alz is now <rsalz@bbn.com>.
117 ** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
119 ** This code was shamelessly stolen from the "pql" code by myself and
120 ** slightly modified :)
122 ** All references to the word "star" were replaced by "percent"
123 ** All references to the word "wild" were replaced by "like"
125 ** All the nice shell RE matching stuff was replaced by just "_" and "%"
127 ** As I don't have a copy of the SQL standard handy I wasn't sure whether
128 ** to leave in the '\' escape character handling. (I suspect the standard
129 ** handles "%%" as a single literal percent)
131 ** Keith Parks. <keith@mtcc.demon.co.uk>
133 ** [SQL92 lets you specify the escape character by saying
134 ** LIKE <pattern> ESCAPE <escape character>. We are a small operation
135 ** so we force you to use '\'. - ay 7/95]
141 #define LIKE_ABORT -1
144 ** Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
147 DoMatch(pg_wchar * text, pg_wchar * p)
151 for (; *p && *text; text ++, p++)
156 /* Literal match with following character. */
164 /* Match anything. */
167 /* %% is the same as % according to the SQL standard */
168 /* Advance past all %'s */
172 /* Trailing percent matches everything. */
176 /* Optimization to prevent most recursion */
178 *p == '\\' || *p == '%' || *p == '_') &&
179 (matched = DoMatch(text, p)) != LIKE_FALSE)
191 /* End of input string. Do we have matching string remaining? */
192 while (*p == '%') /* allow multiple %'s at end of pattern */
203 ** User-level routine. Returns TRUE or FALSE.
206 like(pg_wchar * text, pg_wchar * p)
208 if (p[0] == '%' && p[1] == '\0')
210 return DoMatch(text, p) == LIKE_TRUE;