PostgreSQL Source Code  git master
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/memutils.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, bool fetching_unmatched)
 
static ArrayTypebuild_regexp_match_result (regexp_matches_ctx *matchctx)
 
static Datum build_regexp_split_result (regexp_matches_ctx *splitctx)
 
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)
 
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

◆ MAX_CACHED_RES

#define MAX_CACHED_RES   32

Definition at line 96 of file regexp.c.

Referenced by RE_compile_and_cache().

◆ PG_GETARG_TEXT_PP_IF_EXISTS

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

Typedef Documentation

◆ cached_re_str

◆ pg_re_flags

◆ regexp_matches_ctx

Function Documentation

◆ build_regexp_match_result()

static ArrayType * build_regexp_match_result ( regexp_matches_ctx matchctx)
static

Definition at line 1228 of file regexp.c.

References Assert, buf, construct_md_array(), regexp_matches_ctx::conv_buf, regexp_matches_ctx::conv_bufsiz, cstring_to_text_with_len(), 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, PG_USED_FOR_ASSERTS_ONLY, pg_wchar2mb_with_len(), PointerGetDatum, text_substr(), and regexp_matches_ctx::wide_str.

Referenced by regexp_match(), and regexp_matches().

1229 {
1230  char *buf = matchctx->conv_buf;
1231  int bufsiz PG_USED_FOR_ASSERTS_ONLY = matchctx->conv_bufsiz;
1232  Datum *elems = matchctx->elems;
1233  bool *nulls = matchctx->nulls;
1234  int dims[1];
1235  int lbs[1];
1236  int loc;
1237  int i;
1238 
1239  /* Extract matching substrings from the original string */
1240  loc = matchctx->next_match * matchctx->npatterns * 2;
1241  for (i = 0; i < matchctx->npatterns; i++)
1242  {
1243  int so = matchctx->match_locs[loc++];
1244  int eo = matchctx->match_locs[loc++];
1245 
1246  if (so < 0 || eo < 0)
1247  {
1248  elems[i] = (Datum) 0;
1249  nulls[i] = true;
1250  }
1251  else if (buf)
1252  {
1253  int len = pg_wchar2mb_with_len(matchctx->wide_str + so,
1254  buf,
1255  eo - so);
1256 
1257  Assert(len < bufsiz);
1258  elems[i] = PointerGetDatum(cstring_to_text_with_len(buf, len));
1259  nulls[i] = false;
1260  }
1261  else
1262  {
1264  PointerGetDatum(matchctx->orig_str),
1265  Int32GetDatum(so + 1),
1266  Int32GetDatum(eo - so));
1267  nulls[i] = false;
1268  }
1269  }
1270 
1271  /* And form an array */
1272  dims[0] = matchctx->npatterns;
1273  lbs[0] = 1;
1274  /* XXX: this hardcodes assumptions about the text type */
1275  return construct_md_array(elems, nulls, 1, dims, lbs,
1276  TEXTOID, -1, false, 'i');
1277 }
Datum * elems
Definition: regexp.c:63
#define PointerGetDatum(X)
Definition: postgres.h:556
bool * nulls
Definition: regexp.c:64
int * match_locs
Definition: regexp.c:60
char * conv_buf
Definition: regexp.c:66
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:183
text * orig_str
Definition: regexp.c:55
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:620
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:803
uintptr_t Datum
Definition: postgres.h:367
pg_wchar * wide_str
Definition: regexp.c:65
#define Assert(condition)
Definition: c.h:732
#define Int32GetDatum(X)
Definition: postgres.h:479
int i
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:737
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:123
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:3326

◆ build_regexp_split_result()

static Datum build_regexp_split_result ( regexp_matches_ctx splitctx)
static

Definition at line 1400 of file regexp.c.

References Assert, buf, regexp_matches_ctx::conv_buf, regexp_matches_ctx::conv_bufsiz, cstring_to_text_with_len(), DirectFunctionCall3, elog, endpos, ERROR, Int32GetDatum, regexp_matches_ctx::match_locs, regexp_matches_ctx::next_match, regexp_matches_ctx::orig_str, PG_USED_FOR_ASSERTS_ONLY, pg_wchar2mb_with_len(), PointerGetDatum, startpos, text_substr(), and regexp_matches_ctx::wide_str.

Referenced by regexp_split_to_array(), and regexp_split_to_table().

1401 {
1402  char *buf = splitctx->conv_buf;
1403  int startpos;
1404  int endpos;
1405 
1406  if (splitctx->next_match > 0)
1407  startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
1408  else
1409  startpos = 0;
1410  if (startpos < 0)
1411  elog(ERROR, "invalid match ending position");
1412 
1413  if (buf)
1414  {
1415  int bufsiz PG_USED_FOR_ASSERTS_ONLY = splitctx->conv_bufsiz;
1416  int len;
1417 
1418  endpos = splitctx->match_locs[splitctx->next_match * 2];
1419  if (endpos < startpos)
1420  elog(ERROR, "invalid match starting position");
1421  len = pg_wchar2mb_with_len(splitctx->wide_str + startpos,
1422  buf,
1423  endpos - startpos);
1424  Assert(len < bufsiz);
1425  return PointerGetDatum(cstring_to_text_with_len(buf, len));
1426  }
1427  else
1428  {
1429  endpos = splitctx->match_locs[splitctx->next_match * 2];
1430  if (endpos < startpos)
1431  elog(ERROR, "invalid match starting position");
1433  PointerGetDatum(splitctx->orig_str),
1434  Int32GetDatum(startpos + 1),
1435  Int32GetDatum(endpos - startpos));
1436  }
1437 }
#define PointerGetDatum(X)
Definition: postgres.h:556
int * match_locs
Definition: regexp.c:60
char * conv_buf
Definition: regexp.c:66
#define ERROR
Definition: elog.h:43
static XLogRecPtr endpos
Definition: pg_receivewal.c:48
static char * buf
Definition: pg_test_fsync.c:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:183
text * orig_str
Definition: regexp.c:55
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:620
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:803
pg_wchar * wide_str
Definition: regexp.c:65
#define Assert(condition)
Definition: c.h:732
static XLogRecPtr startpos
#define Int32GetDatum(X)
Definition: postgres.h:479
#define elog(elevel,...)
Definition: elog.h:226
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:737
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:123

◆ nameicregexeq()

Datum nameicregexeq ( PG_FUNCTION_ARGS  )

Definition at line 503 of file regexp.c.

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

504 {
505  Name n = PG_GETARG_NAME(0);
506  text *p = PG_GETARG_TEXT_PP(1);
507 
509  NameStr(*n),
510  strlen(NameStr(*n)),
513  0, NULL));
514 }
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
Definition: c.h:603
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ nameicregexne()

Datum nameicregexne ( PG_FUNCTION_ARGS  )

Definition at line 517 of file regexp.c.

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

518 {
519  Name n = PG_GETARG_NAME(0);
520  text *p = PG_GETARG_TEXT_PP(1);
521 
523  NameStr(*n),
524  strlen(NameStr(*n)),
527  0, NULL));
528 }
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
Definition: c.h:603
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ nameregexeq()

Datum nameregexeq ( PG_FUNCTION_ARGS  )

Definition at line 440 of file regexp.c.

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

441 {
442  Name n = PG_GETARG_NAME(0);
443  text *p = PG_GETARG_TEXT_PP(1);
444 
446  NameStr(*n),
447  strlen(NameStr(*n)),
448  REG_ADVANCED,
450  0, NULL));
451 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
Definition: c.h:603
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ nameregexne()

Datum nameregexne ( PG_FUNCTION_ARGS  )

Definition at line 454 of file regexp.c.

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

455 {
456  Name n = PG_GETARG_NAME(0);
457  text *p = PG_GETARG_TEXT_PP(1);
458 
460  NameStr(*n),
461  strlen(NameStr(*n)),
462  REG_ADVANCED,
464  0, NULL));
465 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
Definition: c.h:603
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ parse_re_flags()

static void parse_re_flags ( pg_re_flags flags,
text opts 
)
static

Definition at line 366 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().

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

◆ RE_compile_and_cache()

regex_t* RE_compile_and_cache ( text text_re,
int  cflags,
Oid  collation 
)

Definition at line 137 of file regexp.c.

References Assert, pg_re_flags::cflags, 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, 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().

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

◆ RE_compile_and_execute()

bool RE_compile_and_execute ( text text_re,
char *  dat,
int  dat_len,
int  cflags,
Oid  collation,
int  nmatch,
regmatch_t pmatch 
)

Definition at line 343 of file regexp.c.

References RE_compile_and_cache(), and RE_execute().

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

346 {
347  regex_t *re;
348 
349  /* Compile RE */
350  re = RE_compile_and_cache(text_re, cflags, collation);
351 
352  return RE_execute(re, dat, dat_len, nmatch, pmatch);
353 }
static bool RE_execute(regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:309
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:137
Definition: regex.h:55

◆ RE_execute()

static bool RE_execute ( regex_t re,
char *  dat,
int  dat_len,
int  nmatch,
regmatch_t pmatch 
)
static

Definition at line 309 of file regexp.c.

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

Referenced by RE_compile_and_execute(), and textregexsubstr().

311 {
312  pg_wchar *data;
313  int data_len;
314  bool match;
315 
316  /* Convert data string to wide characters */
317  data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
318  data_len = pg_mb2wchar_with_len(dat, data, dat_len);
319 
320  /* Perform RE match and return result */
321  match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
322 
323  pfree(data);
324  return match;
325 }
void pfree(void *pointer)
Definition: mcxt.c:1031
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:715
void * palloc(Size size)
Definition: mcxt.c:924
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:266

◆ RE_wchar_execute()

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 266 of file regexp.c.

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

Referenced by RE_execute(), and setup_regexp_matches().

268 {
269  int regexec_result;
270  char errMsg[100];
271 
272  /* Perform RE match and return result */
273  regexec_result = pg_regexec(re,
274  data,
275  data_len,
276  start_search,
277  NULL, /* no details */
278  nmatch,
279  pmatch,
280  0);
281 
282  if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
283  {
284  /* re failed??? */
286  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
287  ereport(ERROR,
288  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
289  errmsg("regular expression failed: %s", errMsg)));
290  }
291 
292  return (regexec_result == REG_OKAY);
293 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#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:141
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:784
#define REG_NOMATCH
Definition: regex.h:138
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ regexp_fixed_prefix()

char* regexp_fixed_prefix ( text text_re,
bool  case_insensitive,
Oid  collation,
bool exact 
)

Definition at line 1446 of file regexp.c.

References Assert, pg_re_flags::cflags, CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, free, 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 generate_unaccent_rules::str.

Referenced by regex_fixed_prefix().

1448 {
1449  char *result;
1450  regex_t *re;
1451  int cflags;
1452  int re_result;
1453  pg_wchar *str;
1454  size_t slen;
1455  size_t maxlen;
1456  char errMsg[100];
1457 
1458  *exact = false; /* default result */
1459 
1460  /* Compile RE */
1461  cflags = REG_ADVANCED;
1462  if (case_insensitive)
1463  cflags |= REG_ICASE;
1464 
1465  re = RE_compile_and_cache(text_re, cflags, collation);
1466 
1467  /* Examine it to see if there's a fixed prefix */
1468  re_result = pg_regprefix(re, &str, &slen);
1469 
1470  switch (re_result)
1471  {
1472  case REG_NOMATCH:
1473  return NULL;
1474 
1475  case REG_PREFIX:
1476  /* continue with wchar conversion */
1477  break;
1478 
1479  case REG_EXACT:
1480  *exact = true;
1481  /* continue with wchar conversion */
1482  break;
1483 
1484  default:
1485  /* re failed??? */
1487  pg_regerror(re_result, re, errMsg, sizeof(errMsg));
1488  ereport(ERROR,
1489  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
1490  errmsg("regular expression failed: %s", errMsg)));
1491  break;
1492  }
1493 
1494  /* Convert pg_wchar result back to database encoding */
1495  maxlen = pg_database_encoding_max_length() * slen + 1;
1496  result = (char *) palloc(maxlen);
1497  slen = pg_wchar2mb_with_len(str, result, slen);
1498  Assert(slen < maxlen);
1499 
1500  free(str);
1501 
1502  return result;
1503 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#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:1881
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:141
unsigned int pg_wchar
Definition: mbprint.c:31
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:732
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:137
#define REG_EXACT
Definition: regex.h:163
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define REG_NOMATCH
Definition: regex.h:138
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:737
Definition: regex.h:55

◆ regexp_match()

Datum regexp_match ( PG_FUNCTION_ARGS  )

Definition at line 909 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().

910 {
911  text *orig_str = PG_GETARG_TEXT_PP(0);
912  text *pattern = PG_GETARG_TEXT_PP(1);
913  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
914  pg_re_flags re_flags;
915  regexp_matches_ctx *matchctx;
916 
917  /* Determine options */
918  parse_re_flags(&re_flags, flags);
919  /* User mustn't specify 'g' */
920  if (re_flags.glob)
921  ereport(ERROR,
922  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
923  /* translator: %s is a SQL function name */
924  errmsg("%s does not support the \"global\" option",
925  "regexp_match()"),
926  errhint("Use the regexp_matches function instead.")));
927 
928  matchctx = setup_regexp_matches(orig_str, pattern, &re_flags,
929  PG_GET_COLLATION(), true, false, false);
930 
931  if (matchctx->nmatches == 0)
932  PG_RETURN_NULL();
933 
934  Assert(matchctx->nmatches == 1);
935 
936  /* Create workspace that build_regexp_match_result needs */
937  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
938  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
939 
941 }
int errhint(const char *fmt,...)
Definition: elog.c:974
Datum * elems
Definition: regexp.c:63
#define PointerGetDatum(X)
Definition: postgres.h:556
bool glob
Definition: regexp.c:49
int errcode(int sqlerrcode)
Definition: elog.c:570
bool * nulls
Definition: regexp.c:64
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:43
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1027
#define ereport(elevel, rest)
Definition: elog.h:141
uintptr_t Datum
Definition: postgres.h:367
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1228
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define Assert(condition)
Definition: c.h:732
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:366
Definition: c.h:549
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ regexp_match_no_flags()

Datum regexp_match_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 945 of file regexp.c.

References regexp_match().

946 {
947  return regexp_match(fcinfo);
948 }
Datum regexp_match(PG_FUNCTION_ARGS)
Definition: regexp.c:909

◆ regexp_matches()

Datum regexp_matches ( PG_FUNCTION_ARGS  )

Definition at line 955 of file regexp.c.

References build_regexp_match_result(), 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().

956 {
957  FuncCallContext *funcctx;
958  regexp_matches_ctx *matchctx;
959 
960  if (SRF_IS_FIRSTCALL())
961  {
962  text *pattern = PG_GETARG_TEXT_PP(1);
963  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
964  pg_re_flags re_flags;
965  MemoryContext oldcontext;
966 
967  funcctx = SRF_FIRSTCALL_INIT();
968  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
969 
970  /* Determine options */
971  parse_re_flags(&re_flags, flags);
972 
973  /* be sure to copy the input string into the multi-call ctx */
974  matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
975  &re_flags,
977  true, false, false);
978 
979  /* Pre-create workspace that build_regexp_match_result needs */
980  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
981  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
982 
983  MemoryContextSwitchTo(oldcontext);
984  funcctx->user_fctx = (void *) matchctx;
985  }
986 
987  funcctx = SRF_PERCALL_SETUP();
988  matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
989 
990  if (matchctx->next_match < matchctx->nmatches)
991  {
992  ArrayType *result_ary;
993 
994  result_ary = build_regexp_match_result(matchctx);
995  matchctx->next_match++;
996  SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
997  }
998 
999  SRF_RETURN_DONE(funcctx);
1000 }
Datum * elems
Definition: regexp.c:63
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool * nulls
Definition: regexp.c:64
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1027
uintptr_t Datum
Definition: postgres.h:367
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1228
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
void * user_fctx
Definition: funcapi.h:83
void * palloc(Size size)
Definition: mcxt.c:924
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:366
Definition: c.h:549
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ regexp_matches_no_flags()

Datum regexp_matches_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1004 of file regexp.c.

References regexp_matches().

1005 {
1006  return regexp_matches(fcinfo);
1007 }
Datum regexp_matches(PG_FUNCTION_ARGS)
Definition: regexp.c:955

◆ regexp_split_to_array()

Datum regexp_split_to_array ( PG_FUNCTION_ARGS  )

Definition at line 1349 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, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_RETURN_ARRAYTYPE_P, and setup_regexp_matches().

Referenced by regexp_split_to_array_no_flags().

1350 {
1351  ArrayBuildState *astate = NULL;
1352  pg_re_flags re_flags;
1353  regexp_matches_ctx *splitctx;
1354 
1355  /* Determine options */
1357  /* User mustn't specify 'g' */
1358  if (re_flags.glob)
1359  ereport(ERROR,
1360  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1361  /* translator: %s is a SQL function name */
1362  errmsg("%s does not support the \"global\" option",
1363  "regexp_split_to_array()")));
1364  /* But we find all the matches anyway */
1365  re_flags.glob = true;
1366 
1368  PG_GETARG_TEXT_PP(1),
1369  &re_flags,
1370  PG_GET_COLLATION(),
1371  false, true, true);
1372 
1373  while (splitctx->next_match <= splitctx->nmatches)
1374  {
1375  astate = accumArrayResult(astate,
1376  build_regexp_split_result(splitctx),
1377  false,
1378  TEXTOID,
1380  splitctx->next_match++;
1381  }
1382 
1384 }
bool glob
Definition: regexp.c:49
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:43
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1027
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define ereport(elevel, rest)
Definition: elog.h:141
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5117
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5053
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1400
int errmsg(const char *fmt,...)
Definition: elog.c:784
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:366

◆ regexp_split_to_array_no_flags()

Datum regexp_split_to_array_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1388 of file regexp.c.

References regexp_split_to_array().

1389 {
1390  return regexp_split_to_array(fcinfo);
1391 }
Datum regexp_split_to_array(PG_FUNCTION_ARGS)
Definition: regexp.c:1349

◆ regexp_split_to_table()

Datum regexp_split_to_table ( PG_FUNCTION_ARGS  )

Definition at line 1285 of file regexp.c.

References build_regexp_split_result(), 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, 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().

1286 {
1287  FuncCallContext *funcctx;
1288  regexp_matches_ctx *splitctx;
1289 
1290  if (SRF_IS_FIRSTCALL())
1291  {
1292  text *pattern = PG_GETARG_TEXT_PP(1);
1293  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1294  pg_re_flags re_flags;
1295  MemoryContext oldcontext;
1296 
1297  funcctx = SRF_FIRSTCALL_INIT();
1298  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1299 
1300  /* Determine options */
1301  parse_re_flags(&re_flags, flags);
1302  /* User mustn't specify 'g' */
1303  if (re_flags.glob)
1304  ereport(ERROR,
1305  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1306  /* translator: %s is a SQL function name */
1307  errmsg("%s does not support the \"global\" option",
1308  "regexp_split_to_table()")));
1309  /* But we find all the matches anyway */
1310  re_flags.glob = true;
1311 
1312  /* be sure to copy the input string into the multi-call ctx */
1313  splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1314  &re_flags,
1315  PG_GET_COLLATION(),
1316  false, true, true);
1317 
1318  MemoryContextSwitchTo(oldcontext);
1319  funcctx->user_fctx = (void *) splitctx;
1320  }
1321 
1322  funcctx = SRF_PERCALL_SETUP();
1323  splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
1324 
1325  if (splitctx->next_match <= splitctx->nmatches)
1326  {
1327  Datum result = build_regexp_split_result(splitctx);
1328 
1329  splitctx->next_match++;
1330  SRF_RETURN_NEXT(funcctx, result);
1331  }
1332 
1333  SRF_RETURN_DONE(funcctx);
1334 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
bool glob
Definition: regexp.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:43
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1027
#define ereport(elevel, rest)
Definition: elog.h:141
uintptr_t Datum
Definition: postgres.h:367
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1400
void * user_fctx
Definition: funcapi.h:83
int errmsg(const char *fmt,...)
Definition: elog.c:784
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:366
Definition: c.h:549
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ regexp_split_to_table_no_flags()

Datum regexp_split_to_table_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1338 of file regexp.c.

References regexp_split_to_table().

1339 {
1340  return regexp_split_to_table(fcinfo);
1341 }
Datum regexp_split_to_table(PG_FUNCTION_ARGS)
Definition: regexp.c:1285

◆ setup_regexp_matches()

static regexp_matches_ctx * setup_regexp_matches ( text orig_str,
text pattern,
pg_re_flags flags,
Oid  collation,
bool  use_subpatterns,
bool  ignore_degenerate,
bool  fetching_unmatched 
)
static

Definition at line 1027 of file regexp.c.

References pg_re_flags::cflags, regexp_matches_ctx::conv_buf, regexp_matches_ctx::conv_bufsiz, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, i, regexp_matches_ctx::match_locs, MaxAllocSize, regexp_matches_ctx::nmatches, regexp_matches_ctx::npatterns, regexp_matches_ctx::orig_str, palloc(), palloc0(), pfree(), pg_database_encoding_max_length(), 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, VARSIZE_ANY_EXHDR, and regexp_matches_ctx::wide_str.

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

1032 {
1033  regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
1034  int eml = pg_database_encoding_max_length();
1035  int orig_len;
1036  pg_wchar *wide_str;
1037  int wide_len;
1038  regex_t *cpattern;
1039  regmatch_t *pmatch;
1040  int pmatch_len;
1041  int array_len;
1042  int array_idx;
1043  int prev_match_end;
1044  int prev_valid_match_end;
1045  int start_search;
1046  int maxlen = 0; /* largest fetch length in characters */
1047 
1048  /* save original string --- we'll extract result substrings from it */
1049  matchctx->orig_str = orig_str;
1050 
1051  /* convert string to pg_wchar form for matching */
1052  orig_len = VARSIZE_ANY_EXHDR(orig_str);
1053  wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
1054  wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
1055 
1056  /* set up the compiled pattern */
1057  cpattern = RE_compile_and_cache(pattern, re_flags->cflags, collation);
1058 
1059  /* do we want to remember subpatterns? */
1060  if (use_subpatterns && cpattern->re_nsub > 0)
1061  {
1062  matchctx->npatterns = cpattern->re_nsub;
1063  pmatch_len = cpattern->re_nsub + 1;
1064  }
1065  else
1066  {
1067  use_subpatterns = false;
1068  matchctx->npatterns = 1;
1069  pmatch_len = 1;
1070  }
1071 
1072  /* temporary output space for RE package */
1073  pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
1074 
1075  /*
1076  * the real output space (grown dynamically if needed)
1077  *
1078  * use values 2^n-1, not 2^n, so that we hit the limit at 2^28-1 rather
1079  * than at 2^27
1080  */
1081  array_len = re_flags->glob ? 255 : 31;
1082  matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
1083  array_idx = 0;
1084 
1085  /* search for the pattern, perhaps repeatedly */
1086  prev_match_end = 0;
1087  prev_valid_match_end = 0;
1088  start_search = 0;
1089  while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
1090  pmatch_len, pmatch))
1091  {
1092  /*
1093  * If requested, ignore degenerate matches, which are zero-length
1094  * matches occurring at the start or end of a string or just after a
1095  * previous match.
1096  */
1097  if (!ignore_degenerate ||
1098  (pmatch[0].rm_so < wide_len &&
1099  pmatch[0].rm_eo > prev_match_end))
1100  {
1101  /* enlarge output space if needed */
1102  while (array_idx + matchctx->npatterns * 2 + 1 > array_len)
1103  {
1104  array_len += array_len + 1; /* 2^n-1 => 2^(n+1)-1 */
1105  if (array_len > MaxAllocSize / sizeof(int))
1106  ereport(ERROR,
1107  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1108  errmsg("too many regular expression matches")));
1109  matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
1110  sizeof(int) * array_len);
1111  }
1112 
1113  /* save this match's locations */
1114  if (use_subpatterns)
1115  {
1116  int i;
1117 
1118  for (i = 1; i <= matchctx->npatterns; i++)
1119  {
1120  int so = pmatch[i].rm_so;
1121  int eo = pmatch[i].rm_eo;
1122 
1123  matchctx->match_locs[array_idx++] = so;
1124  matchctx->match_locs[array_idx++] = eo;
1125  if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
1126  maxlen = (eo - so);
1127  }
1128  }
1129  else
1130  {
1131  int so = pmatch[0].rm_so;
1132  int eo = pmatch[0].rm_eo;
1133 
1134  matchctx->match_locs[array_idx++] = so;
1135  matchctx->match_locs[array_idx++] = eo;
1136  if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
1137  maxlen = (eo - so);
1138  }
1139  matchctx->nmatches++;
1140 
1141  /*
1142  * check length of unmatched portion between end of previous valid
1143  * (nondegenerate, or degenerate but not ignored) match and start
1144  * of current one
1145  */
1146  if (fetching_unmatched &&
1147  pmatch[0].rm_so >= 0 &&
1148  (pmatch[0].rm_so - prev_valid_match_end) > maxlen)
1149  maxlen = (pmatch[0].rm_so - prev_valid_match_end);
1150  prev_valid_match_end = pmatch[0].rm_eo;
1151  }
1152  prev_match_end = pmatch[0].rm_eo;
1153 
1154  /* if not glob, stop after one match */
1155  if (!re_flags->glob)
1156  break;
1157 
1158  /*
1159  * Advance search position. Normally we start the next search at the
1160  * end of the previous match; but if the match was of zero length, we
1161  * have to advance by one character, or we'd just find the same match
1162  * again.
1163  */
1164  start_search = prev_match_end;
1165  if (pmatch[0].rm_so == pmatch[0].rm_eo)
1166  start_search++;
1167  if (start_search > wide_len)
1168  break;
1169  }
1170 
1171  /*
1172  * check length of unmatched portion between end of last match and end of
1173  * input string
1174  */
1175  if (fetching_unmatched &&
1176  (wide_len - prev_valid_match_end) > maxlen)
1177  maxlen = (wide_len - prev_valid_match_end);
1178 
1179  /*
1180  * Keep a note of the end position of the string for the benefit of
1181  * splitting code.
1182  */
1183  matchctx->match_locs[array_idx] = wide_len;
1184 
1185  if (eml > 1)
1186  {
1187  int64 maxsiz = eml * (int64) maxlen;
1188  int conv_bufsiz;
1189 
1190  /*
1191  * Make the conversion buffer large enough for any substring of
1192  * interest.
1193  *
1194  * Worst case: assume we need the maximum size (maxlen*eml), but take
1195  * advantage of the fact that the original string length in bytes is
1196  * an upper bound on the byte length of any fetched substring (and we
1197  * know that len+1 is safe to allocate because the varlena header is
1198  * longer than 1 byte).
1199  */
1200  if (maxsiz > orig_len)
1201  conv_bufsiz = orig_len + 1;
1202  else
1203  conv_bufsiz = maxsiz + 1; /* safe since maxsiz < 2^30 */
1204 
1205  matchctx->conv_buf = palloc(conv_bufsiz);
1206  matchctx->conv_bufsiz = conv_bufsiz;
1207  matchctx->wide_str = wide_str;
1208  }
1209  else
1210  {
1211  /* No need to keep the wide string if we're in a single-byte charset. */
1212  pfree(wide_str);
1213  matchctx->wide_str = NULL;
1214  matchctx->conv_buf = NULL;
1215  matchctx->conv_bufsiz = 0;
1216  }
1217 
1218  /* Clean up temp storage */
1219  pfree(pmatch);
1220 
1221  return matchctx;
1222 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
regoff_t rm_so
Definition: regex.h:85
int errcode(int sqlerrcode)
Definition: elog.c:570
int * match_locs
Definition: regexp.c:60
char * conv_buf
Definition: regexp.c:66
regoff_t rm_eo
Definition: regex.h:86
size_t re_nsub
Definition: regex.h:58
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
text * orig_str
Definition: regexp.c:55
#define ereport(elevel, rest)
Definition: elog.h:141
unsigned int pg_wchar
Definition: mbprint.c:31
#define MaxAllocSize
Definition: memutils.h:40
void * palloc0(Size size)
Definition: mcxt.c:955
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:715
pg_wchar * wide_str
Definition: regexp.c:65
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:137
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
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:266
int i
Definition: regex.h:55

◆ similar_escape()

Datum similar_escape ( PG_FUNCTION_ARGS  )

Definition at line 662 of file regexp.c.

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

663 {
664  text *pat_text;
665  text *esc_text;
666  text *result;
667  char *p,
668  *e,
669  *r;
670  int plen,
671  elen;
672  bool afterescape = false;
673  bool incharclass = false;
674  int nquotes = 0;
675 
676  /* This function is not strict, so must test explicitly */
677  if (PG_ARGISNULL(0))
678  PG_RETURN_NULL();
679  pat_text = PG_GETARG_TEXT_PP(0);
680  p = VARDATA_ANY(pat_text);
681  plen = VARSIZE_ANY_EXHDR(pat_text);
682  if (PG_ARGISNULL(1))
683  {
684  /* No ESCAPE clause provided; default to backslash as escape */
685  e = "\\";
686  elen = 1;
687  }
688  else
689  {
690  esc_text = PG_GETARG_TEXT_PP(1);
691  e = VARDATA_ANY(esc_text);
692  elen = VARSIZE_ANY_EXHDR(esc_text);
693  if (elen == 0)
694  e = NULL; /* no escape character */
695  else
696  {
697  int escape_mblen = pg_mbstrlen_with_len(e, elen);
698 
699  if (escape_mblen > 1)
700  ereport(ERROR,
701  (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
702  errmsg("invalid escape string"),
703  errhint("Escape string must be empty or one character.")));
704  }
705  }
706 
707  /*----------
708  * We surround the transformed input string with
709  * ^(?: ... )$
710  * which requires some explanation. We need "^" and "$" to force
711  * the pattern to match the entire input string as per the SQL spec.
712  * The "(?:" and ")" are a non-capturing set of parens; we have to have
713  * parens in case the string contains "|", else the "^" and "$" will
714  * be bound into the first and last alternatives which is not what we
715  * want, and the parens must be non capturing because we don't want them
716  * to count when selecting output for SUBSTRING.
717  *
718  * When the pattern is divided into three parts by escape-double-quotes,
719  * what we emit is
720  * ^(?:part1){1,1}?(part2){1,1}(?:part3)$
721  * which requires even more explanation. The "{1,1}?" on part1 makes it
722  * non-greedy so that it will match the smallest possible amount of text
723  * not the largest, as required by SQL. The plain parens around part2
724  * are capturing parens so that that part is what controls the result of
725  * SUBSTRING. The "{1,1}" forces part2 to be greedy, so that it matches
726  * the largest possible amount of text; hence part3 must match the
727  * smallest amount of text, as required by SQL. We don't need an explicit
728  * greediness marker on part3. Note that this also confines the effects
729  * of any "|" characters to the respective part, which is what we want.
730  *
731  * The SQL spec says that SUBSTRING's pattern must contain exactly two
732  * escape-double-quotes, but we only complain if there's more than two.
733  * With none, we act as though part1 and part3 are empty; with one, we
734  * act as though part3 is empty. Both behaviors fall out of omitting
735  * the relevant part separators in the above expansion. If the result
736  * of this function is used in a plain regexp match (SIMILAR TO), the
737  * escape-double-quotes have no effect on the match behavior.
738  *----------
739  */
740 
741  /*
742  * We need room for the prefix/postfix and part separators, plus as many
743  * as 3 output bytes per input byte; since the input is at most 1GB this
744  * can't overflow size_t.
745  */
746  result = (text *) palloc(VARHDRSZ + 23 + 3 * (size_t) plen);
747  r = VARDATA(result);
748 
749  *r++ = '^';
750  *r++ = '(';
751  *r++ = '?';
752  *r++ = ':';
753 
754  while (plen > 0)
755  {
756  char pchar = *p;
757 
758  /*
759  * If both the escape character and the current character from the
760  * pattern are multi-byte, we need to take the slow path.
761  *
762  * But if one of them is single-byte, we can process the pattern one
763  * byte at a time, ignoring multi-byte characters. (This works
764  * because all server-encodings have the property that a valid
765  * multi-byte character representation cannot contain the
766  * representation of a valid single-byte character.)
767  */
768 
769  if (elen > 1)
770  {
771  int mblen = pg_mblen(p);
772 
773  if (mblen > 1)
774  {
775  /* slow, multi-byte path */
776  if (afterescape)
777  {
778  *r++ = '\\';
779  memcpy(r, p, mblen);
780  r += mblen;
781  afterescape = false;
782  }
783  else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
784  {
785  /* SQL escape character; do not send to output */
786  afterescape = true;
787  }
788  else
789  {
790  /*
791  * We know it's a multi-byte character, so we don't need
792  * to do all the comparisons to single-byte characters
793  * that we do below.
794  */
795  memcpy(r, p, mblen);
796  r += mblen;
797  }
798 
799  p += mblen;
800  plen -= mblen;
801 
802  continue;
803  }
804  }
805 
806  /* fast path */
807  if (afterescape)
808  {
809  if (pchar == '"' && !incharclass) /* escape-double-quote? */
810  {
811  /* emit appropriate part separator, per notes above */
812  if (nquotes == 0)
813  {
814  *r++ = ')';
815  *r++ = '{';
816  *r++ = '1';
817  *r++ = ',';
818  *r++ = '1';
819  *r++ = '}';
820  *r++ = '?';
821  *r++ = '(';
822  }
823  else if (nquotes == 1)
824  {
825  *r++ = ')';
826  *r++ = '{';
827  *r++ = '1';
828  *r++ = ',';
829  *r++ = '1';
830  *r++ = '}';
831  *r++ = '(';
832  *r++ = '?';
833  *r++ = ':';
834  }
835  else
836  ereport(ERROR,
837  (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
838  errmsg("SQL regular expression may not contain more than two escape-double-quote separators")));
839  nquotes++;
840  }
841  else
842  {
843  /*
844  * We allow any character at all to be escaped; notably, this
845  * allows access to POSIX character-class escapes such as
846  * "\d". The SQL spec is considerably more restrictive.
847  */
848  *r++ = '\\';
849  *r++ = pchar;
850  }
851  afterescape = false;
852  }
853  else if (e && pchar == *e)
854  {
855  /* SQL escape character; do not send to output */
856  afterescape = true;
857  }
858  else if (incharclass)
859  {
860  if (pchar == '\\')
861  *r++ = '\\';
862  *r++ = pchar;
863  if (pchar == ']')
864  incharclass = false;
865  }
866  else if (pchar == '[')
867  {
868  *r++ = pchar;
869  incharclass = true;
870  }
871  else if (pchar == '%')
872  {
873  *r++ = '.';
874  *r++ = '*';
875  }
876  else if (pchar == '_')
877  *r++ = '.';
878  else if (pchar == '(')
879  {
880  /* convert to non-capturing parenthesis */
881  *r++ = '(';
882  *r++ = '?';
883  *r++ = ':';
884  }
885  else if (pchar == '\\' || pchar == '.' ||
886  pchar == '^' || pchar == '$')
887  {
888  *r++ = '\\';
889  *r++ = pchar;
890  }
891  else
892  *r++ = pchar;
893  p++, plen--;
894  }
895 
896  *r++ = ')';
897  *r++ = '$';
898 
899  SET_VARSIZE(result, r - ((char *) result));
900 
901  PG_RETURN_TEXT_P(result);
902 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:786
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
int pg_mblen(const char *mbstr)
Definition: mbutils.c:752
e
Definition: preproc-init.c:82
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ texticregexeq()

Datum texticregexeq ( PG_FUNCTION_ARGS  )

Definition at line 531 of file regexp.c.

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

532 {
533  text *s = PG_GETARG_TEXT_PP(0);
534  text *p = PG_GETARG_TEXT_PP(1);
535 
537  VARDATA_ANY(s),
541  0, NULL));
542 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:549

◆ texticregexne()

Datum texticregexne ( PG_FUNCTION_ARGS  )

Definition at line 545 of file regexp.c.

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

546 {
547  text *s = PG_GETARG_TEXT_PP(0);
548  text *p = PG_GETARG_TEXT_PP(1);
549 
551  VARDATA_ANY(s),
555  0, NULL));
556 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define REG_ICASE
Definition: regex.h:106
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:549

◆ textregexeq()

Datum textregexeq ( PG_FUNCTION_ARGS  )

Definition at line 468 of file regexp.c.

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

469 {
470  text *s = PG_GETARG_TEXT_PP(0);
471  text *p = PG_GETARG_TEXT_PP(1);
472 
474  VARDATA_ANY(s),
476  REG_ADVANCED,
478  0, NULL));
479 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:549

◆ textregexne()

Datum textregexne ( PG_FUNCTION_ARGS  )

Definition at line 482 of file regexp.c.

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

483 {
484  text *s = PG_GETARG_TEXT_PP(0);
485  text *p = PG_GETARG_TEXT_PP(1);
486 
488  VARDATA_ANY(s),
490  REG_ADVANCED,
492  0, NULL));
493 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
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:343
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:549

◆ textregexreplace()

Datum textregexreplace ( PG_FUNCTION_ARGS  )

Definition at line 640 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().

641 {
642  text *s = PG_GETARG_TEXT_PP(0);
643  text *p = PG_GETARG_TEXT_PP(1);
644  text *r = PG_GETARG_TEXT_PP(2);
645  text *opt = PG_GETARG_TEXT_PP(3);
646  regex_t *re;
647  pg_re_flags flags;
648 
649  parse_re_flags(&flags, opt);
650 
651  re = RE_compile_and_cache(p, flags.cflags, PG_GET_COLLATION());
652 
653  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, flags.glob));
654 }
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, bool glob)
Definition: varlena.c:4421
int cflags
Definition: regexp.c:48
bool glob
Definition: regexp.c:49
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:137
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:366
Definition: c.h:549
Definition: regex.h:55

◆ textregexreplace_noopt()

Datum textregexreplace_noopt ( PG_FUNCTION_ARGS  )

Definition at line 623 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().

624 {
625  text *s = PG_GETARG_TEXT_PP(0);
626  text *p = PG_GETARG_TEXT_PP(1);
627  text *r = PG_GETARG_TEXT_PP(2);
628  regex_t *re;
629 
631 
632  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, false));
633 }
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, bool glob)
Definition: varlena.c:4421
#define PG_GET_COLLATION()
Definition: fmgr.h:193
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define REG_ADVANCED
Definition: regex.h:103
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:137
Definition: c.h:549
Definition: regex.h:55

◆ textregexsubstr()

Datum textregexsubstr ( PG_FUNCTION_ARGS  )

Definition at line 564 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.

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

Variable Documentation

◆ num_res

int num_res = 0
static

Definition at line 109 of file regexp.c.

Referenced by RE_compile_and_cache().

◆ re_array

cached_re_str re_array[MAX_CACHED_RES]
static

Definition at line 110 of file regexp.c.