*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.85 2010/01/02 16:57:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.86 2010/01/02 20:59:16 tgl Exp $
*
* Alistair Crooks added the code for the regex caching
* agc - cached the regular expressions used - there's a good chance
int plen,
elen;
bool afterescape = false;
+ bool incharclass = false;
int nquotes = 0;
/* This function is not strict, so must test explicitly */
*/
/*
- * We need room for the prefix/postfix plus as many as 2 output bytes per
- * input byte
+ * We need room for the prefix/postfix plus as many as 3 output bytes per
+ * input byte; since the input is at most 1GB this can't overflow
*/
- result = (text *) palloc(VARHDRSZ + 6 + 2 * plen);
+ result = (text *) palloc(VARHDRSZ + 6 + 3 * plen);
r = VARDATA(result);
*r++ = '^';
if (afterescape)
{
- if (pchar == '"') /* for SUBSTRING patterns */
+ if (pchar == '"' && !incharclass) /* for SUBSTRING patterns */
*r++ = ((nquotes++ % 2) == 0) ? '(' : ')';
else
{
/* SQL99 escape character; do not send to output */
afterescape = true;
}
+ else if (incharclass)
+ {
+ if (pchar == '\\')
+ *r++ = '\\';
+ *r++ = pchar;
+ if (pchar == ']')
+ incharclass = false;
+ }
+ else if (pchar == '[')
+ {
+ *r++ = pchar;
+ incharclass = true;
+ }
else if (pchar == '%')
{
*r++ = '.';
}
else if (pchar == '_')
*r++ = '.';
+ else if (pchar == '(')
+ {
+ /* convert to non-capturing parenthesis */
+ *r++ = '(';
+ *r++ = '?';
+ *r++ = ':';
+ }
else if (pchar == '\\' || pchar == '.' ||
pchar == '^' || pchar == '$')
{