1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* regexp.c
|
2003-02-05 18:41:33 +01:00
|
|
|
* Postgres' interface to the regular expression package.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-11-22 19:17:34 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.61 2005/11/22 18:17:23 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* 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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* agc - incorporated Keith Bostic's Berkeley regex code into
|
|
|
|
* the tree for all ports. To distinguish this regex code from any that
|
2002-06-11 17:44:38 +02:00
|
|
|
* is existent on a platform, I've prepended the string "pg_" to
|
1997-09-07 07:04:48 +02:00
|
|
|
* 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...
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "postgres.h"
|
1996-11-06 11:32:10 +01:00
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "regex/regex.h"
|
2003-02-05 18:41:33 +01:00
|
|
|
#include "mb/pg_wchar.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/builtins.h"
|
2004-01-19 20:04:40 +01:00
|
|
|
#include "utils/guc.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2003-02-06 21:25:33 +01:00
|
|
|
/* GUC-settable flavor parameter */
|
|
|
|
static int regex_flavor = REG_ADVANCED;
|
|
|
|
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
|
|
|
* We cache precompiled regular expressions using a "self organizing list"
|
|
|
|
* structure, in which recently-used items tend to be near the front.
|
|
|
|
* Whenever we use an entry, it's moved up to the front of the list.
|
|
|
|
* Over time, an item's average position corresponds to its frequency of use.
|
|
|
|
*
|
|
|
|
* When we first create an entry, it's inserted at the front of
|
|
|
|
* the array, dropping the entry at the end of the array if necessary to
|
|
|
|
* make room. (This might seem to be weighting the new entry too heavily,
|
|
|
|
* but if we insert new entries further back, we'll be unable to adjust to
|
|
|
|
* a sudden shift in the query mix where we are presented with MAX_CACHED_RES
|
|
|
|
* never-before-seen items used circularly. We ought to be able to handle
|
|
|
|
* that case, so we have to insert at the front.)
|
|
|
|
*
|
|
|
|
* Knuth mentions a variant strategy in which a used item is moved up just
|
|
|
|
* one place in the list. Although he says this uses fewer comparisons on
|
|
|
|
* average, it seems not to adapt very well to the situation where you have
|
|
|
|
* both some reusable patterns and a steady stream of non-reusable patterns.
|
|
|
|
* A reusable pattern that isn't used at least as often as non-reusable
|
|
|
|
* patterns are seen will "fail to keep up" and will drop off the end of the
|
|
|
|
* cache. With move-to-front, a reusable pattern is guaranteed to stay in
|
|
|
|
* the cache as long as it's used at least once in every MAX_CACHED_RES uses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* this is the maximum number of cached regular expressions */
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifndef MAX_CACHED_RES
|
|
|
|
#define MAX_CACHED_RES 32
|
|
|
|
#endif
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/* this structure describes one cached regular expression */
|
|
|
|
typedef struct cached_re_str
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
text *cre_pat; /* original RE (untoasted TEXT form) */
|
|
|
|
int cre_flags; /* compile flags: extended,icase etc */
|
1997-09-08 04:41:22 +02:00
|
|
|
regex_t cre_re; /* the compiled regular expression */
|
2003-08-08 23:42:59 +02:00
|
|
|
} cached_re_str;
|
2003-02-05 18:41:33 +01:00
|
|
|
|
|
|
|
static int num_res = 0; /* # of cached re's */
|
2003-08-04 02:43:34 +02:00
|
|
|
static cached_re_str re_array[MAX_CACHED_RES]; /* cached re's */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
2005-07-10 06:54:33 +02:00
|
|
|
* RE_compile_and_cache - compile a RE, caching if possible
|
2003-02-05 18:41:33 +01:00
|
|
|
*
|
2005-10-18 22:38:58 +02:00
|
|
|
* Returns regex_t *
|
2003-02-05 18:41:33 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* text_re --- the pattern, expressed as an *untoasted* TEXT object
|
|
|
|
* cflags --- compile options for the pattern
|
2003-02-05 18:41:33 +01:00
|
|
|
*
|
2005-07-10 06:54:33 +02:00
|
|
|
* Pattern is given in the database encoding. We internally convert to
|
|
|
|
* array of pg_wchar which is what Spencer's regex package wants.
|
2003-02-05 18:41:33 +01:00
|
|
|
*/
|
2005-10-18 22:38:58 +02:00
|
|
|
static regex_t *
|
2005-07-10 06:54:33 +02:00
|
|
|
RE_compile_and_cache(text *text_re, int cflags)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
int text_re_len = VARSIZE(text_re);
|
|
|
|
pg_wchar *pattern;
|
|
|
|
size_t pattern_len;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
|
|
|
int regcomp_result;
|
2003-08-04 02:43:34 +02:00
|
|
|
cached_re_str re_temp;
|
2004-11-24 23:44:07 +01:00
|
|
|
char errMsg[100];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Look for a match among previously compiled REs. Since the data
|
2005-10-15 04:49:52 +02:00
|
|
|
* structure is self-organizing with most-used entries at the front, our
|
|
|
|
* search strategy can just be to scan from the front.
|
2002-06-15 04:49:47 +02:00
|
|
|
*/
|
2003-02-05 18:41:33 +01:00
|
|
|
for (i = 0; i < num_res; i++)
|
2002-06-11 17:44:38 +02:00
|
|
|
{
|
2004-02-03 18:52:55 +01:00
|
|
|
if (VARSIZE(re_array[i].cre_pat) == text_re_len &&
|
|
|
|
memcmp(re_array[i].cre_pat, text_re, text_re_len) == 0 &&
|
2003-02-05 18:41:33 +01:00
|
|
|
re_array[i].cre_flags == cflags)
|
2002-06-11 17:44:38 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
|
|
|
* Found a match; move it to front if not there already.
|
|
|
|
*/
|
|
|
|
if (i > 0)
|
2002-06-11 17:44:38 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
re_temp = re_array[i];
|
|
|
|
memmove(&re_array[1], &re_array[0], i * sizeof(cached_re_str));
|
|
|
|
re_array[0] = re_temp;
|
2002-06-11 17:44:38 +02:00
|
|
|
}
|
|
|
|
|
2005-10-18 22:38:58 +02:00
|
|
|
return &re_array[0].cre_re;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
|
|
|
* Couldn't find it, so try to compile the new RE. To avoid leaking
|
|
|
|
* resources on failure, we build into the re_temp local.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Convert pattern string to wide characters */
|
|
|
|
pattern = (pg_wchar *) palloc((text_re_len - VARHDRSZ + 1) * sizeof(pg_wchar));
|
2005-09-24 19:53:28 +02:00
|
|
|
pattern_len = pg_mb2wchar_with_len(VARDATA(text_re),
|
2003-02-05 18:41:33 +01:00
|
|
|
pattern,
|
|
|
|
text_re_len - VARHDRSZ);
|
|
|
|
|
|
|
|
regcomp_result = pg_regcomp(&re_temp.cre_re,
|
|
|
|
pattern,
|
|
|
|
pattern_len,
|
|
|
|
cflags);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
pfree(pattern);
|
|
|
|
|
2004-11-24 23:44:07 +01:00
|
|
|
if (regcomp_result != REG_OKAY)
|
2003-02-05 18:41:33 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/* re didn't compile */
|
2003-02-05 18:41:33 +01:00
|
|
|
pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
|
|
|
|
/* XXX should we pg_regfree here? */
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
|
|
|
|
errmsg("invalid regular expression: %s", errMsg)));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
|
|
|
* use malloc/free for the cre_pat field because the storage has to
|
|
|
|
* persist across transactions
|
|
|
|
*/
|
|
|
|
re_temp.cre_pat = malloc(text_re_len);
|
|
|
|
if (re_temp.cre_pat == NULL)
|
|
|
|
{
|
|
|
|
pg_regfree(&re_temp.cre_re);
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2003-02-05 18:41:33 +01:00
|
|
|
}
|
|
|
|
memcpy(re_temp.cre_pat, text_re, text_re_len);
|
|
|
|
re_temp.cre_flags = cflags;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Okay, we have a valid new item in re_temp; insert it into the storage
|
|
|
|
* array. Discard last entry if needed.
|
2003-02-05 18:41:33 +01:00
|
|
|
*/
|
|
|
|
if (num_res >= MAX_CACHED_RES)
|
|
|
|
{
|
|
|
|
--num_res;
|
|
|
|
Assert(num_res < MAX_CACHED_RES);
|
|
|
|
pg_regfree(&re_array[num_res].cre_re);
|
|
|
|
free(re_array[num_res].cre_pat);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
if (num_res > 0)
|
|
|
|
memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
re_array[0] = re_temp;
|
|
|
|
num_res++;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-10-18 22:38:58 +02:00
|
|
|
return &re_array[0].cre_re;
|
2005-07-10 06:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RE_compile_and_execute - compile and execute a RE
|
|
|
|
*
|
|
|
|
* Returns TRUE on match, FALSE on no match
|
|
|
|
*
|
|
|
|
* text_re --- the pattern, expressed as an *untoasted* TEXT object
|
|
|
|
* dat --- the data to match against (need not be null-terminated)
|
|
|
|
* dat_len --- the length of the data string
|
|
|
|
* cflags --- compile options for the pattern
|
|
|
|
* nmatch, pmatch --- optional return area for match details
|
|
|
|
*
|
|
|
|
* Both pattern and data are given in the database encoding. We internally
|
|
|
|
* convert to array of pg_wchar which is what Spencer's regex package wants.
|
|
|
|
*/
|
|
|
|
static bool
|
2005-09-24 19:53:28 +02:00
|
|
|
RE_compile_and_execute(text *text_re, char *dat, int dat_len,
|
2005-07-10 06:54:33 +02:00
|
|
|
int cflags, int nmatch, regmatch_t *pmatch)
|
|
|
|
{
|
|
|
|
pg_wchar *data;
|
|
|
|
size_t data_len;
|
|
|
|
int regexec_result;
|
2005-11-22 19:17:34 +01:00
|
|
|
regex_t *re;
|
2005-10-15 04:49:52 +02:00
|
|
|
char errMsg[100];
|
2005-07-10 06:54:33 +02:00
|
|
|
|
|
|
|
/* Convert data string to wide characters */
|
|
|
|
data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
|
|
|
|
data_len = pg_mb2wchar_with_len(dat, data, dat_len);
|
|
|
|
|
|
|
|
/* Compile RE */
|
|
|
|
re = RE_compile_and_cache(text_re, cflags);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/* Perform RE match and return result */
|
2005-10-18 22:38:58 +02:00
|
|
|
regexec_result = pg_regexec(re,
|
2003-02-05 18:41:33 +01:00
|
|
|
data,
|
|
|
|
data_len,
|
2005-07-10 06:54:33 +02:00
|
|
|
0,
|
2003-08-04 02:43:34 +02:00
|
|
|
NULL, /* no details */
|
2003-02-05 18:41:33 +01:00
|
|
|
nmatch,
|
|
|
|
pmatch,
|
|
|
|
0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
pfree(data);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-11-24 23:44:07 +01:00
|
|
|
if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
|
|
|
|
{
|
|
|
|
/* re failed??? */
|
2005-10-18 22:38:58 +02:00
|
|
|
pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
|
2004-11-24 23:44:07 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
|
|
|
|
errmsg("regular expression failed: %s", errMsg)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (regexec_result == REG_OKAY);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-06 21:25:33 +01:00
|
|
|
/*
|
|
|
|
* assign_regex_flavor - GUC hook to validate and set REGEX_FLAVOR
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
assign_regex_flavor(const char *value,
|
2004-01-19 20:04:40 +01:00
|
|
|
bool doit, GucSource source)
|
2003-02-06 21:25:33 +01:00
|
|
|
{
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(value, "advanced") == 0)
|
2003-02-06 21:25:33 +01:00
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
regex_flavor = REG_ADVANCED;
|
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(value, "extended") == 0)
|
2003-02-06 21:25:33 +01:00
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
regex_flavor = REG_EXTENDED;
|
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(value, "basic") == 0)
|
2003-02-06 21:25:33 +01:00
|
|
|
{
|
|
|
|
if (doit)
|
|
|
|
regex_flavor = REG_BASIC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL; /* fail */
|
|
|
|
return value; /* OK */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-07-06 07:48:31 +02:00
|
|
|
* interface routines called by the function manager
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-07-06 07:48:31 +02:00
|
|
|
|
|
|
|
Datum
|
|
|
|
nameregexeq(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-07-06 07:48:31 +02:00
|
|
|
Name n = PG_GETARG_NAME(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
NameStr(*n),
|
2003-02-05 18:41:33 +01:00
|
|
|
strlen(NameStr(*n)),
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
nameregexne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-07-06 07:48:31 +02:00
|
|
|
Name n = PG_GETARG_NAME(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(!RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
NameStr(*n),
|
2003-02-05 18:41:33 +01:00
|
|
|
strlen(NameStr(*n)),
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
textregexeq(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-07-06 07:48:31 +02:00
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
VARDATA(s),
|
2003-02-05 18:41:33 +01:00
|
|
|
VARSIZE(s) - VARHDRSZ,
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
|
|
|
textregexne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-07-06 07:48:31 +02:00
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(!RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
VARDATA(s),
|
2003-02-05 18:41:33 +01:00
|
|
|
VARSIZE(s) - VARHDRSZ,
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* routines that use the regexp stuff, but ignore the case.
|
2002-06-11 17:44:38 +02:00
|
|
|
* for this, we use the REG_ICASE flag to pg_regcomp
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-07-06 07:48:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2003-02-05 18:41:33 +01:00
|
|
|
nameicregexeq(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
Name n = PG_GETARG_NAME(0);
|
2000-07-06 07:48:31 +02:00
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
NameStr(*n),
|
2003-02-05 18:41:33 +01:00
|
|
|
strlen(NameStr(*n)),
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor | REG_ICASE,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
2003-02-05 18:41:33 +01:00
|
|
|
nameicregexne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
Name n = PG_GETARG_NAME(0);
|
2000-07-06 07:48:31 +02:00
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(!RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
NameStr(*n),
|
2003-02-05 18:41:33 +01:00
|
|
|
strlen(NameStr(*n)),
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor | REG_ICASE,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
2003-02-05 18:41:33 +01:00
|
|
|
texticregexeq(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
2000-07-06 07:48:31 +02:00
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
VARDATA(s),
|
2003-02-05 18:41:33 +01:00
|
|
|
VARSIZE(s) - VARHDRSZ,
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor | REG_ICASE,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-06 07:48:31 +02:00
|
|
|
Datum
|
2003-02-05 18:41:33 +01:00
|
|
|
texticregexne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-05 18:41:33 +01:00
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
2000-07-06 07:48:31 +02:00
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
PG_RETURN_BOOL(!RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
VARDATA(s),
|
2003-02-05 18:41:33 +01:00
|
|
|
VARSIZE(s) - VARHDRSZ,
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor | REG_ICASE,
|
2003-02-05 18:41:33 +01:00
|
|
|
0, NULL));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2002-06-11 17:44:38 +02:00
|
|
|
|
|
|
|
|
2003-02-05 18:41:33 +01:00
|
|
|
/*
|
|
|
|
* textregexsubstr()
|
|
|
|
* Return a substring matched by a regular expression.
|
2002-06-11 17:44:38 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
textregexsubstr(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
bool match;
|
2002-09-22 19:27:25 +02:00
|
|
|
regmatch_t pmatch[2];
|
2002-06-11 17:44:38 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 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.
|
2002-06-11 17:44:38 +02:00
|
|
|
*/
|
2003-02-05 18:41:33 +01:00
|
|
|
match = RE_compile_and_execute(p,
|
2005-09-24 19:53:28 +02:00
|
|
|
VARDATA(s),
|
2003-02-05 18:41:33 +01:00
|
|
|
VARSIZE(s) - VARHDRSZ,
|
2003-02-06 21:25:33 +01:00
|
|
|
regex_flavor,
|
2003-02-05 18:41:33 +01:00
|
|
|
2, pmatch);
|
2002-06-11 17:44:38 +02:00
|
|
|
|
|
|
|
/* match? then return the substring matching the pattern */
|
|
|
|
if (match)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int so,
|
|
|
|
eo;
|
2002-09-22 19:27:25 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-07-10 06:54:33 +02:00
|
|
|
return DirectFunctionCall3(text_substr,
|
2005-10-15 04:49:52 +02:00
|
|
|
PointerGetDatum(s),
|
|
|
|
Int32GetDatum(so + 1),
|
|
|
|
Int32GetDatum(eo - so));
|
2002-06-11 17:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
2002-09-22 19:27:25 +02:00
|
|
|
|
2005-07-10 06:54:33 +02:00
|
|
|
/*
|
|
|
|
* textregexreplace_noopt()
|
2005-10-18 22:38:58 +02:00
|
|
|
* Return a string matched by a regular expression, with replacement.
|
|
|
|
*
|
|
|
|
* This version doesn't have an option argument: we default to case
|
|
|
|
* sensitive match, replace the first instance only.
|
2005-07-10 06:54:33 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
textregexreplace_noopt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
text *r = PG_GETARG_TEXT_P(2);
|
2005-11-22 19:17:34 +01:00
|
|
|
regex_t *re;
|
2005-07-10 06:54:33 +02:00
|
|
|
|
|
|
|
re = RE_compile_and_cache(p, regex_flavor);
|
|
|
|
|
2005-10-18 22:38:58 +02:00
|
|
|
PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, false));
|
2005-07-10 06:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* textregexreplace()
|
2005-10-18 22:38:58 +02:00
|
|
|
* Return a string matched by a regular expression, with replacement.
|
2005-07-10 06:54:33 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
textregexreplace(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *s = PG_GETARG_TEXT_P(0);
|
|
|
|
text *p = PG_GETARG_TEXT_P(1);
|
|
|
|
text *r = PG_GETARG_TEXT_P(2);
|
|
|
|
text *opt = PG_GETARG_TEXT_P(3);
|
|
|
|
char *opt_p = VARDATA(opt);
|
|
|
|
int opt_len = (VARSIZE(opt) - VARHDRSZ);
|
|
|
|
int i;
|
2005-10-18 22:38:58 +02:00
|
|
|
bool glob = false;
|
2005-07-10 06:54:33 +02:00
|
|
|
bool ignorecase = false;
|
2005-11-22 19:17:34 +01:00
|
|
|
regex_t *re;
|
2005-07-10 06:54:33 +02:00
|
|
|
|
|
|
|
/* parse options */
|
|
|
|
for (i = 0; i < opt_len; i++)
|
|
|
|
{
|
|
|
|
switch (opt_p[i])
|
|
|
|
{
|
|
|
|
case 'i':
|
|
|
|
ignorecase = true;
|
|
|
|
break;
|
|
|
|
case 'g':
|
2005-10-18 22:38:58 +02:00
|
|
|
glob = true;
|
2005-07-10 06:54:33 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ereport(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid option of regexp_replace: %c",
|
|
|
|
opt_p[i])));
|
2005-07-10 06:54:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ignorecase)
|
|
|
|
re = RE_compile_and_cache(p, regex_flavor | REG_ICASE);
|
|
|
|
else
|
|
|
|
re = RE_compile_and_cache(p, regex_flavor);
|
|
|
|
|
2005-10-18 22:38:58 +02:00
|
|
|
PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, glob));
|
2005-07-10 06:54:33 +02:00
|
|
|
}
|
|
|
|
|
2002-09-22 19:27:25 +02:00
|
|
|
/* 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;
|
2005-09-24 19:53:28 +02:00
|
|
|
char *p,
|
2002-09-22 19:27:25 +02:00
|
|
|
*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)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
|
|
|
|
errmsg("invalid escape string"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("Escape string must be empty or one character.")));
|
2002-09-22 19:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
char pchar = *p;
|
2002-09-22 19:27:25 +02:00
|
|
|
|
|
|
|
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++ = '$';
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
VARATT_SIZEP(result) = r - ((char *) result);
|
2002-09-22 19:27:25 +02:00
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|