PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
regexp.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * regexp.c
4  * Postgres' interface to the regular expression package.
5  *
6  * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/regexp.c
12  *
13  * Alistair Crooks added the code for the regex caching
14  * agc - cached the regular expressions used - there's a good chance
15  * that we'll get a hit, so this saves a compile step for every
16  * attempted match. I haven't actually measured the speed improvement,
17  * but it `looks' a lot quicker visually when watching regression
18  * test output.
19  *
20  * agc - incorporated Keith Bostic's Berkeley regex code into
21  * the tree for all ports. To distinguish this regex code from any that
22  * is existent on a platform, I've prepended the string "pg_" to
23  * the functions regcomp, regerror, regexec and regfree.
24  * Fixed a bug that was originally a typo by me, where `i' was used
25  * instead of `oldest' when compiling regular expressions - benign
26  * results mostly, although occasionally it bit you...
27  *
28  *-------------------------------------------------------------------------
29  */
30 #include "postgres.h"
31 
32 #include "catalog/pg_type.h"
33 #include "funcapi.h"
34 #include "miscadmin.h"
35 #include "regex/regex.h"
36 #include "utils/array.h"
37 #include "utils/builtins.h"
38 
39 #define PG_GETARG_TEXT_PP_IF_EXISTS(_n) \
40  (PG_NARGS() > (_n) ? PG_GETARG_TEXT_PP(_n) : NULL)
41 
42 
43 /* all the options of interest for regex functions */
44 typedef struct pg_re_flags
45 {
46  int cflags; /* compile flags for Spencer's regex code */
47  bool glob; /* do it globally (for each occurrence) */
48 } pg_re_flags;
49 
50 /* cross-call state for regexp_matches(), also regexp_split() */
51 typedef struct regexp_matches_ctx
52 {
53  text *orig_str; /* data string in original TEXT form */
54  int nmatches; /* number of places where pattern matched */
55  int npatterns; /* number of capturing subpatterns */
56  /* We store start char index and end+1 char index for each match */
57  /* so the number of entries in match_locs is nmatches * npatterns * 2 */
58  int *match_locs; /* 0-based character indexes */
59  int next_match; /* 0-based index of next match to process */
60  /* workspace for build_regexp_matches_result() */
61  Datum *elems; /* has npatterns elements */
62  bool *nulls; /* has npatterns elements */
64 
65 /*
66  * We cache precompiled regular expressions using a "self organizing list"
67  * structure, in which recently-used items tend to be near the front.
68  * Whenever we use an entry, it's moved up to the front of the list.
69  * Over time, an item's average position corresponds to its frequency of use.
70  *
71  * When we first create an entry, it's inserted at the front of
72  * the array, dropping the entry at the end of the array if necessary to
73  * make room. (This might seem to be weighting the new entry too heavily,
74  * but if we insert new entries further back, we'll be unable to adjust to
75  * a sudden shift in the query mix where we are presented with MAX_CACHED_RES
76  * never-before-seen items used circularly. We ought to be able to handle
77  * that case, so we have to insert at the front.)
78  *
79  * Knuth mentions a variant strategy in which a used item is moved up just
80  * one place in the list. Although he says this uses fewer comparisons on
81  * average, it seems not to adapt very well to the situation where you have
82  * both some reusable patterns and a steady stream of non-reusable patterns.
83  * A reusable pattern that isn't used at least as often as non-reusable
84  * patterns are seen will "fail to keep up" and will drop off the end of the
85  * cache. With move-to-front, a reusable pattern is guaranteed to stay in
86  * the cache as long as it's used at least once in every MAX_CACHED_RES uses.
87  */
88 
89 /* this is the maximum number of cached regular expressions */
90 #ifndef MAX_CACHED_RES
91 #define MAX_CACHED_RES 32
92 #endif
93 
94 /* this structure describes one cached regular expression */
95 typedef struct cached_re_str
96 {
97  char *cre_pat; /* original RE (not null terminated!) */
98  int cre_pat_len; /* length of original RE, in bytes */
99  int cre_flags; /* compile flags: extended,icase etc */
100  Oid cre_collation; /* collation to use */
101  regex_t cre_re; /* the compiled regular expression */
102 } cached_re_str;
103 
104 static int num_res = 0; /* # of cached re's */
105 static cached_re_str re_array[MAX_CACHED_RES]; /* cached re's */
106 
107 
108 /* Local functions */
109 static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern,
110  text *flags,
111  Oid collation,
112  bool force_glob,
113  bool use_subpatterns,
114  bool ignore_degenerate);
115 static void cleanup_regexp_matches(regexp_matches_ctx *matchctx);
118 
119 
120 /*
121  * RE_compile_and_cache - compile a RE, caching if possible
122  *
123  * Returns regex_t *
124  *
125  * text_re --- the pattern, expressed as a TEXT object
126  * cflags --- compile options for the pattern
127  * collation --- collation to use for LC_CTYPE-dependent behavior
128  *
129  * Pattern is given in the database encoding. We internally convert to
130  * an array of pg_wchar, which is what Spencer's regex package wants.
131  */
132 static regex_t *
133 RE_compile_and_cache(text *text_re, int cflags, Oid collation)
134 {
135  int text_re_len = VARSIZE_ANY_EXHDR(text_re);
136  char *text_re_val = VARDATA_ANY(text_re);
137  pg_wchar *pattern;
138  int pattern_len;
139  int i;
140  int regcomp_result;
141  cached_re_str re_temp;
142  char errMsg[100];
143 
144  /*
145  * Look for a match among previously compiled REs. Since the data
146  * structure is self-organizing with most-used entries at the front, our
147  * search strategy can just be to scan from the front.
148  */
149  for (i = 0; i < num_res; i++)
150  {
151  if (re_array[i].cre_pat_len == text_re_len &&
152  re_array[i].cre_flags == cflags &&
153  re_array[i].cre_collation == collation &&
154  memcmp(re_array[i].cre_pat, text_re_val, text_re_len) == 0)
155  {
156  /*
157  * Found a match; move it to front if not there already.
158  */
159  if (i > 0)
160  {
161  re_temp = re_array[i];
162  memmove(&re_array[1], &re_array[0], i * sizeof(cached_re_str));
163  re_array[0] = re_temp;
164  }
165 
166  return &re_array[0].cre_re;
167  }
168  }
169 
170  /*
171  * Couldn't find it, so try to compile the new RE. To avoid leaking
172  * resources on failure, we build into the re_temp local.
173  */
174 
175  /* Convert pattern string to wide characters */
176  pattern = (pg_wchar *) palloc((text_re_len + 1) * sizeof(pg_wchar));
177  pattern_len = pg_mb2wchar_with_len(text_re_val,
178  pattern,
179  text_re_len);
180 
181  regcomp_result = pg_regcomp(&re_temp.cre_re,
182  pattern,
183  pattern_len,
184  cflags,
185  collation);
186 
187  pfree(pattern);
188 
189  if (regcomp_result != REG_OKAY)
190  {
191  /* re didn't compile (no need for pg_regfree, if so) */
192 
193  /*
194  * Here and in other places in this file, do CHECK_FOR_INTERRUPTS
195  * before reporting a regex error. This is so that if the regex
196  * library aborts and returns REG_CANCEL, we don't print an error
197  * message that implies the regex was invalid.
198  */
200 
201  pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
202  ereport(ERROR,
203  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
204  errmsg("invalid regular expression: %s", errMsg)));
205  }
206 
207  /*
208  * We use malloc/free for the cre_pat field because the storage has to
209  * persist across transactions, and because we want to get control back on
210  * out-of-memory. The Max() is because some malloc implementations return
211  * NULL for malloc(0).
212  */
213  re_temp.cre_pat = malloc(Max(text_re_len, 1));
214  if (re_temp.cre_pat == NULL)
215  {
216  pg_regfree(&re_temp.cre_re);
217  ereport(ERROR,
218  (errcode(ERRCODE_OUT_OF_MEMORY),
219  errmsg("out of memory")));
220  }
221  memcpy(re_temp.cre_pat, text_re_val, text_re_len);
222  re_temp.cre_pat_len = text_re_len;
223  re_temp.cre_flags = cflags;
224  re_temp.cre_collation = collation;
225 
226  /*
227  * Okay, we have a valid new item in re_temp; insert it into the storage
228  * array. Discard last entry if needed.
229  */
230  if (num_res >= MAX_CACHED_RES)
231  {
232  --num_res;
233  Assert(num_res < MAX_CACHED_RES);
234  pg_regfree(&re_array[num_res].cre_re);
235  free(re_array[num_res].cre_pat);
236  }
237 
238  if (num_res > 0)
239  memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
240 
241  re_array[0] = re_temp;
242  num_res++;
243 
244  return &re_array[0].cre_re;
245 }
246 
247 /*
248  * RE_wchar_execute - execute a RE on pg_wchar data
249  *
250  * Returns TRUE on match, FALSE on no match
251  *
252  * re --- the compiled pattern as returned by RE_compile_and_cache
253  * data --- the data to match against (need not be null-terminated)
254  * data_len --- the length of the data string
255  * start_search -- the offset in the data to start searching
256  * nmatch, pmatch --- optional return area for match details
257  *
258  * Data is given as array of pg_wchar which is what Spencer's regex package
259  * wants.
260  */
261 static bool
262 RE_wchar_execute(regex_t *re, pg_wchar *data, int data_len,
263  int start_search, int nmatch, regmatch_t *pmatch)
264 {
265  int regexec_result;
266  char errMsg[100];
267 
268  /* Perform RE match and return result */
269  regexec_result = pg_regexec(re,
270  data,
271  data_len,
272  start_search,
273  NULL, /* no details */
274  nmatch,
275  pmatch,
276  0);
277 
278  if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
279  {
280  /* re failed??? */
282  pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
283  ereport(ERROR,
284  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
285  errmsg("regular expression failed: %s", errMsg)));
286  }
287 
288  return (regexec_result == REG_OKAY);
289 }
290 
291 /*
292  * RE_execute - execute a RE
293  *
294  * Returns TRUE on match, FALSE on no match
295  *
296  * re --- the compiled pattern as returned by RE_compile_and_cache
297  * dat --- the data to match against (need not be null-terminated)
298  * dat_len --- the length of the data string
299  * nmatch, pmatch --- optional return area for match details
300  *
301  * Data is given in the database encoding. We internally
302  * convert to array of pg_wchar which is what Spencer's regex package wants.
303  */
304 static bool
305 RE_execute(regex_t *re, char *dat, int dat_len,
306  int nmatch, regmatch_t *pmatch)
307 {
308  pg_wchar *data;
309  int data_len;
310  bool match;
311 
312  /* Convert data string to wide characters */
313  data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
314  data_len = pg_mb2wchar_with_len(dat, data, dat_len);
315 
316  /* Perform RE match and return result */
317  match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
318 
319  pfree(data);
320  return match;
321 }
322 
323 /*
324  * RE_compile_and_execute - compile and execute a RE
325  *
326  * Returns TRUE on match, FALSE on no match
327  *
328  * text_re --- the pattern, expressed as a TEXT object
329  * dat --- the data to match against (need not be null-terminated)
330  * dat_len --- the length of the data string
331  * cflags --- compile options for the pattern
332  * collation --- collation to use for LC_CTYPE-dependent behavior
333  * nmatch, pmatch --- optional return area for match details
334  *
335  * Both pattern and data are given in the database encoding. We internally
336  * convert to array of pg_wchar which is what Spencer's regex package wants.
337  */
338 static bool
339 RE_compile_and_execute(text *text_re, char *dat, int dat_len,
340  int cflags, Oid collation,
341  int nmatch, regmatch_t *pmatch)
342 {
343  regex_t *re;
344 
345  /* Compile RE */
346  re = RE_compile_and_cache(text_re, cflags, collation);
347 
348  return RE_execute(re, dat, dat_len, nmatch, pmatch);
349 }
350 
351 
352 /*
353  * parse_re_flags - parse the options argument of regexp_matches and friends
354  *
355  * flags --- output argument, filled with desired options
356  * opts --- TEXT object, or NULL for defaults
357  *
358  * This accepts all the options allowed by any of the callers; callers that
359  * don't want some have to reject them after the fact.
360  */
361 static void
363 {
364  /* regex flavor is always folded into the compile flags */
365  flags->cflags = REG_ADVANCED;
366  flags->glob = false;
367 
368  if (opts)
369  {
370  char *opt_p = VARDATA_ANY(opts);
371  int opt_len = VARSIZE_ANY_EXHDR(opts);
372  int i;
373 
374  for (i = 0; i < opt_len; i++)
375  {
376  switch (opt_p[i])
377  {
378  case 'g':
379  flags->glob = true;
380  break;
381  case 'b': /* BREs (but why???) */
382  flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
383  break;
384  case 'c': /* case sensitive */
385  flags->cflags &= ~REG_ICASE;
386  break;
387  case 'e': /* plain EREs */
388  flags->cflags |= REG_EXTENDED;
389  flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
390  break;
391  case 'i': /* case insensitive */
392  flags->cflags |= REG_ICASE;
393  break;
394  case 'm': /* Perloid synonym for n */
395  case 'n': /* \n affects ^ $ . [^ */
396  flags->cflags |= REG_NEWLINE;
397  break;
398  case 'p': /* ~Perl, \n affects . [^ */
399  flags->cflags |= REG_NLSTOP;
400  flags->cflags &= ~REG_NLANCH;
401  break;
402  case 'q': /* literal string */
403  flags->cflags |= REG_QUOTE;
404  flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
405  break;
406  case 's': /* single line, \n ordinary */
407  flags->cflags &= ~REG_NEWLINE;
408  break;
409  case 't': /* tight syntax */
410  flags->cflags &= ~REG_EXPANDED;
411  break;
412  case 'w': /* weird, \n affects ^ $ only */
413  flags->cflags &= ~REG_NLSTOP;
414  flags->cflags |= REG_NLANCH;
415  break;
416  case 'x': /* expanded syntax */
417  flags->cflags |= REG_EXPANDED;
418  break;
419  default:
420  ereport(ERROR,
421  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
422  errmsg("invalid regexp option: \"%c\"",
423  opt_p[i])));
424  break;
425  }
426  }
427  }
428 }
429 
430 
431 /*
432  * interface routines called by the function manager
433  */
434 
435 Datum
437 {
438  Name n = PG_GETARG_NAME(0);
439  text *p = PG_GETARG_TEXT_PP(1);
440 
442  NameStr(*n),
443  strlen(NameStr(*n)),
444  REG_ADVANCED,
446  0, NULL));
447 }
448 
449 Datum
451 {
452  Name n = PG_GETARG_NAME(0);
453  text *p = PG_GETARG_TEXT_PP(1);
454 
456  NameStr(*n),
457  strlen(NameStr(*n)),
458  REG_ADVANCED,
460  0, NULL));
461 }
462 
463 Datum
465 {
466  text *s = PG_GETARG_TEXT_PP(0);
467  text *p = PG_GETARG_TEXT_PP(1);
468 
470  VARDATA_ANY(s),
472  REG_ADVANCED,
474  0, NULL));
475 }
476 
477 Datum
479 {
480  text *s = PG_GETARG_TEXT_PP(0);
481  text *p = PG_GETARG_TEXT_PP(1);
482 
484  VARDATA_ANY(s),
486  REG_ADVANCED,
488  0, NULL));
489 }
490 
491 
492 /*
493  * routines that use the regexp stuff, but ignore the case.
494  * for this, we use the REG_ICASE flag to pg_regcomp
495  */
496 
497 
498 Datum
500 {
501  Name n = PG_GETARG_NAME(0);
502  text *p = PG_GETARG_TEXT_PP(1);
503 
505  NameStr(*n),
506  strlen(NameStr(*n)),
509  0, NULL));
510 }
511 
512 Datum
514 {
515  Name n = PG_GETARG_NAME(0);
516  text *p = PG_GETARG_TEXT_PP(1);
517 
519  NameStr(*n),
520  strlen(NameStr(*n)),
523  0, NULL));
524 }
525 
526 Datum
528 {
529  text *s = PG_GETARG_TEXT_PP(0);
530  text *p = PG_GETARG_TEXT_PP(1);
531 
533  VARDATA_ANY(s),
537  0, NULL));
538 }
539 
540 Datum
542 {
543  text *s = PG_GETARG_TEXT_PP(0);
544  text *p = PG_GETARG_TEXT_PP(1);
545 
547  VARDATA_ANY(s),
551  0, NULL));
552 }
553 
554 
555 /*
556  * textregexsubstr()
557  * Return a substring matched by a regular expression.
558  */
559 Datum
561 {
562  text *s = PG_GETARG_TEXT_PP(0);
563  text *p = PG_GETARG_TEXT_PP(1);
564  regex_t *re;
565  regmatch_t pmatch[2];
566  int so,
567  eo;
568 
569  /* Compile RE */
571 
572  /*
573  * We pass two regmatch_t structs to get info about the overall match and
574  * the match for the first parenthesized subexpression (if any). If there
575  * is a parenthesized subexpression, we return what it matched; else
576  * return what the whole regexp matched.
577  */
578  if (!RE_execute(re,
580  2, pmatch))
581  PG_RETURN_NULL(); /* definitely no match */
582 
583  if (re->re_nsub > 0)
584  {
585  /* has parenthesized subexpressions, use the first one */
586  so = pmatch[1].rm_so;
587  eo = pmatch[1].rm_eo;
588  }
589  else
590  {
591  /* no parenthesized subexpression, use whole match */
592  so = pmatch[0].rm_so;
593  eo = pmatch[0].rm_eo;
594  }
595 
596  /*
597  * It is possible to have a match to the whole pattern but no match for a
598  * subexpression; for example 'foo(bar)?' is considered to match 'foo' but
599  * there is no subexpression match. So this extra test for match failure
600  * is not redundant.
601  */
602  if (so < 0 || eo < 0)
603  PG_RETURN_NULL();
604 
606  PointerGetDatum(s),
607  Int32GetDatum(so + 1),
608  Int32GetDatum(eo - so));
609 }
610 
611 /*
612  * textregexreplace_noopt()
613  * Return a string matched by a regular expression, with replacement.
614  *
615  * This version doesn't have an option argument: we default to case
616  * sensitive match, replace the first instance only.
617  */
618 Datum
620 {
621  text *s = PG_GETARG_TEXT_PP(0);
622  text *p = PG_GETARG_TEXT_PP(1);
623  text *r = PG_GETARG_TEXT_PP(2);
624  regex_t *re;
625 
627 
628  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, false));
629 }
630 
631 /*
632  * textregexreplace()
633  * Return a string matched by a regular expression, with replacement.
634  */
635 Datum
637 {
638  text *s = PG_GETARG_TEXT_PP(0);
639  text *p = PG_GETARG_TEXT_PP(1);
640  text *r = PG_GETARG_TEXT_PP(2);
641  text *opt = PG_GETARG_TEXT_PP(3);
642  regex_t *re;
643  pg_re_flags flags;
644 
645  parse_re_flags(&flags, opt);
646 
647  re = RE_compile_and_cache(p, flags.cflags, PG_GET_COLLATION());
648 
649  PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, flags.glob));
650 }
651 
652 /*
653  * similar_escape()
654  * Convert a SQL:2008 regexp pattern to POSIX style, so it can be used by
655  * our regexp engine.
656  */
657 Datum
659 {
660  text *pat_text;
661  text *esc_text;
662  text *result;
663  char *p,
664  *e,
665  *r;
666  int plen,
667  elen;
668  bool afterescape = false;
669  bool incharclass = false;
670  int nquotes = 0;
671 
672  /* This function is not strict, so must test explicitly */
673  if (PG_ARGISNULL(0))
674  PG_RETURN_NULL();
675  pat_text = PG_GETARG_TEXT_PP(0);
676  p = VARDATA_ANY(pat_text);
677  plen = VARSIZE_ANY_EXHDR(pat_text);
678  if (PG_ARGISNULL(1))
679  {
680  /* No ESCAPE clause provided; default to backslash as escape */
681  e = "\\";
682  elen = 1;
683  }
684  else
685  {
686  esc_text = PG_GETARG_TEXT_PP(1);
687  e = VARDATA_ANY(esc_text);
688  elen = VARSIZE_ANY_EXHDR(esc_text);
689  if (elen == 0)
690  e = NULL; /* no escape character */
691  else
692  {
693  int escape_mblen = pg_mbstrlen_with_len(e, elen);
694 
695  if (escape_mblen > 1)
696  ereport(ERROR,
697  (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
698  errmsg("invalid escape string"),
699  errhint("Escape string must be empty or one character.")));
700  }
701  }
702 
703  /*----------
704  * We surround the transformed input string with
705  * ^(?: ... )$
706  * which requires some explanation. We need "^" and "$" to force
707  * the pattern to match the entire input string as per SQL99 spec.
708  * The "(?:" and ")" are a non-capturing set of parens; we have to have
709  * parens in case the string contains "|", else the "^" and "$" will
710  * be bound into the first and last alternatives which is not what we
711  * want, and the parens must be non capturing because we don't want them
712  * to count when selecting output for SUBSTRING.
713  *----------
714  */
715 
716  /*
717  * We need room for the prefix/postfix plus as many as 3 output bytes per
718  * input byte; since the input is at most 1GB this can't overflow
719  */
720  result = (text *) palloc(VARHDRSZ + 6 + 3 * plen);
721  r = VARDATA(result);
722 
723  *r++ = '^';
724  *r++ = '(';
725  *r++ = '?';
726  *r++ = ':';
727 
728  while (plen > 0)
729  {
730  char pchar = *p;
731 
732  /*
733  * If both the escape character and the current character from the
734  * pattern are multi-byte, we need to take the slow path.
735  *
736  * But if one of them is single-byte, we can process the pattern one
737  * byte at a time, ignoring multi-byte characters. (This works
738  * because all server-encodings have the property that a valid
739  * multi-byte character representation cannot contain the
740  * representation of a valid single-byte character.)
741  */
742 
743  if (elen > 1)
744  {
745  int mblen = pg_mblen(p);
746 
747  if (mblen > 1)
748  {
749  /* slow, multi-byte path */
750  if (afterescape)
751  {
752  *r++ = '\\';
753  memcpy(r, p, mblen);
754  r += mblen;
755  afterescape = false;
756  }
757  else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
758  {
759  /* SQL99 escape character; do not send to output */
760  afterescape = true;
761  }
762  else
763  {
764  /*
765  * We know it's a multi-byte character, so we don't need
766  * to do all the comparisons to single-byte characters
767  * that we do below.
768  */
769  memcpy(r, p, mblen);
770  r += mblen;
771  }
772 
773  p += mblen;
774  plen -= mblen;
775 
776  continue;
777  }
778  }
779 
780  /* fast path */
781  if (afterescape)
782  {
783  if (pchar == '"' && !incharclass) /* for SUBSTRING patterns */
784  *r++ = ((nquotes++ % 2) == 0) ? '(' : ')';
785  else
786  {
787  *r++ = '\\';
788  *r++ = pchar;
789  }
790  afterescape = false;
791  }
792  else if (e && pchar == *e)
793  {
794  /* SQL99 escape character; do not send to output */
795  afterescape = true;
796  }
797  else if (incharclass)
798  {
799  if (pchar == '\\')
800  *r++ = '\\';
801  *r++ = pchar;
802  if (pchar == ']')
803  incharclass = false;
804  }
805  else if (pchar == '[')
806  {
807  *r++ = pchar;
808  incharclass = true;
809  }
810  else if (pchar == '%')
811  {
812  *r++ = '.';
813  *r++ = '*';
814  }
815  else if (pchar == '_')
816  *r++ = '.';
817  else if (pchar == '(')
818  {
819  /* convert to non-capturing parenthesis */
820  *r++ = '(';
821  *r++ = '?';
822  *r++ = ':';
823  }
824  else if (pchar == '\\' || pchar == '.' ||
825  pchar == '^' || pchar == '$')
826  {
827  *r++ = '\\';
828  *r++ = pchar;
829  }
830  else
831  *r++ = pchar;
832  p++, plen--;
833  }
834 
835  *r++ = ')';
836  *r++ = '$';
837 
838  SET_VARSIZE(result, r - ((char *) result));
839 
840  PG_RETURN_TEXT_P(result);
841 }
842 
843 /*
844  * regexp_matches()
845  * Return a table of matches of a pattern within a string.
846  */
847 Datum
849 {
850  FuncCallContext *funcctx;
851  regexp_matches_ctx *matchctx;
852 
853  if (SRF_IS_FIRSTCALL())
854  {
855  text *pattern = PG_GETARG_TEXT_PP(1);
856  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
857  MemoryContext oldcontext;
858 
859  funcctx = SRF_FIRSTCALL_INIT();
860  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
861 
862  /* be sure to copy the input string into the multi-call ctx */
863  matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
864  flags,
866  false, true, false);
867 
868  /* Pre-create workspace that build_regexp_matches_result needs */
869  matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
870  matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
871 
872  MemoryContextSwitchTo(oldcontext);
873  funcctx->user_fctx = (void *) matchctx;
874  }
875 
876  funcctx = SRF_PERCALL_SETUP();
877  matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
878 
879  if (matchctx->next_match < matchctx->nmatches)
880  {
881  ArrayType *result_ary;
882 
883  result_ary = build_regexp_matches_result(matchctx);
884  matchctx->next_match++;
885  SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
886  }
887 
888  /* release space in multi-call ctx to avoid intraquery memory leak */
889  cleanup_regexp_matches(matchctx);
890 
891  SRF_RETURN_DONE(funcctx);
892 }
893 
894 /* This is separate to keep the opr_sanity regression test from complaining */
895 Datum
897 {
898  return regexp_matches(fcinfo);
899 }
900 
901 /*
902  * setup_regexp_matches --- do the initial matching for regexp_matches()
903  * or regexp_split()
904  *
905  * To avoid having to re-find the compiled pattern on each call, we do
906  * all the matching in one swoop. The returned regexp_matches_ctx contains
907  * the locations of all the substrings matching the pattern.
908  *
909  * The three bool parameters have only two patterns (one for each caller)
910  * but it seems clearer to distinguish the functionality this way than to
911  * key it all off one "is_split" flag.
912  */
913 static regexp_matches_ctx *
914 setup_regexp_matches(text *orig_str, text *pattern, text *flags,
915  Oid collation,
916  bool force_glob, bool use_subpatterns,
917  bool ignore_degenerate)
918 {
919  regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
920  int orig_len;
921  pg_wchar *wide_str;
922  int wide_len;
923  pg_re_flags re_flags;
924  regex_t *cpattern;
925  regmatch_t *pmatch;
926  int pmatch_len;
927  int array_len;
928  int array_idx;
929  int prev_match_end;
930  int start_search;
931 
932  /* save original string --- we'll extract result substrings from it */
933  matchctx->orig_str = orig_str;
934 
935  /* convert string to pg_wchar form for matching */
936  orig_len = VARSIZE_ANY_EXHDR(orig_str);
937  wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
938  wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
939 
940  /* determine options */
941  parse_re_flags(&re_flags, flags);
942  if (force_glob)
943  {
944  /* user mustn't specify 'g' for regexp_split */
945  if (re_flags.glob)
946  ereport(ERROR,
947  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
948  errmsg("regexp_split does not support the global option")));
949  /* but we find all the matches anyway */
950  re_flags.glob = true;
951  }
952 
953  /* set up the compiled pattern */
954  cpattern = RE_compile_and_cache(pattern, re_flags.cflags, collation);
955 
956  /* do we want to remember subpatterns? */
957  if (use_subpatterns && cpattern->re_nsub > 0)
958  {
959  matchctx->npatterns = cpattern->re_nsub;
960  pmatch_len = cpattern->re_nsub + 1;
961  }
962  else
963  {
964  use_subpatterns = false;
965  matchctx->npatterns = 1;
966  pmatch_len = 1;
967  }
968 
969  /* temporary output space for RE package */
970  pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
971 
972  /* the real output space (grown dynamically if needed) */
973  array_len = re_flags.glob ? 256 : 32;
974  matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
975  array_idx = 0;
976 
977  /* search for the pattern, perhaps repeatedly */
978  prev_match_end = 0;
979  start_search = 0;
980  while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
981  pmatch_len, pmatch))
982  {
983  /*
984  * If requested, ignore degenerate matches, which are zero-length
985  * matches occurring at the start or end of a string or just after a
986  * previous match.
987  */
988  if (!ignore_degenerate ||
989  (pmatch[0].rm_so < wide_len &&
990  pmatch[0].rm_eo > prev_match_end))
991  {
992  /* enlarge output space if needed */
993  while (array_idx + matchctx->npatterns * 2 > array_len)
994  {
995  array_len *= 2;
996  matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
997  sizeof(int) * array_len);
998  }
999 
1000  /* save this match's locations */
1001  if (use_subpatterns)
1002  {
1003  int i;
1004 
1005  for (i = 1; i <= matchctx->npatterns; i++)
1006  {
1007  matchctx->match_locs[array_idx++] = pmatch[i].rm_so;
1008  matchctx->match_locs[array_idx++] = pmatch[i].rm_eo;
1009  }
1010  }
1011  else
1012  {
1013  matchctx->match_locs[array_idx++] = pmatch[0].rm_so;
1014  matchctx->match_locs[array_idx++] = pmatch[0].rm_eo;
1015  }
1016  matchctx->nmatches++;
1017  }
1018  prev_match_end = pmatch[0].rm_eo;
1019 
1020  /* if not glob, stop after one match */
1021  if (!re_flags.glob)
1022  break;
1023 
1024  /*
1025  * Advance search position. Normally we start the next search at the
1026  * end of the previous match; but if the match was of zero length, we
1027  * have to advance by one character, or we'd just find the same match
1028  * again.
1029  */
1030  start_search = prev_match_end;
1031  if (pmatch[0].rm_so == pmatch[0].rm_eo)
1032  start_search++;
1033  if (start_search > wide_len)
1034  break;
1035  }
1036 
1037  /* Clean up temp storage */
1038  pfree(wide_str);
1039  pfree(pmatch);
1040 
1041  return matchctx;
1042 }
1043 
1044 /*
1045  * cleanup_regexp_matches - release memory of a regexp_matches_ctx
1046  */
1047 static void
1049 {
1050  pfree(matchctx->orig_str);
1051  pfree(matchctx->match_locs);
1052  if (matchctx->elems)
1053  pfree(matchctx->elems);
1054  if (matchctx->nulls)
1055  pfree(matchctx->nulls);
1056  pfree(matchctx);
1057 }
1058 
1059 /*
1060  * build_regexp_matches_result - build output array for current match
1061  */
1062 static ArrayType *
1064 {
1065  Datum *elems = matchctx->elems;
1066  bool *nulls = matchctx->nulls;
1067  int dims[1];
1068  int lbs[1];
1069  int loc;
1070  int i;
1071 
1072  /* Extract matching substrings from the original string */
1073  loc = matchctx->next_match * matchctx->npatterns * 2;
1074  for (i = 0; i < matchctx->npatterns; i++)
1075  {
1076  int so = matchctx->match_locs[loc++];
1077  int eo = matchctx->match_locs[loc++];
1078 
1079  if (so < 0 || eo < 0)
1080  {
1081  elems[i] = (Datum) 0;
1082  nulls[i] = true;
1083  }
1084  else
1085  {
1087  PointerGetDatum(matchctx->orig_str),
1088  Int32GetDatum(so + 1),
1089  Int32GetDatum(eo - so));
1090  nulls[i] = false;
1091  }
1092  }
1093 
1094  /* And form an array */
1095  dims[0] = matchctx->npatterns;
1096  lbs[0] = 1;
1097  /* XXX: this hardcodes assumptions about the text type */
1098  return construct_md_array(elems, nulls, 1, dims, lbs,
1099  TEXTOID, -1, false, 'i');
1100 }
1101 
1102 /*
1103  * regexp_split_to_table()
1104  * Split the string at matches of the pattern, returning the
1105  * split-out substrings as a table.
1106  */
1107 Datum
1109 {
1110  FuncCallContext *funcctx;
1111  regexp_matches_ctx *splitctx;
1112 
1113  if (SRF_IS_FIRSTCALL())
1114  {
1115  text *pattern = PG_GETARG_TEXT_PP(1);
1116  text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1117  MemoryContext oldcontext;
1118 
1119  funcctx = SRF_FIRSTCALL_INIT();
1120  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1121 
1122  /* be sure to copy the input string into the multi-call ctx */
1123  splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1124  flags,
1125  PG_GET_COLLATION(),
1126  true, false, true);
1127 
1128  MemoryContextSwitchTo(oldcontext);
1129  funcctx->user_fctx = (void *) splitctx;
1130  }
1131 
1132  funcctx = SRF_PERCALL_SETUP();
1133  splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
1134 
1135  if (splitctx->next_match <= splitctx->nmatches)
1136  {
1137  Datum result = build_regexp_split_result(splitctx);
1138 
1139  splitctx->next_match++;
1140  SRF_RETURN_NEXT(funcctx, result);
1141  }
1142 
1143  /* release space in multi-call ctx to avoid intraquery memory leak */
1144  cleanup_regexp_matches(splitctx);
1145 
1146  SRF_RETURN_DONE(funcctx);
1147 }
1148 
1149 /* This is separate to keep the opr_sanity regression test from complaining */
1150 Datum
1152 {
1153  return regexp_split_to_table(fcinfo);
1154 }
1155 
1156 /*
1157  * regexp_split_to_array()
1158  * Split the string at matches of the pattern, returning the
1159  * split-out substrings as an array.
1160  */
1161 Datum
1163 {
1164  ArrayBuildState *astate = NULL;
1165  regexp_matches_ctx *splitctx;
1166 
1168  PG_GETARG_TEXT_PP(1),
1170  PG_GET_COLLATION(),
1171  true, false, true);
1172 
1173  while (splitctx->next_match <= splitctx->nmatches)
1174  {
1175  astate = accumArrayResult(astate,
1176  build_regexp_split_result(splitctx),
1177  false,
1178  TEXTOID,
1180  splitctx->next_match++;
1181  }
1182 
1183  /*
1184  * We don't call cleanup_regexp_matches here; it would try to pfree the
1185  * input string, which we didn't copy. The space is not in a long-lived
1186  * memory context anyway.
1187  */
1188 
1190 }
1191 
1192 /* This is separate to keep the opr_sanity regression test from complaining */
1193 Datum
1195 {
1196  return regexp_split_to_array(fcinfo);
1197 }
1198 
1199 /*
1200  * build_regexp_split_result - build output string for current match
1201  *
1202  * We return the string between the current match and the previous one,
1203  * or the string after the last match when next_match == nmatches.
1204  */
1205 static Datum
1207 {
1208  int startpos;
1209  int endpos;
1210 
1211  if (splitctx->next_match > 0)
1212  startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
1213  else
1214  startpos = 0;
1215  if (startpos < 0)
1216  elog(ERROR, "invalid match ending position");
1217 
1218  if (splitctx->next_match < splitctx->nmatches)
1219  {
1220  endpos = splitctx->match_locs[splitctx->next_match * 2];
1221  if (endpos < startpos)
1222  elog(ERROR, "invalid match starting position");
1224  PointerGetDatum(splitctx->orig_str),
1225  Int32GetDatum(startpos + 1),
1226  Int32GetDatum(endpos - startpos));
1227  }
1228  else
1229  {
1230  /* no more matches, return rest of string */
1232  PointerGetDatum(splitctx->orig_str),
1233  Int32GetDatum(startpos + 1));
1234  }
1235 }
1236 
1237 /*
1238  * regexp_fixed_prefix - extract fixed prefix, if any, for a regexp
1239  *
1240  * The result is NULL if there is no fixed prefix, else a palloc'd string.
1241  * If it is an exact match, not just a prefix, *exact is returned as TRUE.
1242  */
1243 char *
1244 regexp_fixed_prefix(text *text_re, bool case_insensitive, Oid collation,
1245  bool *exact)
1246 {
1247  char *result;
1248  regex_t *re;
1249  int cflags;
1250  int re_result;
1251  pg_wchar *str;
1252  size_t slen;
1253  size_t maxlen;
1254  char errMsg[100];
1255 
1256  *exact = false; /* default result */
1257 
1258  /* Compile RE */
1259  cflags = REG_ADVANCED;
1260  if (case_insensitive)
1261  cflags |= REG_ICASE;
1262 
1263  re = RE_compile_and_cache(text_re, cflags, collation);
1264 
1265  /* Examine it to see if there's a fixed prefix */
1266  re_result = pg_regprefix(re, &str, &slen);
1267 
1268  switch (re_result)
1269  {
1270  case REG_NOMATCH:
1271  return NULL;
1272 
1273  case REG_PREFIX:
1274  /* continue with wchar conversion */
1275  break;
1276 
1277  case REG_EXACT:
1278  *exact = true;
1279  /* continue with wchar conversion */
1280  break;
1281 
1282  default:
1283  /* re failed??? */
1285  pg_regerror(re_result, re, errMsg, sizeof(errMsg));
1286  ereport(ERROR,
1287  (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
1288  errmsg("regular expression failed: %s", errMsg)));
1289  break;
1290  }
1291 
1292  /* Convert pg_wchar result back to database encoding */
1293  maxlen = pg_database_encoding_max_length() * slen + 1;
1294  result = (char *) palloc(maxlen);
1295  slen = pg_wchar2mb_with_len(str, result, slen);
1296  Assert(slen < maxlen);
1297 
1298  free(str);
1299 
1300  return result;
1301 }
static int num_res
Definition: regexp.c:104
static bool RE_execute(regex_t *re, char *dat, int dat_len, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:305
text * replace_text_regexp(text *src_text, void *regexp, text *replace_text, bool glob)
Definition: varlena.c:3533
#define REG_NLSTOP
Definition: regex.h:109
Datum textregexsubstr(PG_FUNCTION_ARGS)
Definition: regexp.c:560
int errhint(const char *fmt,...)
Definition: elog.c:982
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define VARDATA(PTR)
Definition: postgres.h:305
Datum regexp_matches(PG_FUNCTION_ARGS)
Definition: regexp.c:848
regex_t cre_re
Definition: regexp.c:101
Datum regexp_matches_no_flags(PG_FUNCTION_ARGS)
Definition: regexp.c:896
Datum nameregexeq(PG_FUNCTION_ARGS)
Definition: regexp.c:436
Datum * elems
Definition: regexp.c:61
#define TEXTOID
Definition: pg_type.h:324
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
#define VARHDRSZ
Definition: c.h:429
#define REG_QUOTE
Definition: regex.h:104
int cflags
Definition: regexp.c:46
regoff_t rm_so
Definition: regex.h:85
Datum texticregexne(PG_FUNCTION_ARGS)
Definition: regexp.c:541
bool glob
Definition: regexp.c:47
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int cre_flags
Definition: regexp.c:99
Datum regexp_split_to_table_no_flags(PG_FUNCTION_ARGS)
Definition: regexp.c:1151
int errcode(int sqlerrcode)
Definition: elog.c:573
bool * nulls
Definition: regexp.c:62
Datum textregexeq(PG_FUNCTION_ARGS)
Definition: regexp.c:464
Datum regexp_split_to_array(PG_FUNCTION_ARGS)
Definition: regexp.c:1162
int * match_locs
Definition: regexp.c:58
#define MAX_CACHED_RES
Definition: regexp.c:91
#define REG_PREFIX
Definition: regex.h:162
struct cached_re_str cached_re_str
unsigned int Oid
Definition: postgres_ext.h:31
#define REG_ICASE
Definition: regex.h:106
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
int pg_regcomp(regex_t *re, const chr *string, size_t len, int flags, Oid collation)
Definition: regcomp.c:294
#define PG_GET_COLLATION()
Definition: fmgr.h:155
char * cre_pat
Definition: regexp.c:97
int pg_regprefix(regex_t *re, chr **string, size_t *slength)
Definition: regprefix.c:46
#define PG_GETARG_TEXT_P_COPY(n)
Definition: fmgr.h:278
regoff_t rm_eo
Definition: regex.h:86
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define malloc(a)
Definition: header.h:45
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:805
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_TEXT_PP_IF_EXISTS(_n)
Definition: regexp.c:39
size_t re_nsub
Definition: regex.h:58
void pfree(void *pointer)
Definition: mcxt.c:993
#define REG_OKAY
Definition: regex.h:137
static cached_re_str re_array[MAX_CACHED_RES]
Definition: regexp.c:105
#define ERROR
Definition: elog.h:41
Datum texticregexeq(PG_FUNCTION_ARGS)
Definition: regexp.c:527
struct regexp_matches_ctx regexp_matches_ctx
Definition: c.h:477
#define memmove(d, s, c)
Definition: c.h:1027
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
text * orig_str
Definition: regexp.c:53
static bool RE_compile_and_execute(text *text_re, char *dat, int dat_len, int cflags, Oid collation, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:339
Datum textregexreplace_noopt(PG_FUNCTION_ARGS)
Definition: regexp.c:619
Datum nameicregexne(PG_FUNCTION_ARGS)
Definition: regexp.c:513
Datum similar_escape(PG_FUNCTION_ARGS)
Definition: regexp.c:658
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:60
#define REG_NEWLINE
Definition: regex.h:111
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
Datum text_substr_no_len(PG_FUNCTION_ARGS)
Definition: varlena.c:786
#define REG_ADVANCED
Definition: regex.h:103
static void cleanup_regexp_matches(regexp_matches_ctx *matchctx)
Definition: regexp.c:1048
Datum regexp_split_to_table(PG_FUNCTION_ARGS)
Definition: regexp.c:1108
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
Datum textregexne(PG_FUNCTION_ARGS)
Definition: regexp.c:478
#define ereport(elevel, rest)
Definition: elog.h:132
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5028
unsigned int pg_wchar
Definition: mbprint.c:30
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:552
Datum nameicregexeq(PG_FUNCTION_ARGS)
Definition: regexp.c:499
static regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition: regexp.c:133
#define REG_EXTENDED
Definition: regex.h:101
Oid cre_collation
Definition: regexp.c:100
Datum text_substr(PG_FUNCTION_ARGS)
Definition: varlena.c:772
void * palloc0(Size size)
Definition: mcxt.c:921
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
static regexp_matches_ctx * setup_regexp_matches(text *orig_str, text *pattern, text *flags, Oid collation, bool force_glob, bool use_subpatterns, bool ignore_degenerate)
Definition: regexp.c:914
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:734
#define free(a)
Definition: header.h:60
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
#define Max(x, y)
Definition: c.h:781
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:215
#define Assert(condition)
Definition: c.h:656
static ArrayType * build_regexp_matches_result(regexp_matches_ctx *matchctx)
Definition: regexp.c:1063
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define REG_NLANCH
Definition: regex.h:110
static XLogRecPtr startpos
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1022
Datum regexp_split_to_array_no_flags(PG_FUNCTION_ARGS)
Definition: regexp.c:1194
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4964
Datum nameregexne(PG_FUNCTION_ARGS)
Definition: regexp.c:450
#define Int32GetDatum(X)
Definition: postgres.h:487
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx)
Definition: regexp.c:1206
e
Definition: preproc-init.c:82
void * user_fctx
Definition: funcapi.h:90
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:167
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
#define REG_EXACT
Definition: regex.h:163
void * palloc(Size size)
Definition: mcxt.c:892
char * regexp_fixed_prefix(text *text_re, bool case_insensitive, Oid collation, bool *exact)
Definition: regexp.c:1244
int errmsg(const char *fmt,...)
Definition: elog.c:795
static void parse_re_flags(pg_re_flags *flags, text *opts)
Definition: regexp.c:362
static bool RE_wchar_execute(regex_t *re, pg_wchar *data, int data_len, int start_search, int nmatch, regmatch_t *pmatch)
Definition: regexp.c:262
int i
#define REG_EXPANDED
Definition: regex.h:108
#define REG_NOMATCH
Definition: regex.h:138
#define NameStr(name)
Definition: c.h:483
Definition: c.h:423
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:96
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define elog
Definition: elog.h:228
Datum textregexreplace(PG_FUNCTION_ARGS)
Definition: regexp.c:636
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:756
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:3311
void pg_regfree(regex_t *re)
Definition: regfree.c:49
int cre_pat_len
Definition: regexp.c:98
Definition: regex.h:55
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:550
#define PG_RETURN_NULL()
Definition: fmgr.h:289
struct pg_re_flags pg_re_flags
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287