PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
regexp.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "regex/regex.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/varlena.h"
Include dependency graph for regexp.c:

Go to the source code of this file.

Data Structures

struct  pg_re_flags
 
struct  regexp_matches_ctx
 
struct  cached_re_str
 

Macros

#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)   (PG_NARGS() > (_n) ? PG_GETARG_TEXT_PP(_n) : NULL)
 
#define MAX_CACHED_RES   32
 

Typedefs

typedef struct pg_re_flags pg_re_flags
 
typedef struct regexp_matches_ctx regexp_matches_ctx
 
typedef struct cached_re_str cached_re_str
 

Functions

static regexp_matches_ctxsetup_regexp_matches (text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate)
 
static void cleanup_regexp_matches (regexp_matches_ctx *matchctx)
 
static ArrayTypebuild_regexp_match_result (regexp_matches_ctx *matchctx)
 
static Datum build_regexp_split_result (regexp_matches_ctx *splitctx)
 
static regex_tRE_compile_and_cache (text *text_re, int cflags, Oid collation)
 
static bool RE_wchar_execute (regex_t *re, pg_wchar *data, int data_len, int start_search, int nmatch, regmatch_t *pmatch)
 
static bool RE_execute (regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
 
static bool RE_compile_and_execute (text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
 
static void parse_re_flags (pg_re_flags *flags, text *opts)
 
Datum nameregexeq (PG_FUNCTION_ARGS)
 
Datum nameregexne (PG_FUNCTION_ARGS)
 
Datum textregexeq (PG_FUNCTION_ARGS)
 
Datum textregexne (PG_FUNCTION_ARGS)
 
Datum nameicregexeq (PG_FUNCTION_ARGS)
 
Datum nameicregexne (PG_FUNCTION_ARGS)
 
Datum texticregexeq (PG_FUNCTION_ARGS)
 
Datum texticregexne (PG_FUNCTION_ARGS)
 
Datum textregexsubstr (PG_FUNCTION_ARGS)
 
Datum textregexreplace_noopt (PG_FUNCTION_ARGS)
 
Datum textregexreplace (PG_FUNCTION_ARGS)
 
Datum similar_escape (PG_FUNCTION_ARGS)
 
Datum regexp_match (PG_FUNCTION_ARGS)
 
Datum regexp_match_no_flags (PG_FUNCTION_ARGS)
 
Datum regexp_matches (PG_FUNCTION_ARGS)
 
Datum regexp_matches_no_flags (PG_FUNCTION_ARGS)
 
Datum regexp_split_to_table (PG_FUNCTION_ARGS)
 
Datum regexp_split_to_table_no_flags (PG_FUNCTION_ARGS)
 
Datum regexp_split_to_array (PG_FUNCTION_ARGS)
 
Datum regexp_split_to_array_no_flags (PG_FUNCTION_ARGS)
 
char * regexp_fixed_prefix (text *text_re, bool case_insensitive, Oid collation, bool *exact)
 

Variables

static int num_res = 0
 
static cached_re_str re_array [MAX_CACHED_RES]
 

Macro Definition Documentation

#define MAX_CACHED_RES   32

Definition at line 92 of file regexp.c.

Referenced by RE_compile_and_cache().

#define PG_GETARG_TEXT_PP_IF_EXISTS (   _n)    (PG_NARGS() > (_n) ? PG_GETARG_TEXT_PP(_n) : NULL)

Typedef Documentation

Function Documentation

static ArrayType * build_regexp_match_result ( regexp_matches_ctx matchctx)
static

Definition at line 1097 of file regexp.c.

References construct_md_array(), DirectFunctionCall3, regexp_matches_ctx::elems, i, Int32GetDatum, regexp_matches_ctx::match_locs, regexp_matches_ctx::next_match, regexp_matches_ctx::npatterns, regexp_matches_ctx::nulls, regexp_matches_ctx::orig_str, PointerGetDatum, text_substr(), and TEXTOID.

Referenced by regexp_match(), and regexp_matches().

1098 {
1099  Datum *elems = matchctx->elems;
1100  bool *nulls = matchctx->nulls;
1101  int dims[1];
1102  int lbs[1];
1103  int loc;
1104  int i;
1105 
1106  /* Extract matching substrings from the original string */
1107  loc = matchctx->next_match * matchctx->npatterns * 2;
1108  for (i = 0; i < matchctx->npatterns; i++)
1109  {
1110  int so = matchctx->match_locs[loc++];
1111  int eo = matchctx->match_locs[loc++];
1112 
1113  if (so < 0 || eo < 0)
1114  {
1115  elems[i] = (Datum) 0;
1116  nulls[i] = true;
1117  }
1118  else
1119  {
1121  PointerGetDatum(matchctx->orig_str),
1122  Int32GetDatum(so + 1),
1123  Int32GetDatum(eo - so));
1124  nulls[i] = false;
1125  }
1126  }
1127 
1128  /* And form an array */
1129  dims[0] = matchctx->npatterns;
1130  lbs[0] = 1;
1131  /* XXX: this hardcodes assumptions about the text type */
1132  return construct_md_array(elems, nulls, 1, dims, lbs,
1133  TEXTOID, -1, false, 'i');
1134 }
Datum * elems
Definition: regexp.c:62
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:562
bool * nulls
Definition: regexp.c:63
int * match_locs
Definition: regexp.c:59
text * orig_str
Definition: regexp.c:54
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:781
uintptr_t Datum
Definition: postgres.h:372
#define Int32GetDatum(X)
Definition: postgres.h:485
int i
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3340
static Datum build_regexp_split_result ( regexp_matches_ctx splitctx)
static

Definition at line 1262 of file regexp.c.

References DirectFunctionCall2, DirectFunctionCall3, elog, endpos, ERROR, Int32GetDatum, regexp_matches_ctx::match_locs, regexp_matches_ctx::next_match, regexp_matches_ctx::nmatches, regexp_matches_ctx::orig_str, PointerGetDatum, startpos, text_substr(), and text_substr_no_len().

Referenced by regexp_split_to_array(), and regexp_split_to_table().

1263 {
1264  int startpos;
1265  int endpos;
1266 
1267  if (splitctx->next_match > 0)
1268  startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
1269  else
1270  startpos = 0;
1271  if (startpos < 0)
1272  elog(ERROR, "invalid match ending position");
1273 
1274  if (splitctx->next_match < splitctx->nmatches)
1275  {
1276  endpos = splitctx->match_locs[splitctx->next_match * 2];
1277  if (endpos < startpos)
1278  elog(ERROR, "invalid match starting position");
1280  PointerGetDatum(splitctx->orig_str),
1281  Int32GetDatum(startpos + 1),
1282  Int32GetDatum(endpos - startpos));
1283  }
1284  else
1285  {
1286  /* no more matches, return rest of string */
1288  PointerGetDatum(splitctx->orig_str),
1289  Int32GetDatum(startpos + 1));
1290  }
1291 }
#define PointerGetDatum(X)
Definition: postgres.h:562
int * match_locs
Definition: regexp.c:59
static XLogRecPtr endpos
#define ERROR
Definition: elog.h:43
text * orig_str
Definition: regexp.c:54
Datum text_substr_no_len(PG_FUNCTION_ARGS)
Definition: varlena.c:795
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:781
static XLogRecPtr startpos
#define Int32GetDatum(X)
Definition: postgres.h:485
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static void cleanup_regexp_matches ( regexp_matches_ctx matchctx)
static

Definition at line 1082 of file regexp.c.

References regexp_matches_ctx::elems, regexp_matches_ctx::match_locs, regexp_matches_ctx::nulls, regexp_matches_ctx::orig_str, and pfree().

Referenced by regexp_matches(), and regexp_split_to_table().

1083 {
1084  pfree(matchctx->orig_str);
1085  pfree(matchctx->match_locs);
1086  if (matchctx->elems)
1087  pfree(matchctx->elems);
1088  if (matchctx->nulls)
1089  pfree(matchctx->nulls);
1090  pfree(matchctx);
1091 }
Datum * elems
Definition: regexp.c:62
bool * nulls
Definition: regexp.c:63
int * match_locs
Definition: regexp.c:59
void pfree(void *pointer)
Definition: mcxt.c:950
text * orig_str
Definition: regexp.c:54
Datum nameicregexeq ( PG_FUNCTION_ARGS  )

Definition at line 499 of file regexp.c.

References NameStr, NULL, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), REG_ADVANCED, and REG_ICASE.

500 {
501  Name n = PG_GETARG_NAME(0);
502  text *p = PG_GETARG_TEXT_PP(1);
503 
505  NameStr(*n),
506  strlen(NameStr(*n)),
509  0, NULL));
510 }
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:493
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum nameicregexne ( PG_FUNCTION_ARGS  )

Definition at line 513 of file regexp.c.

References NameStr, NULL, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), REG_ADVANCED, and REG_ICASE.

514 {
515  Name n = PG_GETARG_NAME(0);
516  text *p = PG_GETARG_TEXT_PP(1);
517 
519  NameStr(*n),
520  strlen(NameStr(*n)),
523  0, NULL));
524 }
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:493
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum nameregexeq ( PG_FUNCTION_ARGS  )

Definition at line 436 of file regexp.c.

References NameStr, NULL, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), and REG_ADVANCED.

437 {
438  Name n = PG_GETARG_NAME(0);
439  text *p = PG_GETARG_TEXT_PP(1);
440 
442  NameStr(*n),
443  strlen(NameStr(*n)),
444  REG_ADVANCED,
446  0, NULL));
447 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:493
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum nameregexne ( PG_FUNCTION_ARGS  )

Definition at line 450 of file regexp.c.

References NameStr, NULL, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), and REG_ADVANCED.

451 {
452  Name n = PG_GETARG_NAME(0);
453  text *p = PG_GETARG_TEXT_PP(1);
454 
456  NameStr(*n),
457  strlen(NameStr(*n)),
458  REG_ADVANCED,
460  0, NULL));
461 }
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
Definition: c.h:493
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define NameStr(name)
Definition: c.h:499
Definition: c.h:439
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
static void parse_re_flags ( pg_re_flags flags,
text opts 
)
static

Definition at line 362 of file regexp.c.

References pg_re_flags::cflags, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, i, REG_ADVANCED, REG_EXPANDED, REG_EXTENDED, REG_ICASE, REG_NEWLINE, REG_NLANCH, REG_NLSTOP, REG_QUOTE, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by regexp_match(), regexp_matches(), regexp_split_to_array(), regexp_split_to_table(), and textregexreplace().

363 {
364  /* regex flavor is always folded into the compile flags */
365  flags->cflags = REG_ADVANCED;
366  flags->glob = false;
367 
368  if (opts)
369  {
370  char *opt_p = VARDATA_ANY(opts);
371  int opt_len = VARSIZE_ANY_EXHDR(opts);
372  int i;
373 
374  for (i = 0; i < opt_len; i++)
375  {
376  switch (opt_p[i])
377  {
378  case 'g':
379  flags->glob = true;
380  break;
381  case 'b': /* BREs (but why???) */
382  flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
383  break;
384  case 'c': /* case sensitive */
385  flags->cflags &= ~REG_ICASE;
386  break;
387  case 'e': /* plain EREs */
388  flags->cflags |= REG_EXTENDED;
389  flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
390  break;
391  case 'i': /* case insensitive */
392  flags->cflags |= REG_ICASE;
393  break;
394  case 'm': /* Perloid synonym for n */
395  case 'n': /* \n affects ^ $ . [^ */
396  flags->cflags |= REG_NEWLINE;
397  break;
398  case 'p': /* ~Perl, \n affects . [^ */
399  flags->cflags |= REG_NLSTOP;
400  flags->cflags &= ~REG_NLANCH;
401  break;
402  case 'q': /* literal string */
403  flags->cflags |= REG_QUOTE;
404  flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
405  break;
406  case 's': /* single line, \n ordinary */
407  flags->cflags &= ~REG_NEWLINE;
408  break;
409  case 't': /* tight syntax */
410  flags->cflags &= ~REG_EXPANDED;
411  break;
412  case 'w': /* weird, \n affects ^ $ only */
413  flags->cflags &= ~REG_NLSTOP;
414  flags->cflags |= REG_NLANCH;
415  break;
416  case 'x': /* expanded syntax */
417  flags->cflags |= REG_EXPANDED;
418  break;
419  default:
420  ereport(ERROR,
421  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
422  errmsg("invalid regexp option: \"%c\"",
423  opt_p[i])));
424  break;
425  }
426  }
427  }
428 }
#define REG_NLSTOP
Definition: regex.h:109
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define REG_QUOTE
Definition: regex.h:104
int cflags
Definition: regexp.c:47
bool glob
Definition: regexp.c:48
int errcode(int sqlerrcode)
Definition: elog.c:575
#define REG_ICASE
Definition: regex.h:106
#define ERROR
Definition: elog.h:43
#define REG_NEWLINE
Definition: regex.h:111
#define REG_ADVANCED
Definition: regex.h:103
#define ereport(elevel, rest)
Definition: elog.h:122
#define REG_EXTENDED
Definition: regex.h:101
#define REG_NLANCH
Definition: regex.h:110
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define REG_EXPANDED
Definition: regex.h:108
static regex_t* RE_compile_and_cache ( text text_re,
int  cflags,
Oid  collation 
)
static

Definition at line 133 of file regexp.c.

References Assert, CHECK_FOR_INTERRUPTS, cached_re_str::cre_collation, cached_re_str::cre_flags, cached_re_str::cre_pat, cached_re_str::cre_pat_len, cached_re_str::cre_re, ereport, errcode(), errmsg(), ERROR, free, i, malloc, Max, MAX_CACHED_RES, memmove, NULL, num_res, palloc(), pfree(), pg_mb2wchar_with_len(), pg_regcomp(), pg_regerror(), pg_regfree(), REG_OKAY, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by RE_compile_and_execute(), regexp_fixed_prefix(), setup_regexp_matches(), textregexreplace(), textregexreplace_noopt(), and textregexsubstr().

134 {
135  int text_re_len = VARSIZE_ANY_EXHDR(text_re);
136  char *text_re_val = VARDATA_ANY(text_re);
137  pg_wchar *pattern;
138  int pattern_len;
139  int i;
140  int regcomp_result;
141  cached_re_str re_temp;
142  char errMsg[100];
143 
144  /*
145  * Look for a match among previously compiled REs. Since the data
146  * structure is self-organizing with most-used entries at the front, our
147  * search strategy can just be to scan from the front.
148  */
149  for (i = 0; i < num_res; i++)
150  {
151  if (re_array[i].cre_pat_len == text_re_len &&
152  re_array[i].cre_flags == cflags &&
153  re_array[i].cre_collation == collation &&
154  memcmp(re_array[i].cre_pat, text_re_val, text_re_len) == 0)
155  {
156  /*
157  * Found a match; move it to front if not there already.
158  */
159  if (i > 0)
160  {
161  re_temp = re_array[i];
162  memmove(&re_array[1], &re_array[0], i * sizeof(cached_re_str));
163  re_array[0] = re_temp;
164  }
165 
166  return &re_array[0].cre_re;
167  }
168  }
169 
170  /*
171  * Couldn't find it, so try to compile the new RE. To avoid leaking
172  * resources on failure, we build into the re_temp local.
173  */
174 
175  /* Convert pattern string to wide characters */
176  pattern = (pg_wchar *) palloc((text_re_len + 1) * sizeof(pg_wchar));
177  pattern_len = pg_mb2wchar_with_len(text_re_val,
178  pattern,
179  text_re_len);
180 
181  regcomp_result = pg_regcomp(&re_temp.cre_re,
182  pattern,
183  pattern_len,
184  cflags,
185  collation);
186 
187  pfree(pattern);
188 
189  if (regcomp_result != REG_OKAY)
190  {
191  /* re didn't compile (no need for pg_regfree, if so) */
192 
193  /*
194  * Here and in other places in this file, do CHECK_FOR_INTERRUPTS
195  * before reporting a regex error. This is so that if the regex
196  * library aborts and returns REG_CANCEL, we don't print an error
197  * message that implies the regex was invalid.
198  */
200 
201  pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
202  ereport(ERROR,
203  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
204  errmsg("invalid regular expression: %s", errMsg)));
205  }
206 
207  /*
208  * We use malloc/free for the cre_pat field because the storage has to
209  * persist across transactions, and because we want to get control back on
210  * out-of-memory. The Max() is because some malloc implementations return
211  * NULL for malloc(0).
212  */
213  re_temp.cre_pat = malloc(Max(text_re_len, 1));
214  if (re_temp.cre_pat == NULL)
215  {
216  pg_regfree(&re_temp.cre_re);
217  ereport(ERROR,
218  (errcode(ERRCODE_OUT_OF_MEMORY),
219  errmsg("out of memory")));
220  }
221  memcpy(re_temp.cre_pat, text_re_val, text_re_len);
222  re_temp.cre_pat_len = text_re_len;
223  re_temp.cre_flags = cflags;
224  re_temp.cre_collation = collation;
225 
226  /*
227  * Okay, we have a valid new item in re_temp; insert it into the storage
228  * array. Discard last entry if needed.
229  */
230  if (num_res >= MAX_CACHED_RES)
231  {
232  --num_res;
233  Assert(num_res < MAX_CACHED_RES);
234  pg_regfree(&re_array[num_res].cre_re);
235  free(re_array[num_res].cre_pat);
236  }
237 
238  if (num_res > 0)
239  memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
240 
241  re_array[0] = re_temp;
242  num_res++;
243 
244  return &re_array[0].cre_re;
245 }
static int num_res
Definition: regexp.c:105
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
regex_t cre_re
Definition: regexp.c:102
int cre_flags
Definition: regexp.c:100
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MAX_CACHED_RES
Definition: regexp.c:92
int pg_regcomp(regex_t *re, const chr *string, size_t len, int flags, Oid collation)
Definition: regcomp.c:314
char * cre_pat
Definition: regexp.c:98
#define malloc(a)
Definition: header.h:50
void pfree(void *pointer)
Definition: mcxt.c:950
#define REG_OKAY
Definition: regex.h:137
static cached_re_str re_array[MAX_CACHED_RES]
Definition: regexp.c:106
#define ERROR
Definition: elog.h:43
#define memmove(d, s, c)
Definition: c.h:1058
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define ereport(elevel, rest)
Definition: elog.h:122
unsigned int pg_wchar
Definition: mbprint.c:31
Oid cre_collation
Definition: regexp.c:101
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:734
#define free(a)
Definition: header.h:65
#define Max(x, y)
Definition: c.h:800
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void pg_regfree(regex_t *re)
Definition: regfree.c:49
int cre_pat_len
Definition: regexp.c:99
static bool RE_compile_and_execute ( text text_re,
char *  dat,
int  dat_len,
int  cflags,
Oid  collation,
int  nmatch,
regmatch_t pmatch 
)
static

Definition at line 339 of file regexp.c.

References RE_compile_and_cache(), and RE_execute().

Referenced by nameicregexeq(), nameicregexne(), nameregexeq(), nameregexne(), texticregexeq(), texticregexne(), textregexeq(), and textregexne().

342 {
343  regex_t *re;
344 
345  /* Compile RE */
346  re = RE_compile_and_cache(text_re, cflags, collation);
347 
348  return RE_execute(re, dat, dat_len, nmatch, pmatch);
349 }
static bool RE_execute(regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:305
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
Definition: regex.h:55
static bool RE_execute ( regex_t re,
char *  dat,
int  dat_len,
int  nmatch,
regmatch_t pmatch 
)
static

Definition at line 305 of file regexp.c.

References palloc(), pfree(), pg_mb2wchar_with_len(), and RE_wchar_execute().

Referenced by RE_compile_and_execute(), and textregexsubstr().

307 {
308  pg_wchar *data;
309  int data_len;
310  bool match;
311 
312  /* Convert data string to wide characters */
313  data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
314  data_len = pg_mb2wchar_with_len(dat, data, dat_len);
315 
316  /* Perform RE match and return result */
317  match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
318 
319  pfree(data);
320  return match;
321 }
void pfree(void *pointer)
Definition: mcxt.c:950
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:734
void * palloc(Size size)
Definition: mcxt.c:849
static bool RE_wchar_execute(regex_t *re, pg_wchar *data, int data_len, int start_search, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:262
static bool RE_wchar_execute ( regex_t re,
pg_wchar data,
int  data_len,
int  start_search,
int  nmatch,
regmatch_t pmatch 
)
static

Definition at line 262 of file regexp.c.

References CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, NULL, pg_regerror(), pg_regexec(), REG_NOMATCH, and REG_OKAY.

Referenced by RE_execute(), and setup_regexp_matches().

264 {
265  int regexec_result;
266  char errMsg[100];
267 
268  /* Perform RE match and return result */
269  regexec_result = pg_regexec(re,
270  data,
271  data_len,
272  start_search,
273  NULL, /* no details */
274  nmatch,
275  pmatch,
276  0);
277 
278  if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
279  {
280  /* re failed??? */
282  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
283  ereport(ERROR,
284  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
285  errmsg("regular expression failed: %s", errMsg)));
286  }
287 
288  return (regexec_result == REG_OKAY);
289 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define REG_OKAY
Definition: regex.h:137
#define ERROR
Definition: elog.h:43
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
Definition: regexec.c:172
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define REG_NOMATCH
Definition: regex.h:138
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
char* regexp_fixed_prefix ( text text_re,
bool  case_insensitive,
Oid  collation,
bool exact 
)

Definition at line 1300 of file regexp.c.

References Assert, CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, free, NULL, palloc(), pg_database_encoding_max_length(), pg_regerror(), pg_regprefix(), pg_wchar2mb_with_len(), RE_compile_and_cache(), REG_ADVANCED, REG_EXACT, REG_ICASE, REG_NOMATCH, REG_PREFIX, and result.

Referenced by regex_fixed_prefix().

1302 {
1303  char *result;
1304  regex_t *re;
1305  int cflags;
1306  int re_result;
1307  pg_wchar *str;
1308  size_t slen;
1309  size_t maxlen;
1310  char errMsg[100];
1311 
1312  *exact = false; /* default result */
1313 
1314  /* Compile RE */
1315  cflags = REG_ADVANCED;
1316  if (case_insensitive)
1317  cflags |= REG_ICASE;
1318 
1319  re = RE_compile_and_cache(text_re, cflags, collation);
1320 
1321  /* Examine it to see if there's a fixed prefix */
1322  re_result = pg_regprefix(re, &str, &slen);
1323 
1324  switch (re_result)
1325  {
1326  case REG_NOMATCH:
1327  return NULL;
1328 
1329  case REG_PREFIX:
1330  /* continue with wchar conversion */
1331  break;
1332 
1333  case REG_EXACT:
1334  *exact = true;
1335  /* continue with wchar conversion */
1336  break;
1337 
1338  default:
1339  /* re failed??? */
1341  pg_regerror(re_result, re, errMsg, sizeof(errMsg));
1342  ereport(ERROR,
1343  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
1344  errmsg("regular expression failed: %s", errMsg)));
1345  break;
1346  }
1347 
1348  /* Convert pg_wchar result back to database encoding */
1349  maxlen = pg_database_encoding_max_length() * slen + 1;
1350  result = (char *) palloc(maxlen);
1351  slen = pg_wchar2mb_with_len(str, result, slen);
1352  Assert(slen < maxlen);
1353 
1354  free(str);
1355 
1356  return result;
1357 }
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define REG_PREFIX
Definition: regex.h:162
#define REG_ICASE
Definition: regex.h:106
int pg_regprefix(regex_t *re, chr **string, size_t *slength)
Definition: regprefix.c:46
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define REG_ADVANCED
Definition: regex.h:103
#define ereport(elevel, rest)
Definition: elog.h:122
unsigned int pg_wchar
Definition: mbprint.c:31
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define REG_EXACT
Definition: regex.h:163
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define REG_NOMATCH
Definition: regex.h:138
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:756
Definition: regex.h:55
Datum regexp_match ( PG_FUNCTION_ARGS  )

Definition at line 848 of file regexp.c.

References Assert, build_regexp_match_result(), regexp_matches_ctx::elems, ereport, errcode(), errhint(), errmsg(), ERROR, pg_re_flags::glob, regexp_matches_ctx::nmatches, regexp_matches_ctx::npatterns, regexp_matches_ctx::nulls, palloc(), parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_RETURN_DATUM, PG_RETURN_NULL, PointerGetDatum, and setup_regexp_matches().

Referenced by regexp_match_no_flags().

849 {
850  text *orig_str = PG_GETARG_TEXT_PP(0);
851  text *pattern = PG_GETARG_TEXT_PP(1);
852  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
853  pg_re_flags re_flags;
854  regexp_matches_ctx *matchctx;
855 
856  /* Determine options */
857  parse_re_flags(&re_flags, flags);
858  /* User mustn't specify 'g' */
859  if (re_flags.glob)
860  ereport(ERROR,
861  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
862  errmsg("regexp_match does not support the global option"),
863  errhint("Use the regexp_matches function instead.")));
864 
865  matchctx = setup_regexp_matches(orig_str, pattern, &re_flags,
866  PG_GET_COLLATION(), true, false);
867 
868  if (matchctx->nmatches == 0)
869  PG_RETURN_NULL();
870 
871  Assert(matchctx->nmatches == 1);
872 
873  /* Create workspace that build_regexp_match_result needs */
874  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
875  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
876 
878 }
int errhint(const char *fmt,...)
Definition: elog.c:987
Datum * elems
Definition: regexp.c:62
#define PointerGetDatum(X)
Definition: postgres.h:562
bool glob
Definition: regexp.c:48
int errcode(int sqlerrcode)
Definition: elog.c:575
bool * nulls
Definition: regexp.c:63
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:40
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1097
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define Assert(condition)
Definition: c.h:675
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:362
Definition: c.h:439
#define PG_RETURN_NULL()
Definition: fmgr.h:305
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate)
Definition: regexp.c:962
Datum regexp_match_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 882 of file regexp.c.

References regexp_match().

883 {
884  return regexp_match(fcinfo);
885 }
Datum regexp_match(PG_FUNCTION_ARGS)
Definition: regexp.c:848
Datum regexp_matches ( PG_FUNCTION_ARGS  )

Definition at line 892 of file regexp.c.

References build_regexp_match_result(), cleanup_regexp_matches(), regexp_matches_ctx::elems, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, regexp_matches_ctx::next_match, regexp_matches_ctx::nmatches, regexp_matches_ctx::npatterns, regexp_matches_ctx::nulls, palloc(), parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_P_COPY, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PointerGetDatum, setup_regexp_matches(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and FuncCallContext::user_fctx.

Referenced by regexp_matches_no_flags().

893 {
894  FuncCallContext *funcctx;
895  regexp_matches_ctx *matchctx;
896 
897  if (SRF_IS_FIRSTCALL())
898  {
899  text *pattern = PG_GETARG_TEXT_PP(1);
900  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
901  pg_re_flags re_flags;
902  MemoryContext oldcontext;
903 
904  funcctx = SRF_FIRSTCALL_INIT();
905  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
906 
907  /* Determine options */
908  parse_re_flags(&re_flags, flags);
909 
910  /* be sure to copy the input string into the multi-call ctx */
911  matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
912  &re_flags,
914  true, false);
915 
916  /* Pre-create workspace that build_regexp_match_result needs */
917  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
918  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
919 
920  MemoryContextSwitchTo(oldcontext);
921  funcctx->user_fctx = (void *) matchctx;
922  }
923 
924  funcctx = SRF_PERCALL_SETUP();
925  matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
926 
927  if (matchctx->next_match < matchctx->nmatches)
928  {
929  ArrayType *result_ary;
930 
931  result_ary = build_regexp_match_result(matchctx);
932  matchctx->next_match++;
933  SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
934  }
935 
936  /* release space in multi-call ctx to avoid intraquery memory leak */
937  cleanup_regexp_matches(matchctx);
938 
939  SRF_RETURN_DONE(funcctx);
940 }
Datum * elems
Definition: regexp.c:62
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool * nulls
Definition: regexp.c:63
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:279
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:40
static void cleanup_regexp_matches(regexp_matches_ctx *matchctx)
Definition: regexp.c:1082
uintptr_t Datum
Definition: postgres.h:372
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1097
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:362
Definition: c.h:439
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate)
Definition: regexp.c:962
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum regexp_matches_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 944 of file regexp.c.

References regexp_matches().

945 {
946  return regexp_matches(fcinfo);
947 }
Datum regexp_matches(PG_FUNCTION_ARGS)
Definition: regexp.c:892
Datum regexp_split_to_array ( PG_FUNCTION_ARGS  )

Definition at line 1207 of file regexp.c.

References accumArrayResult(), build_regexp_split_result(), CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, makeArrayResult(), regexp_matches_ctx::next_match, regexp_matches_ctx::nmatches, NULL, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_RETURN_ARRAYTYPE_P, setup_regexp_matches(), and TEXTOID.

Referenced by regexp_split_to_array_no_flags().

1208 {
1209  ArrayBuildState *astate = NULL;
1210  pg_re_flags re_flags;
1211  regexp_matches_ctx *splitctx;
1212 
1213  /* Determine options */
1215  /* User mustn't specify 'g' */
1216  if (re_flags.glob)
1217  ereport(ERROR,
1218  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1219  errmsg("regexp_split_to_array does not support the global option")));
1220  /* But we find all the matches anyway */
1221  re_flags.glob = true;
1222 
1224  PG_GETARG_TEXT_PP(1),
1225  &re_flags,
1226  PG_GET_COLLATION(),
1227  false, true);
1228 
1229  while (splitctx->next_match <= splitctx->nmatches)
1230  {
1231  astate = accumArrayResult(astate,
1232  build_regexp_split_result(splitctx),
1233  false,
1234  TEXTOID,
1236  splitctx->next_match++;
1237  }
1238 
1239  /*
1240  * We don't call cleanup_regexp_matches here; it would try to pfree the
1241  * input string, which we didn't copy. The space is not in a long-lived
1242  * memory context anyway.
1243  */
1244 
1246 }
#define TEXTOID
Definition: pg_type.h:324
bool glob
Definition: regexp.c:48
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:40
#define ERROR
Definition: elog.h:43
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define ereport(elevel, rest)
Definition: elog.h:122
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5055
#define NULL
Definition: c.h:229
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4991
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1262
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:362
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate)
Definition: regexp.c:962
Datum regexp_split_to_array_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1250 of file regexp.c.

References regexp_split_to_array().

1251 {
1252  return regexp_split_to_array(fcinfo);
1253 }
Datum regexp_split_to_array(PG_FUNCTION_ARGS)
Definition: regexp.c:1207
Datum regexp_split_to_table ( PG_FUNCTION_ARGS  )

Definition at line 1142 of file regexp.c.

References build_regexp_split_result(), cleanup_regexp_matches(), ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, regexp_matches_ctx::next_match, regexp_matches_ctx::nmatches, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_P_COPY, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, result, setup_regexp_matches(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and FuncCallContext::user_fctx.

Referenced by regexp_split_to_table_no_flags().

1143 {
1144  FuncCallContext *funcctx;
1145  regexp_matches_ctx *splitctx;
1146 
1147  if (SRF_IS_FIRSTCALL())
1148  {
1149  text *pattern = PG_GETARG_TEXT_PP(1);
1150  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1151  pg_re_flags re_flags;
1152  MemoryContext oldcontext;
1153 
1154  funcctx = SRF_FIRSTCALL_INIT();
1155  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1156 
1157  /* Determine options */
1158  parse_re_flags(&re_flags, flags);
1159  /* User mustn't specify 'g' */
1160  if (re_flags.glob)
1161  ereport(ERROR,
1162  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1163  errmsg("regexp_split_to_table does not support the global option")));
1164  /* But we find all the matches anyway */
1165  re_flags.glob = true;
1166 
1167  /* be sure to copy the input string into the multi-call ctx */
1168  splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1169  &re_flags,
1170  PG_GET_COLLATION(),
1171  false, true);
1172 
1173  MemoryContextSwitchTo(oldcontext);
1174  funcctx->user_fctx = (void *) splitctx;
1175  }
1176 
1177  funcctx = SRF_PERCALL_SETUP();
1178  splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
1179 
1180  if (splitctx->next_match <= splitctx->nmatches)
1181  {
1183 
1184  splitctx->next_match++;
1185  SRF_RETURN_NEXT(funcctx, result);
1186  }
1187 
1188  /* release space in multi-call ctx to avoid intraquery memory leak */
1189  cleanup_regexp_matches(splitctx);
1190 
1191  SRF_RETURN_DONE(funcctx);
1192 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
bool glob
Definition: regexp.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:279
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:40
#define ERROR
Definition: elog.h:43
static void cleanup_regexp_matches(regexp_matches_ctx *matchctx)
Definition: regexp.c:1082
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1262
void * user_fctx
Definition: funcapi.h:90
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:362
Definition: c.h:439
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate)
Definition: regexp.c:962
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum regexp_split_to_table_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1196 of file regexp.c.

References regexp_split_to_table().

1197 {
1198  return regexp_split_to_table(fcinfo);
1199 }
Datum regexp_split_to_table(PG_FUNCTION_ARGS)
Definition: regexp.c:1142
static regexp_matches_ctx * setup_regexp_matches ( text orig_str,
text pattern,
pg_re_flags flags,
Oid  collation,
bool  use_subpatterns,
bool  ignore_degenerate 
)
static

Definition at line 962 of file regexp.c.

References pg_re_flags::cflags, pg_re_flags::glob, i, regexp_matches_ctx::match_locs, regexp_matches_ctx::nmatches, regexp_matches_ctx::npatterns, regexp_matches_ctx::orig_str, palloc(), palloc0(), pfree(), pg_mb2wchar_with_len(), RE_compile_and_cache(), regex_t::re_nsub, RE_wchar_execute(), repalloc(), regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by regexp_match(), regexp_matches(), regexp_split_to_array(), and regexp_split_to_table().

966 {
967  regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
968  int orig_len;
969  pg_wchar *wide_str;
970  int wide_len;
971  regex_t *cpattern;
972  regmatch_t *pmatch;
973  int pmatch_len;
974  int array_len;
975  int array_idx;
976  int prev_match_end;
977  int start_search;
978 
979  /* save original string --- we'll extract result substrings from it */
980  matchctx->orig_str = orig_str;
981 
982  /* convert string to pg_wchar form for matching */
983  orig_len = VARSIZE_ANY_EXHDR(orig_str);
984  wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
985  wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
986 
987  /* set up the compiled pattern */
988  cpattern = RE_compile_and_cache(pattern, re_flags->cflags, collation);
989 
990  /* do we want to remember subpatterns? */
991  if (use_subpatterns && cpattern->re_nsub > 0)
992  {
993  matchctx->npatterns = cpattern->re_nsub;
994  pmatch_len = cpattern->re_nsub + 1;
995  }
996  else
997  {
998  use_subpatterns = false;
999  matchctx->npatterns = 1;
1000  pmatch_len = 1;
1001  }
1002 
1003  /* temporary output space for RE package */
1004  pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
1005 
1006  /* the real output space (grown dynamically if needed) */
1007  array_len = re_flags->glob ? 256 : 32;
1008  matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
1009  array_idx = 0;
1010 
1011  /* search for the pattern, perhaps repeatedly */
1012  prev_match_end = 0;
1013  start_search = 0;
1014  while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
1015  pmatch_len, pmatch))
1016  {
1017  /*
1018  * If requested, ignore degenerate matches, which are zero-length
1019  * matches occurring at the start or end of a string or just after a
1020  * previous match.
1021  */
1022  if (!ignore_degenerate ||
1023  (pmatch[0].rm_so < wide_len &&
1024  pmatch[0].rm_eo > prev_match_end))
1025  {
1026  /* enlarge output space if needed */
1027  while (array_idx + matchctx->npatterns * 2 > array_len)
1028  {
1029  array_len *= 2;
1030  matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
1031  sizeof(int) * array_len);
1032  }
1033 
1034  /* save this match's locations */
1035  if (use_subpatterns)
1036  {
1037  int i;
1038 
1039  for (i = 1; i <= matchctx->npatterns; i++)
1040  {
1041  matchctx->match_locs[array_idx++] = pmatch[i].rm_so;
1042  matchctx->match_locs[array_idx++] = pmatch[i].rm_eo;
1043  }
1044  }
1045  else
1046  {
1047  matchctx->match_locs[array_idx++] = pmatch[0].rm_so;
1048  matchctx->match_locs[array_idx++] = pmatch[0].rm_eo;
1049  }
1050  matchctx->nmatches++;
1051  }
1052  prev_match_end = pmatch[0].rm_eo;
1053 
1054  /* if not glob, stop after one match */
1055  if (!re_flags->glob)
1056  break;
1057 
1058  /*
1059  * Advance search position. Normally we start the next search at the
1060  * end of the previous match; but if the match was of zero length, we
1061  * have to advance by one character, or we'd just find the same match
1062  * again.
1063  */
1064  start_search = prev_match_end;
1065  if (pmatch[0].rm_so == pmatch[0].rm_eo)
1066  start_search++;
1067  if (start_search > wide_len)
1068  break;
1069  }
1070 
1071  /* Clean up temp storage */
1072  pfree(wide_str);
1073  pfree(pmatch);
1074 
1075  return matchctx;
1076 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
regoff_t rm_so
Definition: regex.h:85
int * match_locs
Definition: regexp.c:59
regoff_t rm_eo
Definition: regex.h:86
size_t re_nsub
Definition: regex.h:58
void pfree(void *pointer)
Definition: mcxt.c:950
text * orig_str
Definition: regexp.c:54
unsigned int pg_wchar
Definition: mbprint.c:31
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
void * palloc0(Size size)
Definition: mcxt.c:878
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:734
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
static bool RE_wchar_execute(regex_t *re, pg_wchar *data, int data_len, int start_search, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:262
int i
Definition: regex.h:55
Datum similar_escape ( PG_FUNCTION_ARGS  )

Definition at line 658 of file regexp.c.

References ereport, errcode(), errhint(), errmsg(), ERROR, NULL, palloc(), PG_ARGISNULL, PG_GETARG_TEXT_PP, pg_mblen(), pg_mbstrlen_with_len(), PG_RETURN_NULL, PG_RETURN_TEXT_P, result, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

659 {
660  text *pat_text;
661  text *esc_text;
662  text *result;
663  char *p,
664  *e,
665  *r;
666  int plen,
667  elen;
668  bool afterescape = false;
669  bool incharclass = false;
670  int nquotes = 0;
671 
672  /* This function is not strict, so must test explicitly */
673  if (PG_ARGISNULL(0))
674  PG_RETURN_NULL();
675  pat_text = PG_GETARG_TEXT_PP(0);
676  p = VARDATA_ANY(pat_text);
677  plen = VARSIZE_ANY_EXHDR(pat_text);
678  if (PG_ARGISNULL(1))
679  {
680  /* No ESCAPE clause provided; default to backslash as escape */
681  e = "\\";
682  elen = 1;
683  }
684  else
685  {
686  esc_text = PG_GETARG_TEXT_PP(1);
687  e = VARDATA_ANY(esc_text);
688  elen = VARSIZE_ANY_EXHDR(esc_text);
689  if (elen == 0)
690  e = NULL; /* no escape character */
691  else
692  {
693  int escape_mblen = pg_mbstrlen_with_len(e, elen);
694 
695  if (escape_mblen > 1)
696  ereport(ERROR,
697  (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
698  errmsg("invalid escape string"),
699  errhint("Escape string must be empty or one character.")));
700  }
701  }
702 
703  /*----------
704  * We surround the transformed input string with
705  * ^(?: ... )$
706  * which requires some explanation. We need "^" and "$" to force
707  * the pattern to match the entire input string as per SQL99 spec.
708  * The "(?:" and ")" are a non-capturing set of parens; we have to have
709  * parens in case the string contains "|", else the "^" and "$" will
710  * be bound into the first and last alternatives which is not what we
711  * want, and the parens must be non capturing because we don't want them
712  * to count when selecting output for SUBSTRING.
713  *----------
714  */
715 
716  /*
717  * We need room for the prefix/postfix plus as many as 3 output bytes per
718  * input byte; since the input is at most 1GB this can't overflow
719  */
720  result = (text *) palloc(VARHDRSZ + 6 + 3 * plen);
721  r = VARDATA(result);
722 
723  *r++ = '^';
724  *r++ = '(';
725  *r++ = '?';
726  *r++ = ':';
727 
728  while (plen > 0)
729  {
730  char pchar = *p;
731 
732  /*
733  * If both the escape character and the current character from the
734  * pattern are multi-byte, we need to take the slow path.
735  *
736  * But if one of them is single-byte, we can process the pattern one
737  * byte at a time, ignoring multi-byte characters. (This works
738  * because all server-encodings have the property that a valid
739  * multi-byte character representation cannot contain the
740  * representation of a valid single-byte character.)
741  */
742 
743  if (elen > 1)
744  {
745  int mblen = pg_mblen(p);
746 
747  if (mblen > 1)
748  {
749  /* slow, multi-byte path */
750  if (afterescape)
751  {
752  *r++ = '\\';
753  memcpy(r, p, mblen);
754  r += mblen;
755  afterescape = false;
756  }
757  else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
758  {
759  /* SQL99 escape character; do not send to output */
760  afterescape = true;
761  }
762  else
763  {
764  /*
765  * We know it's a multi-byte character, so we don't need
766  * to do all the comparisons to single-byte characters
767  * that we do below.
768  */
769  memcpy(r, p, mblen);
770  r += mblen;
771  }
772 
773  p += mblen;
774  plen -= mblen;
775 
776  continue;
777  }
778  }
779 
780  /* fast path */
781  if (afterescape)
782  {
783  if (pchar == '"' && !incharclass) /* for SUBSTRING patterns */
784  *r++ = ((nquotes++ % 2) == 0) ? '(' : ')';
785  else
786  {
787  *r++ = '\\';
788  *r++ = pchar;
789  }
790  afterescape = false;
791  }
792  else if (e && pchar == *e)
793  {
794  /* SQL99 escape character; do not send to output */
795  afterescape = true;
796  }
797  else if (incharclass)
798  {
799  if (pchar == '\\')
800  *r++ = '\\';
801  *r++ = pchar;
802  if (pchar == ']')
803  incharclass = false;
804  }
805  else if (pchar == '[')
806  {
807  *r++ = pchar;
808  incharclass = true;
809  }
810  else if (pchar == '%')
811  {
812  *r++ = '.';
813  *r++ = '*';
814  }
815  else if (pchar == '_')
816  *r++ = '.';
817  else if (pchar == '(')
818  {
819  /* convert to non-capturing parenthesis */
820  *r++ = '(';
821  *r++ = '?';
822  *r++ = ':';
823  }
824  else if (pchar == '\\' || pchar == '.' ||
825  pchar == '^' || pchar == '$')
826  {
827  *r++ = '\\';
828  *r++ = pchar;
829  }
830  else
831  *r++ = pchar;
832  p++, plen--;
833  }
834 
835  *r++ = ')';
836  *r++ = '$';
837 
838  SET_VARSIZE(result, r - ((char *) result));
839 
840  PG_RETURN_TEXT_P(result);
841 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
e
Definition: preproc-init.c:82
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum texticregexeq ( PG_FUNCTION_ARGS  )

Definition at line 527 of file regexp.c.

References NULL, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), REG_ADVANCED, REG_ICASE, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

528 {
529  text *s = PG_GETARG_TEXT_PP(0);
530  text *p = PG_GETARG_TEXT_PP(1);
531 
533  VARDATA_ANY(s),
537  0, NULL));
538 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum texticregexne ( PG_FUNCTION_ARGS  )

Definition at line 541 of file regexp.c.

References NULL, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), REG_ADVANCED, REG_ICASE, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

542 {
543  text *s = PG_GETARG_TEXT_PP(0);
544  text *p = PG_GETARG_TEXT_PP(1);
545 
547  VARDATA_ANY(s),
551  0, NULL));
552 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum textregexeq ( PG_FUNCTION_ARGS  )

Definition at line 464 of file regexp.c.

References NULL, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), REG_ADVANCED, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

465 {
466  text *s = PG_GETARG_TEXT_PP(0);
467  text *p = PG_GETARG_TEXT_PP(1);
468 
470  VARDATA_ANY(s),
472  REG_ADVANCED,
474  0, NULL));
475 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum textregexne ( PG_FUNCTION_ARGS  )

Definition at line 478 of file regexp.c.

References NULL, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, RE_compile_and_execute(), REG_ADVANCED, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

479 {
480  text *s = PG_GETARG_TEXT_PP(0);
481  text *p = PG_GETARG_TEXT_PP(1);
482 
484  VARDATA_ANY(s),
486  REG_ADVANCED,
488  0, NULL));
489 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NULL
Definition: c.h:229
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum textregexreplace ( PG_FUNCTION_ARGS  )

Definition at line 636 of file regexp.c.

References pg_re_flags::cflags, pg_re_flags::glob, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, RE_compile_and_cache(), and replace_text_regexp().

Referenced by execute_extension_script().

637 {
638  text *s = PG_GETARG_TEXT_PP(0);
639  text *p = PG_GETARG_TEXT_PP(1);
640  text *r = PG_GETARG_TEXT_PP(2);
641  text *opt = PG_GETARG_TEXT_PP(3);
642  regex_t *re;
643  pg_re_flags flags;
644 
645  parse_re_flags(&flags, opt);
646 
647  re = RE_compile_and_cache(p, flags.cflags, PG_GET_COLLATION());
648 
649  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, flags.glob));
650 }
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, bool glob)
Definition: varlena.c:3886
int cflags
Definition: regexp.c:47
bool glob
Definition: regexp.c:48
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:362
Definition: c.h:439
Definition: regex.h:55
Datum textregexreplace_noopt ( PG_FUNCTION_ARGS  )

Definition at line 619 of file regexp.c.

References PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, RE_compile_and_cache(), REG_ADVANCED, and replace_text_regexp().

620 {
621  text *s = PG_GETARG_TEXT_PP(0);
622  text *p = PG_GETARG_TEXT_PP(1);
623  text *r = PG_GETARG_TEXT_PP(2);
624  regex_t *re;
625 
627 
628  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, false));
629 }
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, bool glob)
Definition: varlena.c:3886
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define REG_ADVANCED
Definition: regex.h:103
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
Definition: c.h:439
Definition: regex.h:55
Datum textregexsubstr ( PG_FUNCTION_ARGS  )

Definition at line 560 of file regexp.c.

References DirectFunctionCall3, Int32GetDatum, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PointerGetDatum, RE_compile_and_cache(), RE_execute(), regex_t::re_nsub, REG_ADVANCED, regmatch_t::rm_eo, regmatch_t::rm_so, text_substr(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

561 {
562  text *s = PG_GETARG_TEXT_PP(0);
563  text *p = PG_GETARG_TEXT_PP(1);
564  regex_t *re;
565  regmatch_t pmatch[2];
566  int so,
567  eo;
568 
569  /* Compile RE */
571 
572  /*
573  * We pass two regmatch_t structs to get info about the overall match and
574  * the match for the first parenthesized subexpression (if any). If there
575  * is a parenthesized subexpression, we return what it matched; else
576  * return what the whole regexp matched.
577  */
578  if (!RE_execute(re,
580  2, pmatch))
581  PG_RETURN_NULL(); /* definitely no match */
582 
583  if (re->re_nsub > 0)
584  {
585  /* has parenthesized subexpressions, use the first one */
586  so = pmatch[1].rm_so;
587  eo = pmatch[1].rm_eo;
588  }
589  else
590  {
591  /* no parenthesized subexpression, use whole match */
592  so = pmatch[0].rm_so;
593  eo = pmatch[0].rm_eo;
594  }
595 
596  /*
597  * It is possible to have a match to the whole pattern but no match for a
598  * subexpression; for example 'foo(bar)?' is considered to match 'foo' but
599  * there is no subexpression match. So this extra test for match failure
600  * is not redundant.
601  */
602  if (so < 0 || eo < 0)
603  PG_RETURN_NULL();
604 
606  PointerGetDatum(s),
607  Int32GetDatum(so + 1),
608  Int32GetDatum(eo - so));
609 }
static bool RE_execute(regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:305
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PointerGetDatum(X)
Definition: postgres.h:562
regoff_t rm_so
Definition: regex.h:85
#define PG_GET_COLLATION()
Definition: fmgr.h:163
regoff_t rm_eo
Definition: regex.h:86
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
size_t re_nsub
Definition: regex.h:58
#define REG_ADVANCED
Definition: regex.h:103
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:781
#define Int32GetDatum(X)
Definition: postgres.h:485
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Definition: regex.h:55
#define PG_RETURN_NULL()
Definition: fmgr.h:305

Variable Documentation

int num_res = 0
static

Definition at line 105 of file regexp.c.

Referenced by RE_compile_and_cache().

cached_re_str re_array[MAX_CACHED_RES]
static

Definition at line 106 of file regexp.c.