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 *re_flags, int start_search, 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 textregexreplace_extended (PG_FUNCTION_ARGS)
 
Datum textregexreplace_extended_no_n (PG_FUNCTION_ARGS)
 
Datum textregexreplace_extended_no_flags (PG_FUNCTION_ARGS)
 
static textsimilar_escape_internal (text *pat_text, text *esc_text)
 
Datum similar_to_escape_2 (PG_FUNCTION_ARGS)
 
Datum similar_to_escape_1 (PG_FUNCTION_ARGS)
 
Datum similar_escape (PG_FUNCTION_ARGS)
 
Datum regexp_count (PG_FUNCTION_ARGS)
 
Datum regexp_count_no_start (PG_FUNCTION_ARGS)
 
Datum regexp_count_no_flags (PG_FUNCTION_ARGS)
 
Datum regexp_instr (PG_FUNCTION_ARGS)
 
Datum regexp_instr_no_start (PG_FUNCTION_ARGS)
 
Datum regexp_instr_no_n (PG_FUNCTION_ARGS)
 
Datum regexp_instr_no_endoption (PG_FUNCTION_ARGS)
 
Datum regexp_instr_no_flags (PG_FUNCTION_ARGS)
 
Datum regexp_instr_no_subexpr (PG_FUNCTION_ARGS)
 
Datum regexp_like (PG_FUNCTION_ARGS)
 
Datum regexp_like_no_flags (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)
 
Datum regexp_substr (PG_FUNCTION_ARGS)
 
Datum regexp_substr_no_start (PG_FUNCTION_ARGS)
 
Datum regexp_substr_no_n (PG_FUNCTION_ARGS)
 
Datum regexp_substr_no_flags (PG_FUNCTION_ARGS)
 
Datum regexp_substr_no_subexpr (PG_FUNCTION_ARGS)
 
char * regexp_fixed_prefix (text *text_re, bool case_insensitive, Oid collation, bool *exact)
 

Variables

static MemoryContext RegexpCacheMemoryContext
 
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.

◆ PG_GETARG_TEXT_PP_IF_EXISTS

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

Definition at line 41 of file regexp.c.

Typedef Documentation

◆ cached_re_str

typedef struct cached_re_str cached_re_str

◆ pg_re_flags

typedef struct pg_re_flags 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 1621 of file regexp.c.

1622 {
1623  char *buf = matchctx->conv_buf;
1624  Datum *elems = matchctx->elems;
1625  bool *nulls = matchctx->nulls;
1626  int dims[1];
1627  int lbs[1];
1628  int loc;
1629  int i;
1630 
1631  /* Extract matching substrings from the original string */
1632  loc = matchctx->next_match * matchctx->npatterns * 2;
1633  for (i = 0; i < matchctx->npatterns; i++)
1634  {
1635  int so = matchctx->match_locs[loc++];
1636  int eo = matchctx->match_locs[loc++];
1637 
1638  if (so < 0 || eo < 0)
1639  {
1640  elems[i] = (Datum) 0;
1641  nulls[i] = true;
1642  }
1643  else if (buf)
1644  {
1645  int len = pg_wchar2mb_with_len(matchctx->wide_str + so,
1646  buf,
1647  eo - so);
1648 
1649  Assert(len < matchctx->conv_bufsiz);
1651  nulls[i] = false;
1652  }
1653  else
1654  {
1656  PointerGetDatum(matchctx->orig_str),
1657  Int32GetDatum(so + 1),
1658  Int32GetDatum(eo - so));
1659  nulls[i] = false;
1660  }
1661  }
1662 
1663  /* And form an array */
1664  dims[0] = matchctx->npatterns;
1665  lbs[0] = 1;
1666  /* XXX: this hardcodes assumptions about the text type */
1667  return construct_md_array(elems, nulls, 1, dims, lbs,
1668  TEXTOID, -1, false, TYPALIGN_INT);
1669 }
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:3462
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:1009
const void size_t len
static char * buf
Definition: pg_test_fsync.c:67
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
Datum * elems
Definition: regexp.c:63
text * orig_str
Definition: regexp.c:55
char * conv_buf
Definition: regexp.c:66
int * match_locs
Definition: regexp.c:60
pg_wchar * wide_str
Definition: regexp.c:65
bool * nulls
Definition: regexp.c:64
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:850
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:194

References Assert(), buf, construct_md_array(), regexp_matches_ctx::conv_buf, cstring_to_text_with_len(), DirectFunctionCall3, regexp_matches_ctx::elems, i, Int32GetDatum(), len, regexp_matches_ctx::match_locs, regexp_matches_ctx::next_match, regexp_matches_ctx::npatterns, regexp_matches_ctx::nulls, regexp_matches_ctx::orig_str, pg_wchar2mb_with_len(), PointerGetDatum(), text_substr(), and regexp_matches_ctx::wide_str.

Referenced by regexp_match(), and regexp_matches().

◆ build_regexp_split_result()

static Datum build_regexp_split_result ( regexp_matches_ctx splitctx)
static

Definition at line 1792 of file regexp.c.

1793 {
1794  char *buf = splitctx->conv_buf;
1795  int startpos;
1796  int endpos;
1797 
1798  if (splitctx->next_match > 0)
1799  startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
1800  else
1801  startpos = 0;
1802  if (startpos < 0)
1803  elog(ERROR, "invalid match ending position");
1804 
1805  endpos = splitctx->match_locs[splitctx->next_match * 2];
1806  if (endpos < startpos)
1807  elog(ERROR, "invalid match starting position");
1808 
1809  if (buf)
1810  {
1811  int len;
1812 
1813  len = pg_wchar2mb_with_len(splitctx->wide_str + startpos,
1814  buf,
1815  endpos - startpos);
1816  Assert(len < splitctx->conv_bufsiz);
1818  }
1819  else
1820  {
1822  PointerGetDatum(splitctx->orig_str),
1823  Int32GetDatum(startpos + 1),
1825  }
1826 }
#define ERROR
Definition: elog.h:39
static XLogRecPtr endpos
Definition: pg_receivewal.c:56
static XLogRecPtr startpos

References Assert(), buf, regexp_matches_ctx::conv_buf, cstring_to_text_with_len(), DirectFunctionCall3, elog(), endpos, ERROR, Int32GetDatum(), len, regexp_matches_ctx::match_locs, regexp_matches_ctx::next_match, regexp_matches_ctx::orig_str, 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().

◆ nameicregexeq()

Datum nameicregexeq ( PG_FUNCTION_ARGS  )

Definition at line 523 of file regexp.c.

524 {
525  Name n = PG_GETARG_NAME(0);
526  text *p = PG_GETARG_TEXT_PP(1);
527 
529  NameStr(*n),
530  strlen(NameStr(*n)),
533  0, NULL));
534 }
#define NameStr(name)
Definition: c.h:730
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define REG_ICASE
Definition: regex.h:106
#define REG_ADVANCED
Definition: regex.h:103
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:359
Definition: c.h:725
Definition: c.h:671

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

◆ nameicregexne()

Datum nameicregexne ( PG_FUNCTION_ARGS  )

Definition at line 537 of file regexp.c.

538 {
539  Name n = PG_GETARG_NAME(0);
540  text *p = PG_GETARG_TEXT_PP(1);
541 
543  NameStr(*n),
544  strlen(NameStr(*n)),
547  0, NULL));
548 }

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

◆ nameregexeq()

Datum nameregexeq ( PG_FUNCTION_ARGS  )

Definition at line 460 of file regexp.c.

461 {
462  Name n = PG_GETARG_NAME(0);
463  text *p = PG_GETARG_TEXT_PP(1);
464 
466  NameStr(*n),
467  strlen(NameStr(*n)),
468  REG_ADVANCED,
470  0, NULL));
471 }

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

◆ nameregexne()

Datum nameregexne ( PG_FUNCTION_ARGS  )

Definition at line 474 of file regexp.c.

475 {
476  Name n = PG_GETARG_NAME(0);
477  text *p = PG_GETARG_TEXT_PP(1);
478 
480  NameStr(*n),
481  strlen(NameStr(*n)),
482  REG_ADVANCED,
484  0, NULL));
485 }

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

◆ parse_re_flags()

static void parse_re_flags ( pg_re_flags flags,
text opts 
)
static

Definition at line 386 of file regexp.c.

387 {
388  /* regex flavor is always folded into the compile flags */
389  flags->cflags = REG_ADVANCED;
390  flags->glob = false;
391 
392  if (opts)
393  {
394  char *opt_p = VARDATA_ANY(opts);
395  int opt_len = VARSIZE_ANY_EXHDR(opts);
396  int i;
397 
398  for (i = 0; i < opt_len; i++)
399  {
400  switch (opt_p[i])
401  {
402  case 'g':
403  flags->glob = true;
404  break;
405  case 'b': /* BREs (but why???) */
406  flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
407  break;
408  case 'c': /* case sensitive */
409  flags->cflags &= ~REG_ICASE;
410  break;
411  case 'e': /* plain EREs */
412  flags->cflags |= REG_EXTENDED;
413  flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
414  break;
415  case 'i': /* case insensitive */
416  flags->cflags |= REG_ICASE;
417  break;
418  case 'm': /* Perloid synonym for n */
419  case 'n': /* \n affects ^ $ . [^ */
420  flags->cflags |= REG_NEWLINE;
421  break;
422  case 'p': /* ~Perl, \n affects . [^ */
423  flags->cflags |= REG_NLSTOP;
424  flags->cflags &= ~REG_NLANCH;
425  break;
426  case 'q': /* literal string */
427  flags->cflags |= REG_QUOTE;
428  flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
429  break;
430  case 's': /* single line, \n ordinary */
431  flags->cflags &= ~REG_NEWLINE;
432  break;
433  case 't': /* tight syntax */
434  flags->cflags &= ~REG_EXPANDED;
435  break;
436  case 'w': /* weird, \n affects ^ $ only */
437  flags->cflags &= ~REG_NLSTOP;
438  flags->cflags |= REG_NLANCH;
439  break;
440  case 'x': /* expanded syntax */
441  flags->cflags |= REG_EXPANDED;
442  break;
443  default:
444  ereport(ERROR,
445  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
446  errmsg("invalid regular expression option: \"%.*s\"",
447  pg_mblen(opt_p + i), opt_p + i)));
448  break;
449  }
450  }
451  }
452 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1024
static AmcheckOptions opts
Definition: pg_amcheck.c:110
#define REG_EXPANDED
Definition: regex.h:108
#define REG_NLANCH
Definition: regex.h:110
#define REG_EXTENDED
Definition: regex.h:101
#define REG_NLSTOP
Definition: regex.h:109
#define REG_NEWLINE
Definition: regex.h:111
#define REG_QUOTE
Definition: regex.h:104
bool glob
Definition: regexp.c:49
int cflags
Definition: regexp.c:48
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References pg_re_flags::cflags, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, i, opts, pg_mblen(), 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_count(), regexp_instr(), regexp_like(), regexp_match(), regexp_matches(), regexp_split_to_array(), regexp_split_to_table(), regexp_substr(), textregexreplace(), and textregexreplace_extended().

◆ RE_compile_and_cache()

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

Definition at line 142 of file regexp.c.

143 {
144  int text_re_len = VARSIZE_ANY_EXHDR(text_re);
145  char *text_re_val = VARDATA_ANY(text_re);
146  pg_wchar *pattern;
147  int pattern_len;
148  int i;
149  int regcomp_result;
150  cached_re_str re_temp;
151  char errMsg[100];
152  MemoryContext oldcontext;
153 
154  /*
155  * Look for a match among previously compiled REs. Since the data
156  * structure is self-organizing with most-used entries at the front, our
157  * search strategy can just be to scan from the front.
158  */
159  for (i = 0; i < num_res; i++)
160  {
161  if (re_array[i].cre_pat_len == text_re_len &&
162  re_array[i].cre_flags == cflags &&
163  re_array[i].cre_collation == collation &&
164  memcmp(re_array[i].cre_pat, text_re_val, text_re_len) == 0)
165  {
166  /*
167  * Found a match; move it to front if not there already.
168  */
169  if (i > 0)
170  {
171  re_temp = re_array[i];
172  memmove(&re_array[1], &re_array[0], i * sizeof(cached_re_str));
173  re_array[0] = re_temp;
174  }
175 
176  return &re_array[0].cre_re;
177  }
178  }
179 
180  /* Set up the cache memory on first go through. */
181  if (unlikely(RegexpCacheMemoryContext == NULL))
184  "RegexpCacheMemoryContext",
186 
187  /*
188  * Couldn't find it, so try to compile the new RE. To avoid leaking
189  * resources on failure, we build into the re_temp local.
190  */
191 
192  /* Convert pattern string to wide characters */
193  pattern = (pg_wchar *) palloc((text_re_len + 1) * sizeof(pg_wchar));
194  pattern_len = pg_mb2wchar_with_len(text_re_val,
195  pattern,
196  text_re_len);
197 
198  /*
199  * Make a memory context for this compiled regexp. This is initially a
200  * child of the current memory context, so it will be cleaned up
201  * automatically if compilation is interrupted and throws an ERROR. We'll
202  * re-parent it under the longer lived cache context if we make it to the
203  * bottom of this function.
204  */
206  "RegexpMemoryContext",
208  oldcontext = MemoryContextSwitchTo(re_temp.cre_context);
209 
210  regcomp_result = pg_regcomp(&re_temp.cre_re,
211  pattern,
212  pattern_len,
213  cflags,
214  collation);
215 
216  pfree(pattern);
217 
218  if (regcomp_result != REG_OKAY)
219  {
220  /* re didn't compile (no need for pg_regfree, if so) */
221  pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
222  ereport(ERROR,
223  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
224  errmsg("invalid regular expression: %s", errMsg)));
225  }
226 
227  /* Copy the pattern into the per-regexp memory context. */
228  re_temp.cre_pat = palloc(text_re_len + 1);
229  memcpy(re_temp.cre_pat, text_re_val, text_re_len);
230 
231  /*
232  * NUL-terminate it only for the benefit of the identifier used for the
233  * memory context, visible in the pg_backend_memory_contexts view.
234  */
235  re_temp.cre_pat[text_re_len] = 0;
237 
238  re_temp.cre_pat_len = text_re_len;
239  re_temp.cre_flags = cflags;
240  re_temp.cre_collation = collation;
241 
242  /*
243  * Okay, we have a valid new item in re_temp; insert it into the storage
244  * array. Discard last entry if needed.
245  */
246  if (num_res >= MAX_CACHED_RES)
247  {
248  --num_res;
250  /* Delete the memory context holding the regexp and pattern. */
251  MemoryContextDelete(re_array[num_res].cre_context);
252  }
253 
254  /* Re-parent the memory context to our long-lived cache context. */
256 
257  if (num_res > 0)
258  memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
259 
260  re_array[0] = re_temp;
261  num_res++;
262 
263  MemoryContextSwitchTo(oldcontext);
264 
265  return &re_array[0].cre_re;
266 }
#define unlikely(x)
Definition: c.h:295
unsigned int pg_wchar
Definition: mbprint.c:31
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:987
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:546
void pfree(void *pointer)
Definition: mcxt.c:1456
MemoryContext TopMemoryContext
Definition: mcxt.c:141
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
void * palloc(Size size)
Definition: mcxt.c:1226
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:521
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
int pg_regcomp(regex_t *re, const chr *string, size_t len, int flags, Oid collation)
Definition: regcomp.c:370
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define REG_OKAY
Definition: regex.h:137
static MemoryContext RegexpCacheMemoryContext
Definition: regexp.c:100
#define MAX_CACHED_RES
Definition: regexp.c:96
static int num_res
Definition: regexp.c:113
static cached_re_str re_array[MAX_CACHED_RES]
Definition: regexp.c:114
char * cre_pat
Definition: regexp.c:106
int cre_pat_len
Definition: regexp.c:107
regex_t cre_re
Definition: regexp.c:110
Oid cre_collation
Definition: regexp.c:109
int cre_flags
Definition: regexp.c:108
MemoryContext cre_context
Definition: regexp.c:105

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert(), cached_re_str::cre_collation, cached_re_str::cre_context, cached_re_str::cre_flags, cached_re_str::cre_pat, cached_re_str::cre_pat_len, cached_re_str::cre_re, CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, i, MAX_CACHED_RES, MemoryContextDelete(), MemoryContextSetIdentifier(), MemoryContextSetParent(), MemoryContextSwitchTo(), num_res, palloc(), pfree(), pg_mb2wchar_with_len(), pg_regcomp(), pg_regerror(), re_array, REG_OKAY, RegexpCacheMemoryContext, TopMemoryContext, unlikely, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by RE_compile_and_execute(), regexp_fixed_prefix(), replace_text_regexp(), setup_regexp_matches(), and textregexsubstr().

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

362 {
363  regex_t *re;
364 
365  /* Use REG_NOSUB if caller does not want sub-match details */
366  if (nmatch < 2)
367  cflags |= REG_NOSUB;
368 
369  /* Compile RE */
370  re = RE_compile_and_cache(text_re, cflags, collation);
371 
372  return RE_execute(re, dat, dat_len, nmatch, pmatch);
373 }
#define REG_NOSUB
Definition: regex.h:107
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:142
static bool RE_execute(regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:325
Definition: regex.h:56

References RE_compile_and_cache(), RE_execute(), and REG_NOSUB.

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

◆ RE_execute()

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

Definition at line 325 of file regexp.c.

327 {
328  pg_wchar *data;
329  int data_len;
330  bool match;
331 
332  /* Convert data string to wide characters */
333  data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
334  data_len = pg_mb2wchar_with_len(dat, data, dat_len);
335 
336  /* Perform RE match and return result */
337  match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
338 
339  pfree(data);
340  return match;
341 }
const void * data
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:283

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

Referenced by RE_compile_and_execute(), and textregexsubstr().

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

285 {
286  int regexec_result;
287  char errMsg[100];
288 
289  /* Perform RE match and return result */
290  regexec_result = pg_regexec(re,
291  data,
292  data_len,
293  start_search,
294  NULL, /* no details */
295  nmatch,
296  pmatch,
297  0);
298 
299  if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
300  {
301  /* re failed??? */
302  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
303  ereport(ERROR,
304  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
305  errmsg("regular expression failed: %s", errMsg)));
306  }
307 
308  return (regexec_result == REG_OKAY);
309 }
#define REG_NOMATCH
Definition: regex.h:138
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:185

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

Referenced by RE_execute(), and setup_regexp_matches().

◆ regexp_count()

Datum regexp_count ( PG_FUNCTION_ARGS  )

Definition at line 1067 of file regexp.c.

1068 {
1069  text *str = PG_GETARG_TEXT_PP(0);
1070  text *pattern = PG_GETARG_TEXT_PP(1);
1071  int start = 1;
1072  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(3);
1073  pg_re_flags re_flags;
1074  regexp_matches_ctx *matchctx;
1075 
1076  /* Collect optional parameters */
1077  if (PG_NARGS() > 2)
1078  {
1079  start = PG_GETARG_INT32(2);
1080  if (start <= 0)
1081  ereport(ERROR,
1082  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1083  errmsg("invalid value for parameter \"%s\": %d",
1084  "start", start)));
1085  }
1086 
1087  /* Determine options */
1088  parse_re_flags(&re_flags, flags);
1089  /* User mustn't specify 'g' */
1090  if (re_flags.glob)
1091  ereport(ERROR,
1092  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1093  /* translator: %s is a SQL function name */
1094  errmsg("%s does not support the \"global\" option",
1095  "regexp_count()")));
1096  /* But we find all the matches anyway */
1097  re_flags.glob = true;
1098 
1099  /* Do the matching */
1100  matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
1101  PG_GET_COLLATION(),
1102  false, /* can ignore subexprs */
1103  false, false);
1104 
1105  PG_RETURN_INT32(matchctx->nmatches);
1106 }
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *re_flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1417
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:386

References ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, regexp_matches_ctx::nmatches, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_NARGS, PG_RETURN_INT32, setup_regexp_matches(), and generate_unaccent_rules::str.

Referenced by regexp_count_no_flags(), and regexp_count_no_start().

◆ regexp_count_no_flags()

Datum regexp_count_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1117 of file regexp.c.

1118 {
1119  return regexp_count(fcinfo);
1120 }
Datum regexp_count(PG_FUNCTION_ARGS)
Definition: regexp.c:1067

References regexp_count().

◆ regexp_count_no_start()

Datum regexp_count_no_start ( PG_FUNCTION_ARGS  )

Definition at line 1110 of file regexp.c.

1111 {
1112  return regexp_count(fcinfo);
1113 }

References regexp_count().

◆ regexp_fixed_prefix()

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

Definition at line 1954 of file regexp.c.

1956 {
1957  char *result;
1958  regex_t *re;
1959  int cflags;
1960  int re_result;
1961  pg_wchar *str;
1962  size_t slen;
1963  size_t maxlen;
1964  char errMsg[100];
1965 
1966  *exact = false; /* default result */
1967 
1968  /* Compile RE */
1969  cflags = REG_ADVANCED;
1970  if (case_insensitive)
1971  cflags |= REG_ICASE;
1972 
1973  re = RE_compile_and_cache(text_re, cflags | REG_NOSUB, collation);
1974 
1975  /* Examine it to see if there's a fixed prefix */
1976  re_result = pg_regprefix(re, &str, &slen);
1977 
1978  switch (re_result)
1979  {
1980  case REG_NOMATCH:
1981  return NULL;
1982 
1983  case REG_PREFIX:
1984  /* continue with wchar conversion */
1985  break;
1986 
1987  case REG_EXACT:
1988  *exact = true;
1989  /* continue with wchar conversion */
1990  break;
1991 
1992  default:
1993  /* re failed??? */
1994  pg_regerror(re_result, re, errMsg, sizeof(errMsg));
1995  ereport(ERROR,
1996  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
1997  errmsg("regular expression failed: %s", errMsg)));
1998  break;
1999  }
2000 
2001  /* Convert pg_wchar result back to database encoding */
2002  maxlen = pg_database_encoding_max_length() * slen + 1;
2003  result = (char *) palloc(maxlen);
2004  slen = pg_wchar2mb_with_len(str, result, slen);
2005  Assert(slen < maxlen);
2006 
2007  pfree(str);
2008 
2009  return result;
2010 }
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1553
#define REG_EXACT
Definition: regex.h:162
#define REG_PREFIX
Definition: regex.h:161
int pg_regprefix(regex_t *re, chr **string, size_t *slength)
Definition: regprefix.c:46

References Assert(), ereport, errcode(), errmsg(), ERROR, palloc(), pfree(), 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_NOSUB, REG_PREFIX, and generate_unaccent_rules::str.

Referenced by regex_fixed_prefix().

◆ regexp_instr()

Datum regexp_instr ( PG_FUNCTION_ARGS  )

Definition at line 1127 of file regexp.c.

1128 {
1129  text *str = PG_GETARG_TEXT_PP(0);
1130  text *pattern = PG_GETARG_TEXT_PP(1);
1131  int start = 1;
1132  int n = 1;
1133  int endoption = 0;
1134  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
1135  int subexpr = 0;
1136  int pos;
1137  pg_re_flags re_flags;
1138  regexp_matches_ctx *matchctx;
1139 
1140  /* Collect optional parameters */
1141  if (PG_NARGS() > 2)
1142  {
1143  start = PG_GETARG_INT32(2);
1144  if (start <= 0)
1145  ereport(ERROR,
1146  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1147  errmsg("invalid value for parameter \"%s\": %d",
1148  "start", start)));
1149  }
1150  if (PG_NARGS() > 3)
1151  {
1152  n = PG_GETARG_INT32(3);
1153  if (n <= 0)
1154  ereport(ERROR,
1155  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1156  errmsg("invalid value for parameter \"%s\": %d",
1157  "n", n)));
1158  }
1159  if (PG_NARGS() > 4)
1160  {
1161  endoption = PG_GETARG_INT32(4);
1162  if (endoption != 0 && endoption != 1)
1163  ereport(ERROR,
1164  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1165  errmsg("invalid value for parameter \"%s\": %d",
1166  "endoption", endoption)));
1167  }
1168  if (PG_NARGS() > 6)
1169  {
1170  subexpr = PG_GETARG_INT32(6);
1171  if (subexpr < 0)
1172  ereport(ERROR,
1173  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1174  errmsg("invalid value for parameter \"%s\": %d",
1175  "subexpr", subexpr)));
1176  }
1177 
1178  /* Determine options */
1179  parse_re_flags(&re_flags, flags);
1180  /* User mustn't specify 'g' */
1181  if (re_flags.glob)
1182  ereport(ERROR,
1183  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1184  /* translator: %s is a SQL function name */
1185  errmsg("%s does not support the \"global\" option",
1186  "regexp_instr()")));
1187  /* But we find all the matches anyway */
1188  re_flags.glob = true;
1189 
1190  /* Do the matching */
1191  matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
1192  PG_GET_COLLATION(),
1193  (subexpr > 0), /* need submatches? */
1194  false, false);
1195 
1196  /* When n exceeds matches return 0 (includes case of no matches) */
1197  if (n > matchctx->nmatches)
1198  PG_RETURN_INT32(0);
1199 
1200  /* When subexpr exceeds number of subexpressions return 0 */
1201  if (subexpr > matchctx->npatterns)
1202  PG_RETURN_INT32(0);
1203 
1204  /* Select the appropriate match position to return */
1205  pos = (n - 1) * matchctx->npatterns;
1206  if (subexpr > 0)
1207  pos += subexpr - 1;
1208  pos *= 2;
1209  if (endoption == 1)
1210  pos += 1;
1211 
1212  if (matchctx->match_locs[pos] >= 0)
1213  PG_RETURN_INT32(matchctx->match_locs[pos] + 1);
1214  else
1215  PG_RETURN_INT32(0); /* position not identifiable */
1216 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

References ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, if(), regexp_matches_ctx::match_locs, regexp_matches_ctx::nmatches, regexp_matches_ctx::npatterns, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_NARGS, PG_RETURN_INT32, setup_regexp_matches(), and generate_unaccent_rules::str.

Referenced by regexp_instr_no_endoption(), regexp_instr_no_flags(), regexp_instr_no_n(), regexp_instr_no_start(), and regexp_instr_no_subexpr().

◆ regexp_instr_no_endoption()

Datum regexp_instr_no_endoption ( PG_FUNCTION_ARGS  )

Definition at line 1234 of file regexp.c.

1235 {
1236  return regexp_instr(fcinfo);
1237 }
Datum regexp_instr(PG_FUNCTION_ARGS)
Definition: regexp.c:1127

References regexp_instr().

◆ regexp_instr_no_flags()

Datum regexp_instr_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1241 of file regexp.c.

1242 {
1243  return regexp_instr(fcinfo);
1244 }

References regexp_instr().

◆ regexp_instr_no_n()

Datum regexp_instr_no_n ( PG_FUNCTION_ARGS  )

Definition at line 1227 of file regexp.c.

1228 {
1229  return regexp_instr(fcinfo);
1230 }

References regexp_instr().

◆ regexp_instr_no_start()

Datum regexp_instr_no_start ( PG_FUNCTION_ARGS  )

Definition at line 1220 of file regexp.c.

1221 {
1222  return regexp_instr(fcinfo);
1223 }

References regexp_instr().

◆ regexp_instr_no_subexpr()

Datum regexp_instr_no_subexpr ( PG_FUNCTION_ARGS  )

Definition at line 1248 of file regexp.c.

1249 {
1250  return regexp_instr(fcinfo);
1251 }

References regexp_instr().

◆ regexp_like()

Datum regexp_like ( PG_FUNCTION_ARGS  )

Definition at line 1258 of file regexp.c.

1259 {
1260  text *str = PG_GETARG_TEXT_PP(0);
1261  text *pattern = PG_GETARG_TEXT_PP(1);
1262  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1263  pg_re_flags re_flags;
1264 
1265  /* Determine options */
1266  parse_re_flags(&re_flags, flags);
1267  /* User mustn't specify 'g' */
1268  if (re_flags.glob)
1269  ereport(ERROR,
1270  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1271  /* translator: %s is a SQL function name */
1272  errmsg("%s does not support the \"global\" option",
1273  "regexp_like()")));
1274 
1275  /* Otherwise it's like textregexeq/texticregexeq */
1277  VARDATA_ANY(str),
1279  re_flags.cflags,
1280  PG_GET_COLLATION(),
1281  0, NULL));
1282 }

References pg_re_flags::cflags, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_RETURN_BOOL, RE_compile_and_execute(), generate_unaccent_rules::str, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by regexp_like_no_flags().

◆ regexp_like_no_flags()

Datum regexp_like_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1286 of file regexp.c.

1287 {
1288  return regexp_like(fcinfo);
1289 }
Datum regexp_like(PG_FUNCTION_ARGS)
Definition: regexp.c:1258

References regexp_like().

◆ regexp_match()

Datum regexp_match ( PG_FUNCTION_ARGS  )

Definition at line 1296 of file regexp.c.

1297 {
1298  text *orig_str = PG_GETARG_TEXT_PP(0);
1299  text *pattern = PG_GETARG_TEXT_PP(1);
1300  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1301  pg_re_flags re_flags;
1302  regexp_matches_ctx *matchctx;
1303 
1304  /* Determine options */
1305  parse_re_flags(&re_flags, flags);
1306  /* User mustn't specify 'g' */
1307  if (re_flags.glob)
1308  ereport(ERROR,
1309  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1310  /* translator: %s is a SQL function name */
1311  errmsg("%s does not support the \"global\" option",
1312  "regexp_match()"),
1313  errhint("Use the regexp_matches function instead.")));
1314 
1315  matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, 0,
1316  PG_GET_COLLATION(), true, false, false);
1317 
1318  if (matchctx->nmatches == 0)
1319  PG_RETURN_NULL();
1320 
1321  Assert(matchctx->nmatches == 1);
1322 
1323  /* Create workspace that build_regexp_match_result needs */
1324  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
1325  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
1326 
1328 }
int errhint(const char *fmt,...)
Definition: elog.c:1316
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1621

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().

◆ regexp_match_no_flags()

Datum regexp_match_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1332 of file regexp.c.

1333 {
1334  return regexp_match(fcinfo);
1335 }
Datum regexp_match(PG_FUNCTION_ARGS)
Definition: regexp.c:1296

References regexp_match().

◆ regexp_matches()

Datum regexp_matches ( PG_FUNCTION_ARGS  )

Definition at line 1342 of file regexp.c.

1343 {
1344  FuncCallContext *funcctx;
1345  regexp_matches_ctx *matchctx;
1346 
1347  if (SRF_IS_FIRSTCALL())
1348  {
1349  text *pattern = PG_GETARG_TEXT_PP(1);
1350  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1351  pg_re_flags re_flags;
1352  MemoryContext oldcontext;
1353 
1354  funcctx = SRF_FIRSTCALL_INIT();
1355  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1356 
1357  /* Determine options */
1358  parse_re_flags(&re_flags, flags);
1359 
1360  /* be sure to copy the input string into the multi-call ctx */
1361  matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1362  &re_flags, 0,
1363  PG_GET_COLLATION(),
1364  true, false, false);
1365 
1366  /* Pre-create workspace that build_regexp_match_result needs */
1367  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
1368  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
1369 
1370  MemoryContextSwitchTo(oldcontext);
1371  funcctx->user_fctx = (void *) matchctx;
1372  }
1373 
1374  funcctx = SRF_PERCALL_SETUP();
1375  matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
1376 
1377  if (matchctx->next_match < matchctx->nmatches)
1378  {
1379  ArrayType *result_ary;
1380 
1381  result_ary = build_regexp_match_result(matchctx);
1382  matchctx->next_match++;
1383  SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
1384  }
1385 
1386  SRF_RETURN_DONE(funcctx);
1387 }
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:315
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101

References build_regexp_match_result(), regexp_matches_ctx::elems, if(), 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().

◆ regexp_matches_no_flags()

Datum regexp_matches_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1391 of file regexp.c.

1392 {
1393  return regexp_matches(fcinfo);
1394 }
Datum regexp_matches(PG_FUNCTION_ARGS)
Definition: regexp.c:1342

References regexp_matches().

◆ regexp_split_to_array()

Datum regexp_split_to_array ( PG_FUNCTION_ARGS  )

Definition at line 1741 of file regexp.c.

1742 {
1743  ArrayBuildState *astate = NULL;
1744  pg_re_flags re_flags;
1745  regexp_matches_ctx *splitctx;
1746 
1747  /* Determine options */
1749  /* User mustn't specify 'g' */
1750  if (re_flags.glob)
1751  ereport(ERROR,
1752  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1753  /* translator: %s is a SQL function name */
1754  errmsg("%s does not support the \"global\" option",
1755  "regexp_split_to_array()")));
1756  /* But we find all the matches anyway */
1757  re_flags.glob = true;
1758 
1760  PG_GETARG_TEXT_PP(1),
1761  &re_flags, 0,
1762  PG_GET_COLLATION(),
1763  false, true, true);
1764 
1765  while (splitctx->next_match <= splitctx->nmatches)
1766  {
1767  astate = accumArrayResult(astate,
1768  build_regexp_split_result(splitctx),
1769  false,
1770  TEXTOID,
1772  splitctx->next_match++;
1773  }
1774 
1776 }
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5318
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5382
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1792

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_DATUM, and setup_regexp_matches().

Referenced by regexp_split_to_array_no_flags().

◆ regexp_split_to_array_no_flags()

Datum regexp_split_to_array_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1780 of file regexp.c.

1781 {
1782  return regexp_split_to_array(fcinfo);
1783 }
Datum regexp_split_to_array(PG_FUNCTION_ARGS)
Definition: regexp.c:1741

References regexp_split_to_array().

◆ regexp_split_to_table()

Datum regexp_split_to_table ( PG_FUNCTION_ARGS  )

Definition at line 1677 of file regexp.c.

1678 {
1679  FuncCallContext *funcctx;
1680  regexp_matches_ctx *splitctx;
1681 
1682  if (SRF_IS_FIRSTCALL())
1683  {
1684  text *pattern = PG_GETARG_TEXT_PP(1);
1685  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1686  pg_re_flags re_flags;
1687  MemoryContext oldcontext;
1688 
1689  funcctx = SRF_FIRSTCALL_INIT();
1690  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1691 
1692  /* Determine options */
1693  parse_re_flags(&re_flags, flags);
1694  /* User mustn't specify 'g' */
1695  if (re_flags.glob)
1696  ereport(ERROR,
1697  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1698  /* translator: %s is a SQL function name */
1699  errmsg("%s does not support the \"global\" option",
1700  "regexp_split_to_table()")));
1701  /* But we find all the matches anyway */
1702  re_flags.glob = true;
1703 
1704  /* be sure to copy the input string into the multi-call ctx */
1705  splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1706  &re_flags, 0,
1707  PG_GET_COLLATION(),
1708  false, true, true);
1709 
1710  MemoryContextSwitchTo(oldcontext);
1711  funcctx->user_fctx = (void *) splitctx;
1712  }
1713 
1714  funcctx = SRF_PERCALL_SETUP();
1715  splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
1716 
1717  if (splitctx->next_match <= splitctx->nmatches)
1718  {
1719  Datum result = build_regexp_split_result(splitctx);
1720 
1721  splitctx->next_match++;
1722  SRF_RETURN_NEXT(funcctx, result);
1723  }
1724 
1725  SRF_RETURN_DONE(funcctx);
1726 }

References build_regexp_split_result(), ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, if(), 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().

◆ regexp_split_to_table_no_flags()

Datum regexp_split_to_table_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1730 of file regexp.c.

1731 {
1732  return regexp_split_to_table(fcinfo);
1733 }
Datum regexp_split_to_table(PG_FUNCTION_ARGS)
Definition: regexp.c:1677

References regexp_split_to_table().

◆ regexp_substr()

Datum regexp_substr ( PG_FUNCTION_ARGS  )

Definition at line 1833 of file regexp.c.

1834 {
1835  text *str = PG_GETARG_TEXT_PP(0);
1836  text *pattern = PG_GETARG_TEXT_PP(1);
1837  int start = 1;
1838  int n = 1;
1839  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(4);
1840  int subexpr = 0;
1841  int so,
1842  eo,
1843  pos;
1844  pg_re_flags re_flags;
1845  regexp_matches_ctx *matchctx;
1846 
1847  /* Collect optional parameters */
1848  if (PG_NARGS() > 2)
1849  {
1850  start = PG_GETARG_INT32(2);
1851  if (start <= 0)
1852  ereport(ERROR,
1853  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1854  errmsg("invalid value for parameter \"%s\": %d",
1855  "start", start)));
1856  }
1857  if (PG_NARGS() > 3)
1858  {
1859  n = PG_GETARG_INT32(3);
1860  if (n <= 0)
1861  ereport(ERROR,
1862  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1863  errmsg("invalid value for parameter \"%s\": %d",
1864  "n", n)));
1865  }
1866  if (PG_NARGS() > 5)
1867  {
1868  subexpr = PG_GETARG_INT32(5);
1869  if (subexpr < 0)
1870  ereport(ERROR,
1871  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1872  errmsg("invalid value for parameter \"%s\": %d",
1873  "subexpr", subexpr)));
1874  }
1875 
1876  /* Determine options */
1877  parse_re_flags(&re_flags, flags);
1878  /* User mustn't specify 'g' */
1879  if (re_flags.glob)
1880  ereport(ERROR,
1881  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1882  /* translator: %s is a SQL function name */
1883  errmsg("%s does not support the \"global\" option",
1884  "regexp_substr()")));
1885  /* But we find all the matches anyway */
1886  re_flags.glob = true;
1887 
1888  /* Do the matching */
1889  matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
1890  PG_GET_COLLATION(),
1891  (subexpr > 0), /* need submatches? */
1892  false, false);
1893 
1894  /* When n exceeds matches return NULL (includes case of no matches) */
1895  if (n > matchctx->nmatches)
1896  PG_RETURN_NULL();
1897 
1898  /* When subexpr exceeds number of subexpressions return NULL */
1899  if (subexpr > matchctx->npatterns)
1900  PG_RETURN_NULL();
1901 
1902  /* Select the appropriate match position to return */
1903  pos = (n - 1) * matchctx->npatterns;
1904  if (subexpr > 0)
1905  pos += subexpr - 1;
1906  pos *= 2;
1907  so = matchctx->match_locs[pos];
1908  eo = matchctx->match_locs[pos + 1];
1909 
1910  if (so < 0 || eo < 0)
1911  PG_RETURN_NULL(); /* unidentifiable location */
1912 
1914  PointerGetDatum(matchctx->orig_str),
1915  Int32GetDatum(so + 1),
1916  Int32GetDatum(eo - so)));
1917 }

References DirectFunctionCall3, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, if(), Int32GetDatum(), regexp_matches_ctx::match_locs, regexp_matches_ctx::nmatches, regexp_matches_ctx::npatterns, regexp_matches_ctx::orig_str, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_NARGS, PG_RETURN_DATUM, PG_RETURN_NULL, PointerGetDatum(), setup_regexp_matches(), generate_unaccent_rules::str, and text_substr().

Referenced by regexp_substr_no_flags(), regexp_substr_no_n(), regexp_substr_no_start(), and regexp_substr_no_subexpr().

◆ regexp_substr_no_flags()

Datum regexp_substr_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1935 of file regexp.c.

1936 {
1937  return regexp_substr(fcinfo);
1938 }
Datum regexp_substr(PG_FUNCTION_ARGS)
Definition: regexp.c:1833

References regexp_substr().

◆ regexp_substr_no_n()

Datum regexp_substr_no_n ( PG_FUNCTION_ARGS  )

Definition at line 1928 of file regexp.c.

1929 {
1930  return regexp_substr(fcinfo);
1931 }

References regexp_substr().

◆ regexp_substr_no_start()

Datum regexp_substr_no_start ( PG_FUNCTION_ARGS  )

Definition at line 1921 of file regexp.c.

1922 {
1923  return regexp_substr(fcinfo);
1924 }

References regexp_substr().

◆ regexp_substr_no_subexpr()

Datum regexp_substr_no_subexpr ( PG_FUNCTION_ARGS  )

Definition at line 1942 of file regexp.c.

1943 {
1944  return regexp_substr(fcinfo);
1945 }

References regexp_substr().

◆ setup_regexp_matches()

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

Definition at line 1417 of file regexp.c.

1423 {
1424  regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
1425  int eml = pg_database_encoding_max_length();
1426  int orig_len;
1427  pg_wchar *wide_str;
1428  int wide_len;
1429  int cflags;
1430  regex_t *cpattern;
1431  regmatch_t *pmatch;
1432  int pmatch_len;
1433  int array_len;
1434  int array_idx;
1435  int prev_match_end;
1436  int prev_valid_match_end;
1437  int maxlen = 0; /* largest fetch length in characters */
1438 
1439  /* save original string --- we'll extract result substrings from it */
1440  matchctx->orig_str = orig_str;
1441 
1442  /* convert string to pg_wchar form for matching */
1443  orig_len = VARSIZE_ANY_EXHDR(orig_str);
1444  wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
1445  wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
1446 
1447  /* set up the compiled pattern */
1448  cflags = re_flags->cflags;
1449  if (!use_subpatterns)
1450  cflags |= REG_NOSUB;
1451  cpattern = RE_compile_and_cache(pattern, cflags, collation);
1452 
1453  /* do we want to remember subpatterns? */
1454  if (use_subpatterns && cpattern->re_nsub > 0)
1455  {
1456  matchctx->npatterns = cpattern->re_nsub;
1457  pmatch_len = cpattern->re_nsub + 1;
1458  }
1459  else
1460  {
1461  use_subpatterns = false;
1462  matchctx->npatterns = 1;
1463  pmatch_len = 1;
1464  }
1465 
1466  /* temporary output space for RE package */
1467  pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
1468 
1469  /*
1470  * the real output space (grown dynamically if needed)
1471  *
1472  * use values 2^n-1, not 2^n, so that we hit the limit at 2^28-1 rather
1473  * than at 2^27
1474  */
1475  array_len = re_flags->glob ? 255 : 31;
1476  matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
1477  array_idx = 0;
1478 
1479  /* search for the pattern, perhaps repeatedly */
1480  prev_match_end = 0;
1481  prev_valid_match_end = 0;
1482  while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
1483  pmatch_len, pmatch))
1484  {
1485  /*
1486  * If requested, ignore degenerate matches, which are zero-length
1487  * matches occurring at the start or end of a string or just after a
1488  * previous match.
1489  */
1490  if (!ignore_degenerate ||
1491  (pmatch[0].rm_so < wide_len &&
1492  pmatch[0].rm_eo > prev_match_end))
1493  {
1494  /* enlarge output space if needed */
1495  while (array_idx + matchctx->npatterns * 2 + 1 > array_len)
1496  {
1497  array_len += array_len + 1; /* 2^n-1 => 2^(n+1)-1 */
1498  if (array_len > MaxAllocSize / sizeof(int))
1499  ereport(ERROR,
1500  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1501  errmsg("too many regular expression matches")));
1502  matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
1503  sizeof(int) * array_len);
1504  }
1505 
1506  /* save this match's locations */
1507  if (use_subpatterns)
1508  {
1509  int i;
1510 
1511  for (i = 1; i <= matchctx->npatterns; i++)
1512  {
1513  int so = pmatch[i].rm_so;
1514  int eo = pmatch[i].rm_eo;
1515 
1516  matchctx->match_locs[array_idx++] = so;
1517  matchctx->match_locs[array_idx++] = eo;
1518  if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
1519  maxlen = (eo - so);
1520  }
1521  }
1522  else
1523  {
1524  int so = pmatch[0].rm_so;
1525  int eo = pmatch[0].rm_eo;
1526 
1527  matchctx->match_locs[array_idx++] = so;
1528  matchctx->match_locs[array_idx++] = eo;
1529  if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
1530  maxlen = (eo - so);
1531  }
1532  matchctx->nmatches++;
1533 
1534  /*
1535  * check length of unmatched portion between end of previous valid
1536  * (nondegenerate, or degenerate but not ignored) match and start
1537  * of current one
1538  */
1539  if (fetching_unmatched &&
1540  pmatch[0].rm_so >= 0 &&
1541  (pmatch[0].rm_so - prev_valid_match_end) > maxlen)
1542  maxlen = (pmatch[0].rm_so - prev_valid_match_end);
1543  prev_valid_match_end = pmatch[0].rm_eo;
1544  }
1545  prev_match_end = pmatch[0].rm_eo;
1546 
1547  /* if not glob, stop after one match */
1548  if (!re_flags->glob)
1549  break;
1550 
1551  /*
1552  * Advance search position. Normally we start the next search at the
1553  * end of the previous match; but if the match was of zero length, we
1554  * have to advance by one character, or we'd just find the same match
1555  * again.
1556  */
1557  start_search = prev_match_end;
1558  if (pmatch[0].rm_so == pmatch[0].rm_eo)
1559  start_search++;
1560  if (start_search > wide_len)
1561  break;
1562  }
1563 
1564  /*
1565  * check length of unmatched portion between end of last match and end of
1566  * input string
1567  */
1568  if (fetching_unmatched &&
1569  (wide_len - prev_valid_match_end) > maxlen)
1570  maxlen = (wide_len - prev_valid_match_end);
1571 
1572  /*
1573  * Keep a note of the end position of the string for the benefit of
1574  * splitting code.
1575  */
1576  matchctx->match_locs[array_idx] = wide_len;
1577 
1578  if (eml > 1)
1579  {
1580  int64 maxsiz = eml * (int64) maxlen;
1581  int conv_bufsiz;
1582 
1583  /*
1584  * Make the conversion buffer large enough for any substring of
1585  * interest.
1586  *
1587  * Worst case: assume we need the maximum size (maxlen*eml), but take
1588  * advantage of the fact that the original string length in bytes is
1589  * an upper bound on the byte length of any fetched substring (and we
1590  * know that len+1 is safe to allocate because the varlena header is
1591  * longer than 1 byte).
1592  */
1593  if (maxsiz > orig_len)
1594  conv_bufsiz = orig_len + 1;
1595  else
1596  conv_bufsiz = maxsiz + 1; /* safe since maxsiz < 2^30 */
1597 
1598  matchctx->conv_buf = palloc(conv_bufsiz);
1599  matchctx->conv_bufsiz = conv_bufsiz;
1600  matchctx->wide_str = wide_str;
1601  }
1602  else
1603  {
1604  /* No need to keep the wide string if we're in a single-byte charset. */
1605  pfree(wide_str);
1606  matchctx->wide_str = NULL;
1607  matchctx->conv_buf = NULL;
1608  matchctx->conv_bufsiz = 0;
1609  }
1610 
1611  /* Clean up temp storage */
1612  pfree(pmatch);
1613 
1614  return matchctx;
1615 }
void * palloc0(Size size)
Definition: mcxt.c:1257
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
#define MaxAllocSize
Definition: memutils.h:40
size_t re_nsub
Definition: regex.h:58
regoff_t rm_eo
Definition: regex.h:86
regoff_t rm_so
Definition: regex.h:85

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(), REG_NOSUB, repalloc(), regmatch_t::rm_eo, regmatch_t::rm_so, VARDATA_ANY, VARSIZE_ANY_EXHDR, and regexp_matches_ctx::wide_str.

Referenced by regexp_count(), regexp_instr(), regexp_match(), regexp_matches(), regexp_split_to_array(), regexp_split_to_table(), and regexp_substr().

◆ similar_escape()

Datum similar_escape ( PG_FUNCTION_ARGS  )

Definition at line 1041 of file regexp.c.

1042 {
1043  text *pat_text;
1044  text *esc_text;
1045  text *result;
1046 
1047  /* This function is not strict, so must test explicitly */
1048  if (PG_ARGISNULL(0))
1049  PG_RETURN_NULL();
1050  pat_text = PG_GETARG_TEXT_PP(0);
1051 
1052  if (PG_ARGISNULL(1))
1053  esc_text = NULL; /* use default escape character */
1054  else
1055  esc_text = PG_GETARG_TEXT_PP(1);
1056 
1057  result = similar_escape_internal(pat_text, esc_text);
1058 
1059  PG_RETURN_TEXT_P(result);
1060 }
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static text * similar_escape_internal(text *pat_text, text *esc_text)
Definition: regexp.c:768

References PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and similar_escape_internal().

◆ similar_escape_internal()

static text* similar_escape_internal ( text pat_text,
text esc_text 
)
static

Definition at line 768 of file regexp.c.

769 {
770  text *result;
771  char *p,
772  *e,
773  *r;
774  int plen,
775  elen;
776  bool afterescape = false;
777  bool incharclass = false;
778  int nquotes = 0;
779 
780  p = VARDATA_ANY(pat_text);
781  plen = VARSIZE_ANY_EXHDR(pat_text);
782  if (esc_text == NULL)
783  {
784  /* No ESCAPE clause provided; default to backslash as escape */
785  e = "\\";
786  elen = 1;
787  }
788  else
789  {
790  e = VARDATA_ANY(esc_text);
791  elen = VARSIZE_ANY_EXHDR(esc_text);
792  if (elen == 0)
793  e = NULL; /* no escape character */
794  else if (elen > 1)
795  {
796  int escape_mblen = pg_mbstrlen_with_len(e, elen);
797 
798  if (escape_mblen > 1)
799  ereport(ERROR,
800  (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
801  errmsg("invalid escape string"),
802  errhint("Escape string must be empty or one character.")));
803  }
804  }
805 
806  /*----------
807  * We surround the transformed input string with
808  * ^(?: ... )$
809  * which requires some explanation. We need "^" and "$" to force
810  * the pattern to match the entire input string as per the SQL spec.
811  * The "(?:" and ")" are a non-capturing set of parens; we have to have
812  * parens in case the string contains "|", else the "^" and "$" will
813  * be bound into the first and last alternatives which is not what we
814  * want, and the parens must be non capturing because we don't want them
815  * to count when selecting output for SUBSTRING.
816  *
817  * When the pattern is divided into three parts by escape-double-quotes,
818  * what we emit is
819  * ^(?:part1){1,1}?(part2){1,1}(?:part3)$
820  * which requires even more explanation. The "{1,1}?" on part1 makes it
821  * non-greedy so that it will match the smallest possible amount of text
822  * not the largest, as required by SQL. The plain parens around part2
823  * are capturing parens so that that part is what controls the result of
824  * SUBSTRING. The "{1,1}" forces part2 to be greedy, so that it matches
825  * the largest possible amount of text; hence part3 must match the
826  * smallest amount of text, as required by SQL. We don't need an explicit
827  * greediness marker on part3. Note that this also confines the effects
828  * of any "|" characters to the respective part, which is what we want.
829  *
830  * The SQL spec says that SUBSTRING's pattern must contain exactly two
831  * escape-double-quotes, but we only complain if there's more than two.
832  * With none, we act as though part1 and part3 are empty; with one, we
833  * act as though part3 is empty. Both behaviors fall out of omitting
834  * the relevant part separators in the above expansion. If the result
835  * of this function is used in a plain regexp match (SIMILAR TO), the
836  * escape-double-quotes have no effect on the match behavior.
837  *----------
838  */
839 
840  /*
841  * We need room for the prefix/postfix and part separators, plus as many
842  * as 3 output bytes per input byte; since the input is at most 1GB this
843  * can't overflow size_t.
844  */
845  result = (text *) palloc(VARHDRSZ + 23 + 3 * (size_t) plen);
846  r = VARDATA(result);
847 
848  *r++ = '^';
849  *r++ = '(';
850  *r++ = '?';
851  *r++ = ':';
852 
853  while (plen > 0)
854  {
855  char pchar = *p;
856 
857  /*
858  * If both the escape character and the current character from the
859  * pattern are multi-byte, we need to take the slow path.
860  *
861  * But if one of them is single-byte, we can process the pattern one
862  * byte at a time, ignoring multi-byte characters. (This works
863  * because all server-encodings have the property that a valid
864  * multi-byte character representation cannot contain the
865  * representation of a valid single-byte character.)
866  */
867 
868  if (elen > 1)
869  {
870  int mblen = pg_mblen(p);
871 
872  if (mblen > 1)
873  {
874  /* slow, multi-byte path */
875  if (afterescape)
876  {
877  *r++ = '\\';
878  memcpy(r, p, mblen);
879  r += mblen;
880  afterescape = false;
881  }
882  else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
883  {
884  /* SQL escape character; do not send to output */
885  afterescape = true;
886  }
887  else
888  {
889  /*
890  * We know it's a multi-byte character, so we don't need
891  * to do all the comparisons to single-byte characters
892  * that we do below.
893  */
894  memcpy(r, p, mblen);
895  r += mblen;
896  }
897 
898  p += mblen;
899  plen -= mblen;
900 
901  continue;
902  }
903  }
904 
905  /* fast path */
906  if (afterescape)
907  {
908  if (pchar == '"' && !incharclass) /* escape-double-quote? */
909  {
910  /* emit appropriate part separator, per notes above */
911  if (nquotes == 0)
912  {
913  *r++ = ')';
914  *r++ = '{';
915  *r++ = '1';
916  *r++ = ',';
917  *r++ = '1';
918  *r++ = '}';
919  *r++ = '?';
920  *r++ = '(';
921  }
922  else if (nquotes == 1)
923  {
924  *r++ = ')';
925  *r++ = '{';
926  *r++ = '1';
927  *r++ = ',';
928  *r++ = '1';
929  *r++ = '}';
930  *r++ = '(';
931  *r++ = '?';
932  *r++ = ':';
933  }
934  else
935  ereport(ERROR,
936  (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
937  errmsg("SQL regular expression may not contain more than two escape-double-quote separators")));
938  nquotes++;
939  }
940  else
941  {
942  /*
943  * We allow any character at all to be escaped; notably, this
944  * allows access to POSIX character-class escapes such as
945  * "\d". The SQL spec is considerably more restrictive.
946  */
947  *r++ = '\\';
948  *r++ = pchar;
949  }
950  afterescape = false;
951  }
952  else if (e && pchar == *e)
953  {
954  /* SQL escape character; do not send to output */
955  afterescape = true;
956  }
957  else if (incharclass)
958  {
959  if (pchar == '\\')
960  *r++ = '\\';
961  *r++ = pchar;
962  if (pchar == ']')
963  incharclass = false;
964  }
965  else if (pchar == '[')
966  {
967  *r++ = pchar;
968  incharclass = true;
969  }
970  else if (pchar == '%')
971  {
972  *r++ = '.';
973  *r++ = '*';
974  }
975  else if (pchar == '_')
976  *r++ = '.';
977  else if (pchar == '(')
978  {
979  /* convert to non-capturing parenthesis */
980  *r++ = '(';
981  *r++ = '?';
982  *r++ = ':';
983  }
984  else if (pchar == '\\' || pchar == '.' ||
985  pchar == '^' || pchar == '$')
986  {
987  *r++ = '\\';
988  *r++ = pchar;
989  }
990  else
991  *r++ = pchar;
992  p++, plen--;
993  }
994 
995  *r++ = ')';
996  *r++ = '$';
997 
998  SET_VARSIZE(result, r - ((char *) result));
999 
1000  return result;
1001 }
#define VARHDRSZ
Definition: c.h:676
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1058
e
Definition: preproc-init.c:82
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

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

Referenced by similar_escape(), similar_to_escape_1(), and similar_to_escape_2().

◆ similar_to_escape_1()

Datum similar_to_escape_1 ( PG_FUNCTION_ARGS  )

Definition at line 1023 of file regexp.c.

1024 {
1025  text *pat_text = PG_GETARG_TEXT_PP(0);
1026  text *result;
1027 
1028  result = similar_escape_internal(pat_text, NULL);
1029 
1030  PG_RETURN_TEXT_P(result);
1031 }

References PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and similar_escape_internal().

◆ similar_to_escape_2()

Datum similar_to_escape_2 ( PG_FUNCTION_ARGS  )

Definition at line 1007 of file regexp.c.

1008 {
1009  text *pat_text = PG_GETARG_TEXT_PP(0);
1010  text *esc_text = PG_GETARG_TEXT_PP(1);
1011  text *result;
1012 
1013  result = similar_escape_internal(pat_text, esc_text);
1014 
1015  PG_RETURN_TEXT_P(result);
1016 }

References PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and similar_escape_internal().

◆ texticregexeq()

Datum texticregexeq ( PG_FUNCTION_ARGS  )

Definition at line 551 of file regexp.c.

552 {
553  text *s = PG_GETARG_TEXT_PP(0);
554  text *p = PG_GETARG_TEXT_PP(1);
555 
557  VARDATA_ANY(s),
561  0, NULL));
562 }

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.

◆ texticregexne()

Datum texticregexne ( PG_FUNCTION_ARGS  )

Definition at line 565 of file regexp.c.

566 {
567  text *s = PG_GETARG_TEXT_PP(0);
568  text *p = PG_GETARG_TEXT_PP(1);
569 
571  VARDATA_ANY(s),
575  0, NULL));
576 }

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.

◆ textregexeq()

Datum textregexeq ( PG_FUNCTION_ARGS  )

Definition at line 488 of file regexp.c.

489 {
490  text *s = PG_GETARG_TEXT_PP(0);
491  text *p = PG_GETARG_TEXT_PP(1);
492 
494  VARDATA_ANY(s),
496  REG_ADVANCED,
498  0, NULL));
499 }

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

◆ textregexne()

Datum textregexne ( PG_FUNCTION_ARGS  )

Definition at line 502 of file regexp.c.

503 {
504  text *s = PG_GETARG_TEXT_PP(0);
505  text *p = PG_GETARG_TEXT_PP(1);
506 
508  VARDATA_ANY(s),
510  REG_ADVANCED,
512  0, NULL));
513 }

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

◆ textregexreplace()

Datum textregexreplace ( PG_FUNCTION_ARGS  )

Definition at line 659 of file regexp.c.

660 {
661  text *s = PG_GETARG_TEXT_PP(0);
662  text *p = PG_GETARG_TEXT_PP(1);
663  text *r = PG_GETARG_TEXT_PP(2);
664  text *opt = PG_GETARG_TEXT_PP(3);
665  pg_re_flags flags;
666 
667  /*
668  * regexp_replace() with four arguments will be preferentially resolved as
669  * this form when the fourth argument is of type UNKNOWN. However, the
670  * user might have intended to call textregexreplace_extended_no_n. If we
671  * see flags that look like an integer, emit the same error that
672  * parse_re_flags would, but add a HINT about how to fix it.
673  */
674  if (VARSIZE_ANY_EXHDR(opt) > 0)
675  {
676  char *opt_p = VARDATA_ANY(opt);
677 
678  if (*opt_p >= '0' && *opt_p <= '9')
679  ereport(ERROR,
680  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
681  errmsg("invalid regular expression option: \"%.*s\"",
682  pg_mblen(opt_p), opt_p),
683  errhint("If you meant to use regexp_replace() with a start parameter, cast the fourth argument to integer explicitly.")));
684  }
685 
686  parse_re_flags(&flags, opt);
687 
689  flags.cflags, PG_GET_COLLATION(),
690  0, flags.glob ? 0 : 1));
691 }
text * replace_text_regexp(text *src_text, text *pattern_text, text *replace_text, int cflags, Oid collation, int search_start, int n)
Definition: varlena.c:4203

References pg_re_flags::cflags, ereport, errcode(), errhint(), errmsg(), ERROR, pg_re_flags::glob, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, pg_mblen(), PG_RETURN_TEXT_P, replace_text_regexp(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by execute_extension_script().

◆ textregexreplace_extended()

Datum textregexreplace_extended ( PG_FUNCTION_ARGS  )

Definition at line 700 of file regexp.c.

701 {
702  text *s = PG_GETARG_TEXT_PP(0);
703  text *p = PG_GETARG_TEXT_PP(1);
704  text *r = PG_GETARG_TEXT_PP(2);
705  int start = 1;
706  int n = 1;
707  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
708  pg_re_flags re_flags;
709 
710  /* Collect optional parameters */
711  if (PG_NARGS() > 3)
712  {
713  start = PG_GETARG_INT32(3);
714  if (start <= 0)
715  ereport(ERROR,
716  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
717  errmsg("invalid value for parameter \"%s\": %d",
718  "start", start)));
719  }
720  if (PG_NARGS() > 4)
721  {
722  n = PG_GETARG_INT32(4);
723  if (n < 0)
724  ereport(ERROR,
725  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
726  errmsg("invalid value for parameter \"%s\": %d",
727  "n", n)));
728  }
729 
730  /* Determine options */
731  parse_re_flags(&re_flags, flags);
732 
733  /* If N was not specified, deduce it from the 'g' flag */
734  if (PG_NARGS() <= 4)
735  n = re_flags.glob ? 0 : 1;
736 
737  /* Do the replacement(s) */
739  re_flags.cflags, PG_GET_COLLATION(),
740  start - 1, n));
741 }

References pg_re_flags::cflags, ereport, errcode(), errmsg(), ERROR, pg_re_flags::glob, parse_re_flags(), PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_GETARG_TEXT_PP_IF_EXISTS, PG_NARGS, PG_RETURN_TEXT_P, and replace_text_regexp().

Referenced by textregexreplace_extended_no_flags(), and textregexreplace_extended_no_n().

◆ textregexreplace_extended_no_flags()

Datum textregexreplace_extended_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 752 of file regexp.c.

753 {
754  return textregexreplace_extended(fcinfo);
755 }
Datum textregexreplace_extended(PG_FUNCTION_ARGS)
Definition: regexp.c:700

References textregexreplace_extended().

◆ textregexreplace_extended_no_n()

Datum textregexreplace_extended_no_n ( PG_FUNCTION_ARGS  )

Definition at line 745 of file regexp.c.

746 {
747  return textregexreplace_extended(fcinfo);
748 }

References textregexreplace_extended().

◆ textregexreplace_noopt()

Datum textregexreplace_noopt ( PG_FUNCTION_ARGS  )

Definition at line 643 of file regexp.c.

644 {
645  text *s = PG_GETARG_TEXT_PP(0);
646  text *p = PG_GETARG_TEXT_PP(1);
647  text *r = PG_GETARG_TEXT_PP(2);
648 
651  0, 1));
652 }

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

◆ textregexsubstr()

Datum textregexsubstr ( PG_FUNCTION_ARGS  )

Definition at line 584 of file regexp.c.

585 {
586  text *s = PG_GETARG_TEXT_PP(0);
587  text *p = PG_GETARG_TEXT_PP(1);
588  regex_t *re;
589  regmatch_t pmatch[2];
590  int so,
591  eo;
592 
593  /* Compile RE */
595 
596  /*
597  * We pass two regmatch_t structs to get info about the overall match and
598  * the match for the first parenthesized subexpression (if any). If there
599  * is a parenthesized subexpression, we return what it matched; else
600  * return what the whole regexp matched.
601  */
602  if (!RE_execute(re,
604  2, pmatch))
605  PG_RETURN_NULL(); /* definitely no match */
606 
607  if (re->re_nsub > 0)
608  {
609  /* has parenthesized subexpressions, use the first one */
610  so = pmatch[1].rm_so;
611  eo = pmatch[1].rm_eo;
612  }
613  else
614  {
615  /* no parenthesized subexpression, use whole match */
616  so = pmatch[0].rm_so;
617  eo = pmatch[0].rm_eo;
618  }
619 
620  /*
621  * It is possible to have a match to the whole pattern but no match for a
622  * subexpression; for example 'foo(bar)?' is considered to match 'foo' but
623  * there is no subexpression match. So this extra test for match failure
624  * is not redundant.
625  */
626  if (so < 0 || eo < 0)
627  PG_RETURN_NULL();
628 
630  PointerGetDatum(s),
631  Int32GetDatum(so + 1),
632  Int32GetDatum(eo - so));
633 }

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.

Variable Documentation

◆ num_res

int num_res = 0
static

Definition at line 113 of file regexp.c.

Referenced by RE_compile_and_cache().

◆ re_array

cached_re_str re_array[MAX_CACHED_RES]
static

Definition at line 114 of file regexp.c.

Referenced by RE_compile_and_cache().

◆ RegexpCacheMemoryContext

MemoryContext RegexpCacheMemoryContext
static

Definition at line 100 of file regexp.c.

Referenced by RE_compile_and_cache().