1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*-------------------------------------------------------------------------
*
* regexp.c
* regular expression handling code.
*
* 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/regexp.c,v 1.43 2002/09/22 17:27:23 tgl Exp $
*
* Alistair Crooks added the code for the regex caching
* agc - cached the regular expressions used - there's a good chance
* that we'll get a hit, so this saves a compile step for every
* attempted match. I haven't actually measured the speed improvement,
* but it `looks' a lot quicker visually when watching regression
* test output.
*
* agc - incorporated Keith Bostic's Berkeley regex code into
* the tree for all ports. To distinguish this regex code from any that
* is existent on a platform, I've prepended the string "pg_" to
* the functions regcomp, regerror, regexec and regfree.
* Fixed a bug that was originally a typo by me, where `i' was used
* instead of `oldest' when compiling regular expressions - benign
* results mostly, although occasionally it bit you...
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "regex/regex.h"
#include "utils/builtins.h"
#if defined(DISABLE_XOPEN_NLS)
#undef _XOPEN_SOURCE
#endif /* DISABLE_XOPEN_NLS */
/* this is the number of cached regular expressions held. */
#ifndef MAX_CACHED_RES
#define MAX_CACHED_RES 32
#endif
/* this structure describes a cached regular expression */
struct cached_re_str
{
char *cre_s; /* pattern as null-terminated string */
int cre_type; /* compiled-type: extended,icase etc */
regex_t cre_re; /* the compiled regular expression */
unsigned long cre_lru; /* lru tag */
};
static int rec = 0; /* # of cached re's */
static struct cached_re_str rev[MAX_CACHED_RES]; /* cached re's */
static unsigned long lru; /* system lru tag */
static int pg_lastrec = 0;
/* attempt to compile `re' as an re, then match it against text */
/* cflags - flag to regcomp indicates case sensitivity */
static bool
RE_compile_and_execute(text *text_re, char *text, int cflags,
int nmatch, regmatch_t *pmatch)
{
char *re;
int oldest;
int i;
int regcomp_result;
/* Convert 'text' pattern to null-terminated string */
re = DatumGetCString(DirectFunctionCall1(textout,
PointerGetDatum(text_re)));
/*
* Find a previously compiled regular expression. Run the cache as a
* ring buffer, starting the search from the previous match if any.
*/
i = pg_lastrec;
while (i < rec)
{
if (rev[i].cre_s != NULL)
{
if (strcmp(rev[i].cre_s, re) == 0 &&
rev[i].cre_type == cflags)
{
pg_lastrec = i;
rev[i].cre_lru = ++lru;
pfree(re);
return (pg_regexec(&rev[i].cre_re,
text, nmatch,
pmatch, 0) == 0);
}
}
i++;
/*
* If we were not at the first slot to start, then think about
* wrapping if necessary.
*/
if (pg_lastrec != 0)
{
if (i >= rec)
i = 0;
else if (i == pg_lastrec)
break;
}
}
/* we didn't find it - make room in the cache for it */
if (rec >= MAX_CACHED_RES)
{
/* cache is full - find the oldest entry */
for (oldest = 0, i = 1; i < rec; i++)
{
if (rev[i].cre_lru < rev[oldest].cre_lru)
oldest = i;
}
}
else
oldest = rec++;
/* if there was an old re, then de-allocate the space it used */
if (rev[oldest].cre_s != (char *) NULL)
{
for (lru = i = 0; i < rec; i++)
{
/* downweight all of the other cached entries */
rev[i].cre_lru = (rev[i].cre_lru - rev[oldest].cre_lru) / 2;
if (rev[i].cre_lru > lru)
lru = rev[i].cre_lru;
}
pg_regfree(&rev[oldest].cre_re);
/*
* use malloc/free for the cre_s field because the storage has to
* persist across transactions
*/
free(rev[oldest].cre_s);
rev[oldest].cre_s = (char *) NULL;
}
/* compile the re */
regcomp_result = pg_regcomp(&rev[oldest].cre_re, re, cflags);
if (regcomp_result == 0)
{
pg_lastrec = oldest;
/*
* use malloc/free for the cre_s field because the storage has to
* persist across transactions
*/
rev[oldest].cre_s = strdup(re);
rev[oldest].cre_lru = ++lru;
rev[oldest].cre_type = cflags;
pfree(re);
/* agc - fixed an old typo here */
return (pg_regexec(&rev[oldest].cre_re, text,
nmatch, pmatch, 0) == 0);
}
else
{
char errMsg[1000];
/* re didn't compile */
pg_regerror(regcomp_result, &rev[oldest].cre_re, errMsg,
sizeof(errMsg));
elog(ERROR, "Invalid regular expression: %s", errMsg);
}
/* not reached */
return false;
}
/*
fixedlen_regexeq:
a generic fixed length regexp routine
s - the string to match against (not necessarily null-terminated)
p - the pattern (as a text*)
charlen - the length of the string
*/
static bool
fixedlen_regexeq(char *s, text *p, int charlen, int cflags)
{
char *sterm;
bool result;
/* be sure sterm is null-terminated */
sterm = (char *) palloc(charlen + 1);
memcpy(sterm, s, charlen);
sterm[charlen] = '\0';
result = RE_compile_and_execute(p, sterm, cflags, 0, NULL);
pfree(sterm);
return result;
}
/*
* interface routines called by the function manager
*/
Datum
nameregexeq(PG_FUNCTION_ARGS)
{
Name n = PG_GETARG_NAME(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(fixedlen_regexeq(NameStr(*n),
p,
strlen(NameStr(*n)),
REG_EXTENDED));
}
Datum
nameregexne(PG_FUNCTION_ARGS)
{
Name n = PG_GETARG_NAME(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(!fixedlen_regexeq(NameStr(*n),
p,
strlen(NameStr(*n)),
REG_EXTENDED));
}
Datum
textregexeq(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(fixedlen_regexeq(VARDATA(s),
p,
VARSIZE(s) - VARHDRSZ,
REG_EXTENDED));
}
Datum
textregexne(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(!fixedlen_regexeq(VARDATA(s),
p,
VARSIZE(s) - VARHDRSZ,
REG_EXTENDED));
}
/*
* routines that use the regexp stuff, but ignore the case.
* for this, we use the REG_ICASE flag to pg_regcomp
*/
Datum
texticregexeq(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(fixedlen_regexeq(VARDATA(s),
p,
VARSIZE(s) - VARHDRSZ,
REG_ICASE | REG_EXTENDED));
}
Datum
texticregexne(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(!fixedlen_regexeq(VARDATA(s),
p,
VARSIZE(s) - VARHDRSZ,
REG_ICASE | REG_EXTENDED));
}
Datum
nameicregexeq(PG_FUNCTION_ARGS)
{
Name n = PG_GETARG_NAME(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(fixedlen_regexeq(NameStr(*n),
p,
strlen(NameStr(*n)),
REG_ICASE | REG_EXTENDED));
}
Datum
nameicregexne(PG_FUNCTION_ARGS)
{
Name n = PG_GETARG_NAME(0);
text *p = PG_GETARG_TEXT_P(1);
PG_RETURN_BOOL(!fixedlen_regexeq(NameStr(*n),
p,
strlen(NameStr(*n)),
REG_ICASE | REG_EXTENDED));
}
/* textregexsubstr()
* Return a substring matched by a regular expression.
*/
Datum
textregexsubstr(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *p = PG_GETARG_TEXT_P(1);
char *sterm;
int len;
bool match;
regmatch_t pmatch[2];
/* be sure sterm is null-terminated */
len = VARSIZE(s) - VARHDRSZ;
sterm = (char *) palloc(len + 1);
memcpy(sterm, VARDATA(s), len);
sterm[len] = '\0';
/*
* We pass two regmatch_t structs to get info about the overall match
* and the match for the first parenthesized subexpression (if any).
* If there is a parenthesized subexpression, we return what it matched;
* else return what the whole regexp matched.
*/
match = RE_compile_and_execute(p, sterm, REG_EXTENDED, 2, pmatch);
pfree(sterm);
/* match? then return the substring matching the pattern */
if (match)
{
int so,
eo;
so = pmatch[1].rm_so;
eo = pmatch[1].rm_eo;
if (so < 0 || eo < 0)
{
/* no parenthesized subexpression */
so = pmatch[0].rm_so;
eo = pmatch[0].rm_eo;
}
return (DirectFunctionCall3(text_substr,
PointerGetDatum(s),
Int32GetDatum(so + 1),
Int32GetDatum(eo - so)));
}
PG_RETURN_NULL();
}
/* similar_escape()
* Convert a SQL99 regexp pattern to POSIX style, so it can be used by
* our regexp engine.
*/
Datum
similar_escape(PG_FUNCTION_ARGS)
{
text *pat_text;
text *esc_text;
text *result;
unsigned char *p,
*e,
*r;
int plen,
elen;
bool afterescape = false;
int nquotes = 0;
/* This function is not strict, so must test explicitly */
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
pat_text = PG_GETARG_TEXT_P(0);
p = VARDATA(pat_text);
plen = (VARSIZE(pat_text) - VARHDRSZ);
if (PG_ARGISNULL(1))
{
/* No ESCAPE clause provided; default to backslash as escape */
e = "\\";
elen = 1;
}
else
{
esc_text = PG_GETARG_TEXT_P(1);
e = VARDATA(esc_text);
elen = (VARSIZE(esc_text) - VARHDRSZ);
if (elen == 0)
e = NULL; /* no escape character */
else if (elen != 1)
elog(ERROR, "ESCAPE string must be empty or one character");
}
/* We need room for ^, $, and up to 2 output bytes per input byte */
result = (text *) palloc(VARHDRSZ + 2 + 2 * plen);
r = VARDATA(result);
*r++ = '^';
while (plen > 0)
{
unsigned char pchar = *p;
if (afterescape)
{
if (pchar == '"') /* for SUBSTRING patterns */
*r++ = ((nquotes++ % 2) == 0) ? '(' : ')';
else
{
*r++ = '\\';
*r++ = pchar;
}
afterescape = false;
}
else if (e && pchar == *e)
{
/* SQL99 escape character; do not send to output */
afterescape = true;
}
else if (pchar == '%')
{
*r++ = '.';
*r++ = '*';
}
else if (pchar == '_')
{
*r++ = '.';
}
else if (pchar == '\\' || pchar == '.' || pchar == '?' ||
pchar == '{')
{
*r++ = '\\';
*r++ = pchar;
}
else
{
*r++ = pchar;
}
p++, plen--;
}
*r++ = '$';
VARATT_SIZEP(result) = r - ((unsigned char *) result);
PG_RETURN_TEXT_P(result);
}
|