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, 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 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

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

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

Referenced by regexp_match(), and regexp_matches().

1604 {
1605  char *buf = matchctx->conv_buf;
1606  Datum *elems = matchctx->elems;
1607  bool *nulls = matchctx->nulls;
1608  int dims[1];
1609  int lbs[1];
1610  int loc;
1611  int i;
1612 
1613  /* Extract matching substrings from the original string */
1614  loc = matchctx->next_match * matchctx->npatterns * 2;
1615  for (i = 0; i < matchctx->npatterns; i++)
1616  {
1617  int so = matchctx->match_locs[loc++];
1618  int eo = matchctx->match_locs[loc++];
1619 
1620  if (so < 0 || eo < 0)
1621  {
1622  elems[i] = (Datum) 0;
1623  nulls[i] = true;
1624  }
1625  else if (buf)
1626  {
1627  int len = pg_wchar2mb_with_len(matchctx->wide_str + so,
1628  buf,
1629  eo - so);
1630 
1631  Assert(len < matchctx->conv_bufsiz);
1632  elems[i] = PointerGetDatum(cstring_to_text_with_len(buf, len));
1633  nulls[i] = false;
1634  }
1635  else
1636  {
1638  PointerGetDatum(matchctx->orig_str),
1639  Int32GetDatum(so + 1),
1640  Int32GetDatum(eo - so));
1641  nulls[i] = false;
1642  }
1643  }
1644 
1645  /* And form an array */
1646  dims[0] = matchctx->npatterns;
1647  lbs[0] = 1;
1648  /* XXX: this hardcodes assumptions about the text type */
1649  return construct_md_array(elems, nulls, 1, dims, lbs,
1650  TEXTOID, -1, false, TYPALIGN_INT);
1651 }
Datum * elems
Definition: regexp.c:63
#define PointerGetDatum(X)
Definition: postgres.h:600
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:202
text * orig_str
Definition: regexp.c:55
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:839
uintptr_t Datum
Definition: postgres.h:411
pg_wchar * wide_str
Definition: regexp.c:65
#define Assert(condition)
Definition: c.h:804
#define Int32GetDatum(X)
Definition: postgres.h:523
int i
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:951
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:3353

◆ build_regexp_split_result()

static Datum build_regexp_split_result ( regexp_matches_ctx splitctx)
static

Definition at line 1774 of file regexp.c.

References Assert, buf, regexp_matches_ctx::conv_buf, 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_wchar2mb_with_len(), PointerGetDatum, startpos, text_substr(), and regexp_matches_ctx::wide_str.

Referenced by regexp_split_to_array(), and regexp_split_to_table().

1775 {
1776  char *buf = splitctx->conv_buf;
1777  int startpos;
1778  int endpos;
1779 
1780  if (splitctx->next_match > 0)
1781  startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
1782  else
1783  startpos = 0;
1784  if (startpos < 0)
1785  elog(ERROR, "invalid match ending position");
1786 
1787  endpos = splitctx->match_locs[splitctx->next_match * 2];
1788  if (endpos < startpos)
1789  elog(ERROR, "invalid match starting position");
1790 
1791  if (buf)
1792  {
1793  int len;
1794 
1795  len = pg_wchar2mb_with_len(splitctx->wide_str + startpos,
1796  buf,
1797  endpos - startpos);
1798  Assert(len < splitctx->conv_bufsiz);
1799  return PointerGetDatum(cstring_to_text_with_len(buf, len));
1800  }
1801  else
1802  {
1804  PointerGetDatum(splitctx->orig_str),
1805  Int32GetDatum(startpos + 1),
1806  Int32GetDatum(endpos - startpos));
1807  }
1808 }
#define PointerGetDatum(X)
Definition: postgres.h:600
int * match_locs
Definition: regexp.c:60
char * conv_buf
Definition: regexp.c:66
#define ERROR
Definition: elog.h:46
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:202
text * orig_str
Definition: regexp.c:55
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:839
pg_wchar * wide_str
Definition: regexp.c:65
#define Assert(condition)
Definition: c.h:804
static XLogRecPtr startpos
#define Int32GetDatum(X)
Definition: postgres.h:523
#define elog(elevel,...)
Definition: elog.h:232
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:951

◆ nameicregexeq()

Datum nameicregexeq ( PG_FUNCTION_ARGS  )

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

505 {
506  Name n = PG_GETARG_NAME(0);
507  text *p = PG_GETARG_TEXT_PP(1);
508 
510  NameStr(*n),
511  strlen(NameStr(*n)),
514  0, NULL));
515 }
#define REG_ICASE
Definition: regex.h:108
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
Definition: c.h:675
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ nameicregexne()

Datum nameicregexne ( PG_FUNCTION_ARGS  )

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

519 {
520  Name n = PG_GETARG_NAME(0);
521  text *p = PG_GETARG_TEXT_PP(1);
522 
524  NameStr(*n),
525  strlen(NameStr(*n)),
528  0, NULL));
529 }
#define REG_ICASE
Definition: regex.h:108
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
Definition: c.h:675
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ nameregexeq()

Datum nameregexeq ( PG_FUNCTION_ARGS  )

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

442 {
443  Name n = PG_GETARG_NAME(0);
444  text *p = PG_GETARG_TEXT_PP(1);
445 
447  NameStr(*n),
448  strlen(NameStr(*n)),
449  REG_ADVANCED,
451  0, NULL));
452 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
Definition: c.h:675
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ nameregexne()

Datum nameregexne ( PG_FUNCTION_ARGS  )

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

456 {
457  Name n = PG_GETARG_NAME(0);
458  text *p = PG_GETARG_TEXT_PP(1);
459 
461  NameStr(*n),
462  strlen(NameStr(*n)),
463  REG_ADVANCED,
465  0, NULL));
466 }
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
Definition: c.h:675
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ parse_re_flags()

static void parse_re_flags ( pg_re_flags flags,
text opts 
)
static

Definition at line 367 of file regexp.c.

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

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

◆ RE_compile_and_cache()

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

Definition at line 138 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, 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_extended(), textregexreplace_noopt(), and textregexsubstr().

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

References RE_compile_and_cache(), and RE_execute().

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

347 {
348  regex_t *re;
349 
350  /* Compile RE */
351  re = RE_compile_and_cache(text_re, cflags, collation);
352 
353  return RE_execute(re, dat, dat_len, nmatch, pmatch);
354 }
static bool RE_execute(regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:310
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
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 310 of file regexp.c.

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

Referenced by RE_compile_and_execute(), and textregexsubstr().

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

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

269 {
270  int regexec_result;
271  char errMsg[100];
272 
273  /* Perform RE match and return result */
274  regexec_result = pg_regexec(re,
275  data,
276  data_len,
277  start_search,
278  NULL, /* no details */
279  nmatch,
280  pmatch,
281  0);
282 
283  if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
284  {
285  /* re failed??? */
287  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
288  ereport(ERROR,
289  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
290  errmsg("regular expression failed: %s", errMsg)));
291  }
292 
293  return (regexec_result == REG_OKAY);
294 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define REG_OKAY
Definition: regex.h:139
#define ERROR
Definition: elog.h:46
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define ereport(elevel,...)
Definition: elog.h:157
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:176
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define REG_NOMATCH
Definition: regex.h:140
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120

◆ regexp_count()

Datum regexp_count ( PG_FUNCTION_ARGS  )

Definition at line 1053 of file regexp.c.

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

1054 {
1055  text *str = PG_GETARG_TEXT_PP(0);
1056  text *pattern = PG_GETARG_TEXT_PP(1);
1057  int start = 1;
1058  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(3);
1059  pg_re_flags re_flags;
1060  regexp_matches_ctx *matchctx;
1061 
1062  /* Collect optional parameters */
1063  if (PG_NARGS() > 2)
1064  {
1065  start = PG_GETARG_INT32(2);
1066  if (start <= 0)
1067  ereport(ERROR,
1068  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1069  errmsg("invalid value for parameter \"%s\": %d",
1070  "start", start)));
1071  }
1072 
1073  /* Determine options */
1074  parse_re_flags(&re_flags, flags);
1075  /* User mustn't specify 'g' */
1076  if (re_flags.glob)
1077  ereport(ERROR,
1078  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1079  /* translator: %s is a SQL function name */
1080  errmsg("%s does not support the \"global\" option",
1081  "regexp_count()")));
1082  /* But we find all the matches anyway */
1083  re_flags.glob = true;
1084 
1085  /* Do the matching */
1086  matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
1087  PG_GET_COLLATION(),
1088  false, /* can ignore subexprs */
1089  false, false);
1090 
1091  PG_RETURN_INT32(matchctx->nmatches);
1092 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
bool glob
Definition: regexp.c:49
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_NARGS()
Definition: fmgr.h:203
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621

◆ regexp_count_no_flags()

Datum regexp_count_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1103 of file regexp.c.

References regexp_count().

1104 {
1105  return regexp_count(fcinfo);
1106 }
Datum regexp_count(PG_FUNCTION_ARGS)
Definition: regexp.c:1053

◆ regexp_count_no_start()

Datum regexp_count_no_start ( PG_FUNCTION_ARGS  )

Definition at line 1096 of file regexp.c.

References regexp_count().

1097 {
1098  return regexp_count(fcinfo);
1099 }
Datum regexp_count(PG_FUNCTION_ARGS)
Definition: regexp.c:1053

◆ regexp_fixed_prefix()

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

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

1938 {
1939  char *result;
1940  regex_t *re;
1941  int cflags;
1942  int re_result;
1943  pg_wchar *str;
1944  size_t slen;
1945  size_t maxlen;
1946  char errMsg[100];
1947 
1948  *exact = false; /* default result */
1949 
1950  /* Compile RE */
1951  cflags = REG_ADVANCED;
1952  if (case_insensitive)
1953  cflags |= REG_ICASE;
1954 
1955  re = RE_compile_and_cache(text_re, cflags, collation);
1956 
1957  /* Examine it to see if there's a fixed prefix */
1958  re_result = pg_regprefix(re, &str, &slen);
1959 
1960  switch (re_result)
1961  {
1962  case REG_NOMATCH:
1963  return NULL;
1964 
1965  case REG_PREFIX:
1966  /* continue with wchar conversion */
1967  break;
1968 
1969  case REG_EXACT:
1970  *exact = true;
1971  /* continue with wchar conversion */
1972  break;
1973 
1974  default:
1975  /* re failed??? */
1977  pg_regerror(re_result, re, errMsg, sizeof(errMsg));
1978  ereport(ERROR,
1979  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
1980  errmsg("regular expression failed: %s", errMsg)));
1981  break;
1982  }
1983 
1984  /* Convert pg_wchar result back to database encoding */
1985  maxlen = pg_database_encoding_max_length() * slen + 1;
1986  result = (char *) palloc(maxlen);
1987  slen = pg_wchar2mb_with_len(str, result, slen);
1988  Assert(slen < maxlen);
1989 
1990  free(str);
1991 
1992  return result;
1993 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define REG_PREFIX
Definition: regex.h:164
#define REG_ICASE
Definition: regex.h:108
int pg_regprefix(regex_t *re, chr **string, size_t *slength)
Definition: regprefix.c:46
#define ERROR
Definition: elog.h:46
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:105
unsigned int pg_wchar
Definition: mbprint.c:31
#define ereport(elevel,...)
Definition: elog.h:157
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:804
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495
#define REG_EXACT
Definition: regex.h:165
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define REG_NOMATCH
Definition: regex.h:140
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:951
Definition: regex.h:55

◆ regexp_instr()

Datum regexp_instr ( PG_FUNCTION_ARGS  )

Definition at line 1113 of file regexp.c.

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

1114 {
1115  text *str = PG_GETARG_TEXT_PP(0);
1116  text *pattern = PG_GETARG_TEXT_PP(1);
1117  int start = 1;
1118  int n = 1;
1119  int endoption = 0;
1120  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
1121  int subexpr = 0;
1122  int pos;
1123  pg_re_flags re_flags;
1124  regexp_matches_ctx *matchctx;
1125 
1126  /* Collect optional parameters */
1127  if (PG_NARGS() > 2)
1128  {
1129  start = PG_GETARG_INT32(2);
1130  if (start <= 0)
1131  ereport(ERROR,
1132  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1133  errmsg("invalid value for parameter \"%s\": %d",
1134  "start", start)));
1135  }
1136  if (PG_NARGS() > 3)
1137  {
1138  n = PG_GETARG_INT32(3);
1139  if (n <= 0)
1140  ereport(ERROR,
1141  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1142  errmsg("invalid value for parameter \"%s\": %d",
1143  "n", n)));
1144  }
1145  if (PG_NARGS() > 4)
1146  {
1147  endoption = PG_GETARG_INT32(4);
1148  if (endoption != 0 && endoption != 1)
1149  ereport(ERROR,
1150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1151  errmsg("invalid value for parameter \"%s\": %d",
1152  "endoption", endoption)));
1153  }
1154  if (PG_NARGS() > 6)
1155  {
1156  subexpr = PG_GETARG_INT32(6);
1157  if (subexpr < 0)
1158  ereport(ERROR,
1159  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1160  errmsg("invalid value for parameter \"%s\": %d",
1161  "subexpr", subexpr)));
1162  }
1163 
1164  /* Determine options */
1165  parse_re_flags(&re_flags, flags);
1166  /* User mustn't specify 'g' */
1167  if (re_flags.glob)
1168  ereport(ERROR,
1169  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1170  /* translator: %s is a SQL function name */
1171  errmsg("%s does not support the \"global\" option",
1172  "regexp_instr()")));
1173  /* But we find all the matches anyway */
1174  re_flags.glob = true;
1175 
1176  /* Do the matching */
1177  matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
1178  PG_GET_COLLATION(),
1179  (subexpr > 0), /* need submatches? */
1180  false, false);
1181 
1182  /* When n exceeds matches return 0 (includes case of no matches) */
1183  if (n > matchctx->nmatches)
1184  PG_RETURN_INT32(0);
1185 
1186  /* When subexpr exceeds number of subexpressions return 0 */
1187  if (subexpr > matchctx->npatterns)
1188  PG_RETURN_INT32(0);
1189 
1190  /* Select the appropriate match position to return */
1191  pos = (n - 1) * matchctx->npatterns;
1192  if (subexpr > 0)
1193  pos += subexpr - 1;
1194  pos *= 2;
1195  if (endoption == 1)
1196  pos += 1;
1197 
1198  if (matchctx->match_locs[pos] >= 0)
1199  PG_RETURN_INT32(matchctx->match_locs[pos] + 1);
1200  else
1201  PG_RETURN_INT32(0); /* position not identifiable */
1202 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
bool glob
Definition: regexp.c:49
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
int errcode(int sqlerrcode)
Definition: elog.c:698
int * match_locs
Definition: regexp.c:60
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_NARGS()
Definition: fmgr.h:203
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621

◆ regexp_instr_no_endoption()

Datum regexp_instr_no_endoption ( PG_FUNCTION_ARGS  )

Definition at line 1220 of file regexp.c.

References regexp_instr().

1221 {
1222  return regexp_instr(fcinfo);
1223 }
Datum regexp_instr(PG_FUNCTION_ARGS)
Definition: regexp.c:1113

◆ regexp_instr_no_flags()

Datum regexp_instr_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1227 of file regexp.c.

References regexp_instr().

1228 {
1229  return regexp_instr(fcinfo);
1230 }
Datum regexp_instr(PG_FUNCTION_ARGS)
Definition: regexp.c:1113

◆ regexp_instr_no_n()

Datum regexp_instr_no_n ( PG_FUNCTION_ARGS  )

Definition at line 1213 of file regexp.c.

References regexp_instr().

1214 {
1215  return regexp_instr(fcinfo);
1216 }
Datum regexp_instr(PG_FUNCTION_ARGS)
Definition: regexp.c:1113

◆ regexp_instr_no_start()

Datum regexp_instr_no_start ( PG_FUNCTION_ARGS  )

Definition at line 1206 of file regexp.c.

References regexp_instr().

1207 {
1208  return regexp_instr(fcinfo);
1209 }
Datum regexp_instr(PG_FUNCTION_ARGS)
Definition: regexp.c:1113

◆ regexp_instr_no_subexpr()

Datum regexp_instr_no_subexpr ( PG_FUNCTION_ARGS  )

Definition at line 1234 of file regexp.c.

References regexp_instr().

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

◆ regexp_like()

Datum regexp_like ( PG_FUNCTION_ARGS  )

Definition at line 1244 of file regexp.c.

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

1245 {
1246  text *str = PG_GETARG_TEXT_PP(0);
1247  text *pattern = PG_GETARG_TEXT_PP(1);
1248  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1249  pg_re_flags re_flags;
1250 
1251  /* Determine options */
1252  parse_re_flags(&re_flags, flags);
1253  /* User mustn't specify 'g' */
1254  if (re_flags.glob)
1255  ereport(ERROR,
1256  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1257  /* translator: %s is a SQL function name */
1258  errmsg("%s does not support the \"global\" option",
1259  "regexp_like()")));
1260 
1261  /* Otherwise it's like textregexeq/texticregexeq */
1263  VARDATA_ANY(str),
1264  VARSIZE_ANY_EXHDR(str),
1265  re_flags.cflags,
1266  PG_GET_COLLATION(),
1267  0, NULL));
1268 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
int cflags
Definition: regexp.c:48
bool glob
Definition: regexp.c:49
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
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:344
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define ereport(elevel,...)
Definition: elog.h:157
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621

◆ regexp_like_no_flags()

Datum regexp_like_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1272 of file regexp.c.

References regexp_like().

1273 {
1274  return regexp_like(fcinfo);
1275 }
Datum regexp_like(PG_FUNCTION_ARGS)
Definition: regexp.c:1244

◆ regexp_match()

Datum regexp_match ( PG_FUNCTION_ARGS  )

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

1283 {
1284  text *orig_str = PG_GETARG_TEXT_PP(0);
1285  text *pattern = PG_GETARG_TEXT_PP(1);
1286  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1287  pg_re_flags re_flags;
1288  regexp_matches_ctx *matchctx;
1289 
1290  /* Determine options */
1291  parse_re_flags(&re_flags, flags);
1292  /* User mustn't specify 'g' */
1293  if (re_flags.glob)
1294  ereport(ERROR,
1295  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1296  /* translator: %s is a SQL function name */
1297  errmsg("%s does not support the \"global\" option",
1298  "regexp_match()"),
1299  errhint("Use the regexp_matches function instead.")));
1300 
1301  matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, 0,
1302  PG_GET_COLLATION(), true, false, false);
1303 
1304  if (matchctx->nmatches == 0)
1305  PG_RETURN_NULL();
1306 
1307  Assert(matchctx->nmatches == 1);
1308 
1309  /* Create workspace that build_regexp_match_result needs */
1310  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
1311  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
1312 
1314 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
Datum * elems
Definition: regexp.c:63
#define PointerGetDatum(X)
Definition: postgres.h:600
bool glob
Definition: regexp.c:49
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
int errcode(int sqlerrcode)
Definition: elog.c:698
bool * nulls
Definition: regexp.c:64
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
uintptr_t Datum
Definition: postgres.h:411
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1603
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ regexp_match_no_flags()

Datum regexp_match_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1318 of file regexp.c.

References regexp_match().

1319 {
1320  return regexp_match(fcinfo);
1321 }
Datum regexp_match(PG_FUNCTION_ARGS)
Definition: regexp.c:1282

◆ regexp_matches()

Datum regexp_matches ( PG_FUNCTION_ARGS  )

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

1329 {
1330  FuncCallContext *funcctx;
1331  regexp_matches_ctx *matchctx;
1332 
1333  if (SRF_IS_FIRSTCALL())
1334  {
1335  text *pattern = PG_GETARG_TEXT_PP(1);
1336  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1337  pg_re_flags re_flags;
1338  MemoryContext oldcontext;
1339 
1340  funcctx = SRF_FIRSTCALL_INIT();
1341  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1342 
1343  /* Determine options */
1344  parse_re_flags(&re_flags, flags);
1345 
1346  /* be sure to copy the input string into the multi-call ctx */
1347  matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1348  &re_flags, 0,
1349  PG_GET_COLLATION(),
1350  true, false, false);
1351 
1352  /* Pre-create workspace that build_regexp_match_result needs */
1353  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
1354  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
1355 
1356  MemoryContextSwitchTo(oldcontext);
1357  funcctx->user_fctx = (void *) matchctx;
1358  }
1359 
1360  funcctx = SRF_PERCALL_SETUP();
1361  matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
1362 
1363  if (matchctx->next_match < matchctx->nmatches)
1364  {
1365  ArrayType *result_ary;
1366 
1367  result_ary = build_regexp_match_result(matchctx);
1368  matchctx->next_match++;
1369  SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
1370  }
1371 
1372  SRF_RETURN_DONE(funcctx);
1373 }
Datum * elems
Definition: regexp.c:63
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define PointerGetDatum(X)
Definition: postgres.h:600
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
bool * nulls
Definition: regexp.c:64
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:315
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
uintptr_t Datum
Definition: postgres.h:411
static ArrayType * build_regexp_match_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1603
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:1062
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ regexp_matches_no_flags()

Datum regexp_matches_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1377 of file regexp.c.

References regexp_matches().

1378 {
1379  return regexp_matches(fcinfo);
1380 }
Datum regexp_matches(PG_FUNCTION_ARGS)
Definition: regexp.c:1328

◆ regexp_split_to_array()

Datum regexp_split_to_array ( PG_FUNCTION_ARGS  )

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

1724 {
1725  ArrayBuildState *astate = NULL;
1726  pg_re_flags re_flags;
1727  regexp_matches_ctx *splitctx;
1728 
1729  /* Determine options */
1731  /* User mustn't specify 'g' */
1732  if (re_flags.glob)
1733  ereport(ERROR,
1734  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1735  /* translator: %s is a SQL function name */
1736  errmsg("%s does not support the \"global\" option",
1737  "regexp_split_to_array()")));
1738  /* But we find all the matches anyway */
1739  re_flags.glob = true;
1740 
1742  PG_GETARG_TEXT_PP(1),
1743  &re_flags, 0,
1744  PG_GET_COLLATION(),
1745  false, true, true);
1746 
1747  while (splitctx->next_match <= splitctx->nmatches)
1748  {
1749  astate = accumArrayResult(astate,
1750  build_regexp_split_result(splitctx),
1751  false,
1752  TEXTOID,
1754  splitctx->next_match++;
1755  }
1756 
1758 }
bool glob
Definition: regexp.c:49
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5151
#define ereport(elevel,...)
Definition: elog.h:157
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5087
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1774
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367

◆ regexp_split_to_array_no_flags()

Datum regexp_split_to_array_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1762 of file regexp.c.

References regexp_split_to_array().

1763 {
1764  return regexp_split_to_array(fcinfo);
1765 }
Datum regexp_split_to_array(PG_FUNCTION_ARGS)
Definition: regexp.c:1723

◆ regexp_split_to_table()

Datum regexp_split_to_table ( PG_FUNCTION_ARGS  )

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

1660 {
1661  FuncCallContext *funcctx;
1662  regexp_matches_ctx *splitctx;
1663 
1664  if (SRF_IS_FIRSTCALL())
1665  {
1666  text *pattern = PG_GETARG_TEXT_PP(1);
1667  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1668  pg_re_flags re_flags;
1669  MemoryContext oldcontext;
1670 
1671  funcctx = SRF_FIRSTCALL_INIT();
1672  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1673 
1674  /* Determine options */
1675  parse_re_flags(&re_flags, flags);
1676  /* User mustn't specify 'g' */
1677  if (re_flags.glob)
1678  ereport(ERROR,
1679  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1680  /* translator: %s is a SQL function name */
1681  errmsg("%s does not support the \"global\" option",
1682  "regexp_split_to_table()")));
1683  /* But we find all the matches anyway */
1684  re_flags.glob = true;
1685 
1686  /* be sure to copy the input string into the multi-call ctx */
1687  splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1688  &re_flags, 0,
1689  PG_GET_COLLATION(),
1690  false, true, true);
1691 
1692  MemoryContextSwitchTo(oldcontext);
1693  funcctx->user_fctx = (void *) splitctx;
1694  }
1695 
1696  funcctx = SRF_PERCALL_SETUP();
1697  splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
1698 
1699  if (splitctx->next_match <= splitctx->nmatches)
1700  {
1701  Datum result = build_regexp_split_result(splitctx);
1702 
1703  splitctx->next_match++;
1704  SRF_RETURN_NEXT(funcctx, result);
1705  }
1706 
1707  SRF_RETURN_DONE(funcctx);
1708 }
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
bool glob
Definition: regexp.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:315
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
uintptr_t Datum
Definition: postgres.h:411
#define ereport(elevel,...)
Definition: elog.h:157
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1774
void * user_fctx
Definition: funcapi.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ regexp_split_to_table_no_flags()

Datum regexp_split_to_table_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1712 of file regexp.c.

References regexp_split_to_table().

1713 {
1714  return regexp_split_to_table(fcinfo);
1715 }
Datum regexp_split_to_table(PG_FUNCTION_ARGS)
Definition: regexp.c:1659

◆ regexp_substr()

Datum regexp_substr ( PG_FUNCTION_ARGS  )

Definition at line 1815 of file regexp.c.

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

1816 {
1817  text *str = PG_GETARG_TEXT_PP(0);
1818  text *pattern = PG_GETARG_TEXT_PP(1);
1819  int start = 1;
1820  int n = 1;
1821  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(4);
1822  int subexpr = 0;
1823  int so,
1824  eo,
1825  pos;
1826  pg_re_flags re_flags;
1827  regexp_matches_ctx *matchctx;
1828 
1829  /* Collect optional parameters */
1830  if (PG_NARGS() > 2)
1831  {
1832  start = PG_GETARG_INT32(2);
1833  if (start <= 0)
1834  ereport(ERROR,
1835  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1836  errmsg("invalid value for parameter \"%s\": %d",
1837  "start", start)));
1838  }
1839  if (PG_NARGS() > 3)
1840  {
1841  n = PG_GETARG_INT32(3);
1842  if (n <= 0)
1843  ereport(ERROR,
1844  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1845  errmsg("invalid value for parameter \"%s\": %d",
1846  "n", n)));
1847  }
1848  if (PG_NARGS() > 5)
1849  {
1850  subexpr = PG_GETARG_INT32(5);
1851  if (subexpr < 0)
1852  ereport(ERROR,
1853  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1854  errmsg("invalid value for parameter \"%s\": %d",
1855  "subexpr", subexpr)));
1856  }
1857 
1858  /* Determine options */
1859  parse_re_flags(&re_flags, flags);
1860  /* User mustn't specify 'g' */
1861  if (re_flags.glob)
1862  ereport(ERROR,
1863  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1864  /* translator: %s is a SQL function name */
1865  errmsg("%s does not support the \"global\" option",
1866  "regexp_substr()")));
1867  /* But we find all the matches anyway */
1868  re_flags.glob = true;
1869 
1870  /* Do the matching */
1871  matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
1872  PG_GET_COLLATION(),
1873  (subexpr > 0), /* need submatches? */
1874  false, false);
1875 
1876  /* When n exceeds matches return NULL (includes case of no matches) */
1877  if (n > matchctx->nmatches)
1878  PG_RETURN_NULL();
1879 
1880  /* When subexpr exceeds number of subexpressions return NULL */
1881  if (subexpr > matchctx->npatterns)
1882  PG_RETURN_NULL();
1883 
1884  /* Select the appropriate match position to return */
1885  pos = (n - 1) * matchctx->npatterns;
1886  if (subexpr > 0)
1887  pos += subexpr - 1;
1888  pos *= 2;
1889  so = matchctx->match_locs[pos];
1890  eo = matchctx->match_locs[pos + 1];
1891 
1892  if (so < 0 || eo < 0)
1893  PG_RETURN_NULL(); /* unidentifiable location */
1894 
1896  PointerGetDatum(matchctx->orig_str),
1897  Int32GetDatum(so + 1),
1898  Int32GetDatum(eo - so)));
1899 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PointerGetDatum(X)
Definition: postgres.h:600
bool glob
Definition: regexp.c:49
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *flags, int start_search, Oid collation, bool use_subpatterns, bool ignore_degenerate, bool fetching_unmatched)
Definition: regexp.c:1403
int errcode(int sqlerrcode)
Definition: elog.c:698
int * match_locs
Definition: regexp.c:60
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
text * orig_str
Definition: regexp.c:55
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:839
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_NARGS()
Definition: fmgr.h:203
#define Int32GetDatum(X)
Definition: postgres.h:523
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ regexp_substr_no_flags()

Datum regexp_substr_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 1917 of file regexp.c.

References regexp_substr().

1918 {
1919  return regexp_substr(fcinfo);
1920 }
Datum regexp_substr(PG_FUNCTION_ARGS)
Definition: regexp.c:1815

◆ regexp_substr_no_n()

Datum regexp_substr_no_n ( PG_FUNCTION_ARGS  )

Definition at line 1910 of file regexp.c.

References regexp_substr().

1911 {
1912  return regexp_substr(fcinfo);
1913 }
Datum regexp_substr(PG_FUNCTION_ARGS)
Definition: regexp.c:1815

◆ regexp_substr_no_start()

Datum regexp_substr_no_start ( PG_FUNCTION_ARGS  )

Definition at line 1903 of file regexp.c.

References regexp_substr().

1904 {
1905  return regexp_substr(fcinfo);
1906 }
Datum regexp_substr(PG_FUNCTION_ARGS)
Definition: regexp.c:1815

◆ regexp_substr_no_subexpr()

Datum regexp_substr_no_subexpr ( PG_FUNCTION_ARGS  )

Definition at line 1924 of file regexp.c.

References regexp_substr().

1925 {
1926  return regexp_substr(fcinfo);
1927 }
Datum regexp_substr(PG_FUNCTION_ARGS)
Definition: regexp.c:1815

◆ setup_regexp_matches()

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

Definition at line 1403 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_count(), regexp_instr(), regexp_match(), regexp_matches(), regexp_split_to_array(), regexp_split_to_table(), and regexp_substr().

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

◆ similar_escape()

Datum similar_escape ( PG_FUNCTION_ARGS  )

Definition at line 1027 of file regexp.c.

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

1028 {
1029  text *pat_text;
1030  text *esc_text;
1031  text *result;
1032 
1033  /* This function is not strict, so must test explicitly */
1034  if (PG_ARGISNULL(0))
1035  PG_RETURN_NULL();
1036  pat_text = PG_GETARG_TEXT_PP(0);
1037 
1038  if (PG_ARGISNULL(1))
1039  esc_text = NULL; /* use default escape character */
1040  else
1041  esc_text = PG_GETARG_TEXT_PP(1);
1042 
1043  result = similar_escape_internal(pat_text, esc_text);
1044 
1045  PG_RETURN_TEXT_P(result);
1046 }
static text * similar_escape_internal(text *pat_text, text *esc_text)
Definition: regexp.c:754
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ similar_escape_internal()

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

Definition at line 754 of file regexp.c.

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

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

◆ similar_to_escape_1()

Datum similar_to_escape_1 ( PG_FUNCTION_ARGS  )

Definition at line 1009 of file regexp.c.

References PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and similar_escape_internal().

1010 {
1011  text *pat_text = PG_GETARG_TEXT_PP(0);
1012  text *result;
1013 
1014  result = similar_escape_internal(pat_text, NULL);
1015 
1016  PG_RETURN_TEXT_P(result);
1017 }
static text * similar_escape_internal(text *pat_text, text *esc_text)
Definition: regexp.c:754
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:621

◆ similar_to_escape_2()

Datum similar_to_escape_2 ( PG_FUNCTION_ARGS  )

Definition at line 993 of file regexp.c.

References PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and similar_escape_internal().

994 {
995  text *pat_text = PG_GETARG_TEXT_PP(0);
996  text *esc_text = PG_GETARG_TEXT_PP(1);
997  text *result;
998 
999  result = similar_escape_internal(pat_text, esc_text);
1000 
1001  PG_RETURN_TEXT_P(result);
1002 }
static text * similar_escape_internal(text *pat_text, text *esc_text)
Definition: regexp.c:754
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: c.h:621

◆ texticregexeq()

Datum texticregexeq ( PG_FUNCTION_ARGS  )

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

533 {
534  text *s = PG_GETARG_TEXT_PP(0);
535  text *p = PG_GETARG_TEXT_PP(1);
536 
538  VARDATA_ANY(s),
542  0, NULL));
543 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define REG_ICASE
Definition: regex.h:108
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ texticregexne()

Datum texticregexne ( PG_FUNCTION_ARGS  )

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

547 {
548  text *s = PG_GETARG_TEXT_PP(0);
549  text *p = PG_GETARG_TEXT_PP(1);
550 
552  VARDATA_ANY(s),
556  0, NULL));
557 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define REG_ICASE
Definition: regex.h:108
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ textregexeq()

Datum textregexeq ( PG_FUNCTION_ARGS  )

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

470 {
471  text *s = PG_GETARG_TEXT_PP(0);
472  text *p = PG_GETARG_TEXT_PP(1);
473 
475  VARDATA_ANY(s),
477  REG_ADVANCED,
479  0, NULL));
480 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ textregexne()

Datum textregexne ( PG_FUNCTION_ARGS  )

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

484 {
485  text *s = PG_GETARG_TEXT_PP(0);
486  text *p = PG_GETARG_TEXT_PP(1);
487 
489  VARDATA_ANY(s),
491  REG_ADVANCED,
493  0, NULL));
494 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
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:344
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ textregexreplace()

Datum textregexreplace ( PG_FUNCTION_ARGS  )

Definition at line 641 of file regexp.c.

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, RE_compile_and_cache(), replace_text_regexp(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by execute_extension_script().

642 {
643  text *s = PG_GETARG_TEXT_PP(0);
644  text *p = PG_GETARG_TEXT_PP(1);
645  text *r = PG_GETARG_TEXT_PP(2);
646  text *opt = PG_GETARG_TEXT_PP(3);
647  regex_t *re;
648  pg_re_flags flags;
649 
650  /*
651  * regexp_replace() with four arguments will be preferentially resolved as
652  * this form when the fourth argument is of type UNKNOWN. However, the
653  * user might have intended to call textregexreplace_extended_no_n. If we
654  * see flags that look like an integer, emit the same error that
655  * parse_re_flags would, but add a HINT about how to fix it.
656  */
657  if (VARSIZE_ANY_EXHDR(opt) > 0)
658  {
659  char *opt_p = VARDATA_ANY(opt);
660 
661  if (*opt_p >= '0' && *opt_p <= '9')
662  ereport(ERROR,
663  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
664  errmsg("invalid regular expression option: \"%.*s\"",
665  pg_mblen(opt_p), opt_p),
666  errhint("If you meant to use regexp_replace() with a start parameter, cast the fourth argument to integer explicitly.")));
667  }
668 
669  parse_re_flags(&flags, opt);
670 
671  re = RE_compile_and_cache(p, flags.cflags, PG_GET_COLLATION());
672 
673  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, 0,
674  flags.glob ? 0 : 1));
675 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, int search_start, int n)
Definition: varlena.c:4509
int cflags
Definition: regexp.c:48
bool glob
Definition: regexp.c:49
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621
Definition: regex.h:55

◆ textregexreplace_extended()

Datum textregexreplace_extended ( PG_FUNCTION_ARGS  )

Definition at line 684 of file regexp.c.

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, RE_compile_and_cache(), and replace_text_regexp().

Referenced by textregexreplace_extended_no_flags(), and textregexreplace_extended_no_n().

685 {
686  text *s = PG_GETARG_TEXT_PP(0);
687  text *p = PG_GETARG_TEXT_PP(1);
688  text *r = PG_GETARG_TEXT_PP(2);
689  int start = 1;
690  int n = 1;
691  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
692  pg_re_flags re_flags;
693  regex_t *re;
694 
695  /* Collect optional parameters */
696  if (PG_NARGS() > 3)
697  {
698  start = PG_GETARG_INT32(3);
699  if (start <= 0)
700  ereport(ERROR,
701  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
702  errmsg("invalid value for parameter \"%s\": %d",
703  "start", start)));
704  }
705  if (PG_NARGS() > 4)
706  {
707  n = PG_GETARG_INT32(4);
708  if (n < 0)
709  ereport(ERROR,
710  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
711  errmsg("invalid value for parameter \"%s\": %d",
712  "n", n)));
713  }
714 
715  /* Determine options */
716  parse_re_flags(&re_flags, flags);
717 
718  /* If N was not specified, deduce it from the 'g' flag */
719  if (PG_NARGS() <= 4)
720  n = re_flags.glob ? 0 : 1;
721 
722  /* Compile the regular expression */
723  re = RE_compile_and_cache(p, re_flags.cflags, PG_GET_COLLATION());
724 
725  /* Do the replacement(s) */
726  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, start - 1, n));
727 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, int search_start, int n)
Definition: varlena.c:4509
int cflags
Definition: regexp.c:48
bool glob
Definition: regexp.c:49
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:41
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_NARGS()
Definition: fmgr.h:203
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:367
Definition: c.h:621
Definition: regex.h:55

◆ textregexreplace_extended_no_flags()

Datum textregexreplace_extended_no_flags ( PG_FUNCTION_ARGS  )

Definition at line 738 of file regexp.c.

References textregexreplace_extended().

739 {
740  return textregexreplace_extended(fcinfo);
741 }
Datum textregexreplace_extended(PG_FUNCTION_ARGS)
Definition: regexp.c:684

◆ textregexreplace_extended_no_n()

Datum textregexreplace_extended_no_n ( PG_FUNCTION_ARGS  )

Definition at line 731 of file regexp.c.

References textregexreplace_extended().

732 {
733  return textregexreplace_extended(fcinfo);
734 }
Datum textregexreplace_extended(PG_FUNCTION_ARGS)
Definition: regexp.c:684

◆ textregexreplace_noopt()

Datum textregexreplace_noopt ( PG_FUNCTION_ARGS  )

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

625 {
626  text *s = PG_GETARG_TEXT_PP(0);
627  text *p = PG_GETARG_TEXT_PP(1);
628  text *r = PG_GETARG_TEXT_PP(2);
629  regex_t *re;
630 
632 
633  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, 0, 1));
634 }
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, int search_start, int n)
Definition: varlena.c:4509
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define REG_ADVANCED
Definition: regex.h:105
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:138
Definition: c.h:621
Definition: regex.h:55

◆ textregexsubstr()

Datum textregexsubstr ( PG_FUNCTION_ARGS  )

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

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

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.