1 /*-------------------------------------------------------------------------
4 * Postgres' interface to the regular expression package.
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/regexp.c
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
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...
28 *-------------------------------------------------------------------------
32 #include "catalog/pg_type.h"
34 #include "miscadmin.h"
35 #include "regex/regex.h"
36 #include "utils/array.h"
37 #include "utils/builtins.h"
38 #include "utils/varlena.h"
40 #define PG_GETARG_TEXT_PP_IF_EXISTS(_n) \
41 (PG_NARGS() > (_n) ? PG_GETARG_TEXT_PP(_n) : NULL)
44 /* all the options of interest for regex functions */
45 typedef struct pg_re_flags
47 int cflags; /* compile flags for Spencer's regex code */
48 bool glob; /* do it globally (for each occurrence) */
51 /* cross-call state for regexp_match and regexp_split functions */
52 typedef struct regexp_matches_ctx
54 text *orig_str; /* data string in original TEXT form */
55 int nmatches; /* number of places where pattern matched */
56 int npatterns; /* number of capturing subpatterns */
57 /* We store start char index and end+1 char index for each match */
58 /* so the number of entries in match_locs is nmatches * npatterns * 2 */
59 int *match_locs; /* 0-based character indexes */
60 int next_match; /* 0-based index of next match to process */
61 /* workspace for build_regexp_match_result() */
62 Datum *elems; /* has npatterns elements */
63 bool *nulls; /* has npatterns elements */
67 * We cache precompiled regular expressions using a "self organizing list"
68 * structure, in which recently-used items tend to be near the front.
69 * Whenever we use an entry, it's moved up to the front of the list.
70 * Over time, an item's average position corresponds to its frequency of use.
72 * When we first create an entry, it's inserted at the front of
73 * the array, dropping the entry at the end of the array if necessary to
74 * make room. (This might seem to be weighting the new entry too heavily,
75 * but if we insert new entries further back, we'll be unable to adjust to
76 * a sudden shift in the query mix where we are presented with MAX_CACHED_RES
77 * never-before-seen items used circularly. We ought to be able to handle
78 * that case, so we have to insert at the front.)
80 * Knuth mentions a variant strategy in which a used item is moved up just
81 * one place in the list. Although he says this uses fewer comparisons on
82 * average, it seems not to adapt very well to the situation where you have
83 * both some reusable patterns and a steady stream of non-reusable patterns.
84 * A reusable pattern that isn't used at least as often as non-reusable
85 * patterns are seen will "fail to keep up" and will drop off the end of the
86 * cache. With move-to-front, a reusable pattern is guaranteed to stay in
87 * the cache as long as it's used at least once in every MAX_CACHED_RES uses.
90 /* this is the maximum number of cached regular expressions */
91 #ifndef MAX_CACHED_RES
92 #define MAX_CACHED_RES 32
95 /* this structure describes one cached regular expression */
96 typedef struct cached_re_str
98 char *cre_pat; /* original RE (not null terminated!) */
99 int cre_pat_len; /* length of original RE, in bytes */
100 int cre_flags; /* compile flags: extended,icase etc */
101 Oid cre_collation; /* collation to use */
102 regex_t cre_re; /* the compiled regular expression */
105 static int num_res = 0; /* # of cached re's */
106 static cached_re_str re_array[MAX_CACHED_RES]; /* cached re's */
109 /* Local functions */
110 static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern,
113 bool use_subpatterns,
114 bool ignore_degenerate);
115 static void cleanup_regexp_matches(regexp_matches_ctx *matchctx);
116 static ArrayType *build_regexp_match_result(regexp_matches_ctx *matchctx);
117 static Datum build_regexp_split_result(regexp_matches_ctx *splitctx);
121 * RE_compile_and_cache - compile a RE, caching if possible
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
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.
133 RE_compile_and_cache(text *text_re, int cflags, Oid collation)
135 int text_re_len = VARSIZE_ANY_EXHDR(text_re);
136 char *text_re_val = VARDATA_ANY(text_re);
141 cached_re_str re_temp;
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.
149 for (i = 0; i < num_res; i++)
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)
157 * Found a match; move it to front if not there already.
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;
166 return &re_array[0].cre_re;
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.
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,
181 regcomp_result = pg_regcomp(&re_temp.cre_re,
189 if (regcomp_result != REG_OKAY)
191 /* re didn't compile (no need for pg_regfree, if so) */
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.
199 CHECK_FOR_INTERRUPTS();
201 pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
203 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
204 errmsg("invalid regular expression: %s", errMsg)));
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).
213 re_temp.cre_pat = malloc(Max(text_re_len, 1));
214 if (re_temp.cre_pat == NULL)
216 pg_regfree(&re_temp.cre_re);
218 (errcode(ERRCODE_OUT_OF_MEMORY),
219 errmsg("out of memory")));
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;
227 * Okay, we have a valid new item in re_temp; insert it into the storage
228 * array. Discard last entry if needed.
230 if (num_res >= MAX_CACHED_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);
239 memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
241 re_array[0] = re_temp;
244 return &re_array[0].cre_re;
248 * RE_wchar_execute - execute a RE on pg_wchar data
250 * Returns TRUE on match, FALSE on no match
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
258 * Data is given as array of pg_wchar which is what Spencer's regex package
262 RE_wchar_execute(regex_t *re, pg_wchar *data, int data_len,
263 int start_search, int nmatch, regmatch_t *pmatch)
268 /* Perform RE match and return result */
269 regexec_result = pg_regexec(re,
273 NULL, /* no details */
278 if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
281 CHECK_FOR_INTERRUPTS();
282 pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
284 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
285 errmsg("regular expression failed: %s", errMsg)));
288 return (regexec_result == REG_OKAY);
292 * RE_execute - execute a RE
294 * Returns TRUE on match, FALSE on no match
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
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.
305 RE_execute(regex_t *re, char *dat, int dat_len,
306 int nmatch, regmatch_t *pmatch)
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);
316 /* Perform RE match and return result */
317 match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
324 * RE_compile_and_execute - compile and execute a RE
326 * Returns TRUE on match, FALSE on no match
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
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.
339 RE_compile_and_execute(text *text_re, char *dat, int dat_len,
340 int cflags, Oid collation,
341 int nmatch, regmatch_t *pmatch)
346 re = RE_compile_and_cache(text_re, cflags, collation);
348 return RE_execute(re, dat, dat_len, nmatch, pmatch);
353 * parse_re_flags - parse the options argument of regexp_match and friends
355 * flags --- output argument, filled with desired options
356 * opts --- TEXT object, or NULL for defaults
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.
362 parse_re_flags(pg_re_flags *flags, text *opts)
364 /* regex flavor is always folded into the compile flags */
365 flags->cflags = REG_ADVANCED;
370 char *opt_p = VARDATA_ANY(opts);
371 int opt_len = VARSIZE_ANY_EXHDR(opts);
374 for (i = 0; i < opt_len; i++)
381 case 'b': /* BREs (but why???) */
382 flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
384 case 'c': /* case sensitive */
385 flags->cflags &= ~REG_ICASE;
387 case 'e': /* plain EREs */
388 flags->cflags |= REG_EXTENDED;
389 flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
391 case 'i': /* case insensitive */
392 flags->cflags |= REG_ICASE;
394 case 'm': /* Perloid synonym for n */
395 case 'n': /* \n affects ^ $ . [^ */
396 flags->cflags |= REG_NEWLINE;
398 case 'p': /* ~Perl, \n affects . [^ */
399 flags->cflags |= REG_NLSTOP;
400 flags->cflags &= ~REG_NLANCH;
402 case 'q': /* literal string */
403 flags->cflags |= REG_QUOTE;
404 flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
406 case 's': /* single line, \n ordinary */
407 flags->cflags &= ~REG_NEWLINE;
409 case 't': /* tight syntax */
410 flags->cflags &= ~REG_EXPANDED;
412 case 'w': /* weird, \n affects ^ $ only */
413 flags->cflags &= ~REG_NLSTOP;
414 flags->cflags |= REG_NLANCH;
416 case 'x': /* expanded syntax */
417 flags->cflags |= REG_EXPANDED;
421 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
422 errmsg("invalid regexp option: \"%c\"",
432 * interface routines called by the function manager
436 nameregexeq(PG_FUNCTION_ARGS)
438 Name n = PG_GETARG_NAME(0);
439 text *p = PG_GETARG_TEXT_PP(1);
441 PG_RETURN_BOOL(RE_compile_and_execute(p,
450 nameregexne(PG_FUNCTION_ARGS)
452 Name n = PG_GETARG_NAME(0);
453 text *p = PG_GETARG_TEXT_PP(1);
455 PG_RETURN_BOOL(!RE_compile_and_execute(p,
464 textregexeq(PG_FUNCTION_ARGS)
466 text *s = PG_GETARG_TEXT_PP(0);
467 text *p = PG_GETARG_TEXT_PP(1);
469 PG_RETURN_BOOL(RE_compile_and_execute(p,
471 VARSIZE_ANY_EXHDR(s),
478 textregexne(PG_FUNCTION_ARGS)
480 text *s = PG_GETARG_TEXT_PP(0);
481 text *p = PG_GETARG_TEXT_PP(1);
483 PG_RETURN_BOOL(!RE_compile_and_execute(p,
485 VARSIZE_ANY_EXHDR(s),
493 * routines that use the regexp stuff, but ignore the case.
494 * for this, we use the REG_ICASE flag to pg_regcomp
499 nameicregexeq(PG_FUNCTION_ARGS)
501 Name n = PG_GETARG_NAME(0);
502 text *p = PG_GETARG_TEXT_PP(1);
504 PG_RETURN_BOOL(RE_compile_and_execute(p,
507 REG_ADVANCED | REG_ICASE,
513 nameicregexne(PG_FUNCTION_ARGS)
515 Name n = PG_GETARG_NAME(0);
516 text *p = PG_GETARG_TEXT_PP(1);
518 PG_RETURN_BOOL(!RE_compile_and_execute(p,
521 REG_ADVANCED | REG_ICASE,
527 texticregexeq(PG_FUNCTION_ARGS)
529 text *s = PG_GETARG_TEXT_PP(0);
530 text *p = PG_GETARG_TEXT_PP(1);
532 PG_RETURN_BOOL(RE_compile_and_execute(p,
534 VARSIZE_ANY_EXHDR(s),
535 REG_ADVANCED | REG_ICASE,
541 texticregexne(PG_FUNCTION_ARGS)
543 text *s = PG_GETARG_TEXT_PP(0);
544 text *p = PG_GETARG_TEXT_PP(1);
546 PG_RETURN_BOOL(!RE_compile_and_execute(p,
548 VARSIZE_ANY_EXHDR(s),
549 REG_ADVANCED | REG_ICASE,
557 * Return a substring matched by a regular expression.
560 textregexsubstr(PG_FUNCTION_ARGS)
562 text *s = PG_GETARG_TEXT_PP(0);
563 text *p = PG_GETARG_TEXT_PP(1);
565 regmatch_t pmatch[2];
570 re = RE_compile_and_cache(p, REG_ADVANCED, PG_GET_COLLATION());
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.
579 VARDATA_ANY(s), VARSIZE_ANY_EXHDR(s),
581 PG_RETURN_NULL(); /* definitely no match */
585 /* has parenthesized subexpressions, use the first one */
586 so = pmatch[1].rm_so;
587 eo = pmatch[1].rm_eo;
591 /* no parenthesized subexpression, use whole match */
592 so = pmatch[0].rm_so;
593 eo = pmatch[0].rm_eo;
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
602 if (so < 0 || eo < 0)
605 return DirectFunctionCall3(text_substr,
607 Int32GetDatum(so + 1),
608 Int32GetDatum(eo - so));
612 * textregexreplace_noopt()
613 * Return a string matched by a regular expression, with replacement.
615 * This version doesn't have an option argument: we default to case
616 * sensitive match, replace the first instance only.
619 textregexreplace_noopt(PG_FUNCTION_ARGS)
621 text *s = PG_GETARG_TEXT_PP(0);
622 text *p = PG_GETARG_TEXT_PP(1);
623 text *r = PG_GETARG_TEXT_PP(2);
626 re = RE_compile_and_cache(p, REG_ADVANCED, PG_GET_COLLATION());
628 PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, false));
633 * Return a string matched by a regular expression, with replacement.
636 textregexreplace(PG_FUNCTION_ARGS)
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);
645 parse_re_flags(&flags, opt);
647 re = RE_compile_and_cache(p, flags.cflags, PG_GET_COLLATION());
649 PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, flags.glob));
654 * Convert a SQL:2008 regexp pattern to POSIX style, so it can be used by
658 similar_escape(PG_FUNCTION_ARGS)
668 bool afterescape = false;
669 bool incharclass = false;
672 /* This function is not strict, so must test explicitly */
675 pat_text = PG_GETARG_TEXT_PP(0);
676 p = VARDATA_ANY(pat_text);
677 plen = VARSIZE_ANY_EXHDR(pat_text);
680 /* No ESCAPE clause provided; default to backslash as escape */
686 esc_text = PG_GETARG_TEXT_PP(1);
687 e = VARDATA_ANY(esc_text);
688 elen = VARSIZE_ANY_EXHDR(esc_text);
690 e = NULL; /* no escape character */
693 int escape_mblen = pg_mbstrlen_with_len(e, elen);
695 if (escape_mblen > 1)
697 (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
698 errmsg("invalid escape string"),
699 errhint("Escape string must be empty or one character.")));
704 * We surround the transformed input string with
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.
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
720 result = (text *) palloc(VARHDRSZ + 6 + 3 * plen);
733 * If both the escape character and the current character from the
734 * pattern are multi-byte, we need to take the slow path.
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.)
745 int mblen = pg_mblen(p);
749 /* slow, multi-byte path */
757 else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
759 /* SQL99 escape character; do not send to output */
765 * We know it's a multi-byte character, so we don't need
766 * to do all the comparisons to single-byte characters
783 if (pchar == '"' && !incharclass) /* for SUBSTRING patterns */
784 *r++ = ((nquotes++ % 2) == 0) ? '(' : ')';
792 else if (e && pchar == *e)
794 /* SQL99 escape character; do not send to output */
797 else if (incharclass)
805 else if (pchar == '[')
810 else if (pchar == '%')
815 else if (pchar == '_')
817 else if (pchar == '(')
819 /* convert to non-capturing parenthesis */
824 else if (pchar == '\\' || pchar == '.' ||
825 pchar == '^' || pchar == '$')
838 SET_VARSIZE(result, r - ((char *) result));
840 PG_RETURN_TEXT_P(result);
845 * Return the first substring(s) matching a pattern within a string.
848 regexp_match(PG_FUNCTION_ARGS)
850 text *orig_str = PG_GETARG_TEXT_PP(0);
851 text *pattern = PG_GETARG_TEXT_PP(1);
852 text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
853 pg_re_flags re_flags;
854 regexp_matches_ctx *matchctx;
856 /* Determine options */
857 parse_re_flags(&re_flags, flags);
858 /* User mustn't specify 'g' */
861 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
862 errmsg("regexp_match does not support the global option"),
863 errhint("Use the regexp_matches function instead.")));
865 matchctx = setup_regexp_matches(orig_str, pattern, &re_flags,
866 PG_GET_COLLATION(), true, false);
868 if (matchctx->nmatches == 0)
871 Assert(matchctx->nmatches == 1);
873 /* Create workspace that build_regexp_match_result needs */
874 matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
875 matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
877 PG_RETURN_DATUM(PointerGetDatum(build_regexp_match_result(matchctx)));
880 /* This is separate to keep the opr_sanity regression test from complaining */
882 regexp_match_no_flags(PG_FUNCTION_ARGS)
884 return regexp_match(fcinfo);
889 * Return a table of all matches of a pattern within a string.
892 regexp_matches(PG_FUNCTION_ARGS)
894 FuncCallContext *funcctx;
895 regexp_matches_ctx *matchctx;
897 if (SRF_IS_FIRSTCALL())
899 text *pattern = PG_GETARG_TEXT_PP(1);
900 text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
901 pg_re_flags re_flags;
902 MemoryContext oldcontext;
904 funcctx = SRF_FIRSTCALL_INIT();
905 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
907 /* Determine options */
908 parse_re_flags(&re_flags, flags);
910 /* be sure to copy the input string into the multi-call ctx */
911 matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
916 /* Pre-create workspace that build_regexp_match_result needs */
917 matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
918 matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
920 MemoryContextSwitchTo(oldcontext);
921 funcctx->user_fctx = (void *) matchctx;
924 funcctx = SRF_PERCALL_SETUP();
925 matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
927 if (matchctx->next_match < matchctx->nmatches)
929 ArrayType *result_ary;
931 result_ary = build_regexp_match_result(matchctx);
932 matchctx->next_match++;
933 SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
936 /* release space in multi-call ctx to avoid intraquery memory leak */
937 cleanup_regexp_matches(matchctx);
939 SRF_RETURN_DONE(funcctx);
942 /* This is separate to keep the opr_sanity regression test from complaining */
944 regexp_matches_no_flags(PG_FUNCTION_ARGS)
946 return regexp_matches(fcinfo);
950 * setup_regexp_matches --- do the initial matching for regexp_match
951 * and regexp_split functions
953 * To avoid having to re-find the compiled pattern on each call, we do
954 * all the matching in one swoop. The returned regexp_matches_ctx contains
955 * the locations of all the substrings matching the pattern.
957 * The two bool parameters have only two patterns (one for matching, one for
958 * splitting) but it seems clearer to distinguish the functionality this way
959 * than to key it all off one "is_split" flag.
961 static regexp_matches_ctx *
962 setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *re_flags,
964 bool use_subpatterns,
965 bool ignore_degenerate)
967 regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
979 /* save original string --- we'll extract result substrings from it */
980 matchctx->orig_str = orig_str;
982 /* convert string to pg_wchar form for matching */
983 orig_len = VARSIZE_ANY_EXHDR(orig_str);
984 wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
985 wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
987 /* set up the compiled pattern */
988 cpattern = RE_compile_and_cache(pattern, re_flags->cflags, collation);
990 /* do we want to remember subpatterns? */
991 if (use_subpatterns && cpattern->re_nsub > 0)
993 matchctx->npatterns = cpattern->re_nsub;
994 pmatch_len = cpattern->re_nsub + 1;
998 use_subpatterns = false;
999 matchctx->npatterns = 1;
1003 /* temporary output space for RE package */
1004 pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
1006 /* the real output space (grown dynamically if needed) */
1007 array_len = re_flags->glob ? 256 : 32;
1008 matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
1011 /* search for the pattern, perhaps repeatedly */
1014 while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
1015 pmatch_len, pmatch))
1018 * If requested, ignore degenerate matches, which are zero-length
1019 * matches occurring at the start or end of a string or just after a
1022 if (!ignore_degenerate ||
1023 (pmatch[0].rm_so < wide_len &&
1024 pmatch[0].rm_eo > prev_match_end))
1026 /* enlarge output space if needed */
1027 while (array_idx + matchctx->npatterns * 2 > array_len)
1030 matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
1031 sizeof(int) * array_len);
1034 /* save this match's locations */
1035 if (use_subpatterns)
1039 for (i = 1; i <= matchctx->npatterns; i++)
1041 matchctx->match_locs[array_idx++] = pmatch[i].rm_so;
1042 matchctx->match_locs[array_idx++] = pmatch[i].rm_eo;
1047 matchctx->match_locs[array_idx++] = pmatch[0].rm_so;
1048 matchctx->match_locs[array_idx++] = pmatch[0].rm_eo;
1050 matchctx->nmatches++;
1052 prev_match_end = pmatch[0].rm_eo;
1054 /* if not glob, stop after one match */
1055 if (!re_flags->glob)
1059 * Advance search position. Normally we start the next search at the
1060 * end of the previous match; but if the match was of zero length, we
1061 * have to advance by one character, or we'd just find the same match
1064 start_search = prev_match_end;
1065 if (pmatch[0].rm_so == pmatch[0].rm_eo)
1067 if (start_search > wide_len)
1071 /* Clean up temp storage */
1079 * cleanup_regexp_matches - release memory of a regexp_matches_ctx
1082 cleanup_regexp_matches(regexp_matches_ctx *matchctx)
1084 pfree(matchctx->orig_str);
1085 pfree(matchctx->match_locs);
1086 if (matchctx->elems)
1087 pfree(matchctx->elems);
1088 if (matchctx->nulls)
1089 pfree(matchctx->nulls);
1094 * build_regexp_match_result - build output array for current match
1097 build_regexp_match_result(regexp_matches_ctx *matchctx)
1099 Datum *elems = matchctx->elems;
1100 bool *nulls = matchctx->nulls;
1106 /* Extract matching substrings from the original string */
1107 loc = matchctx->next_match * matchctx->npatterns * 2;
1108 for (i = 0; i < matchctx->npatterns; i++)
1110 int so = matchctx->match_locs[loc++];
1111 int eo = matchctx->match_locs[loc++];
1113 if (so < 0 || eo < 0)
1115 elems[i] = (Datum) 0;
1120 elems[i] = DirectFunctionCall3(text_substr,
1121 PointerGetDatum(matchctx->orig_str),
1122 Int32GetDatum(so + 1),
1123 Int32GetDatum(eo - so));
1128 /* And form an array */
1129 dims[0] = matchctx->npatterns;
1131 /* XXX: this hardcodes assumptions about the text type */
1132 return construct_md_array(elems, nulls, 1, dims, lbs,
1133 TEXTOID, -1, false, 'i');
1137 * regexp_split_to_table()
1138 * Split the string at matches of the pattern, returning the
1139 * split-out substrings as a table.
1142 regexp_split_to_table(PG_FUNCTION_ARGS)
1144 FuncCallContext *funcctx;
1145 regexp_matches_ctx *splitctx;
1147 if (SRF_IS_FIRSTCALL())
1149 text *pattern = PG_GETARG_TEXT_PP(1);
1150 text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
1151 pg_re_flags re_flags;
1152 MemoryContext oldcontext;
1154 funcctx = SRF_FIRSTCALL_INIT();
1155 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1157 /* Determine options */
1158 parse_re_flags(&re_flags, flags);
1159 /* User mustn't specify 'g' */
1162 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1163 errmsg("regexp_split_to_table does not support the global option")));
1164 /* But we find all the matches anyway */
1165 re_flags.glob = true;
1167 /* be sure to copy the input string into the multi-call ctx */
1168 splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
1173 MemoryContextSwitchTo(oldcontext);
1174 funcctx->user_fctx = (void *) splitctx;
1177 funcctx = SRF_PERCALL_SETUP();
1178 splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
1180 if (splitctx->next_match <= splitctx->nmatches)
1182 Datum result = build_regexp_split_result(splitctx);
1184 splitctx->next_match++;
1185 SRF_RETURN_NEXT(funcctx, result);
1188 /* release space in multi-call ctx to avoid intraquery memory leak */
1189 cleanup_regexp_matches(splitctx);
1191 SRF_RETURN_DONE(funcctx);
1194 /* This is separate to keep the opr_sanity regression test from complaining */
1196 regexp_split_to_table_no_flags(PG_FUNCTION_ARGS)
1198 return regexp_split_to_table(fcinfo);
1202 * regexp_split_to_array()
1203 * Split the string at matches of the pattern, returning the
1204 * split-out substrings as an array.
1207 regexp_split_to_array(PG_FUNCTION_ARGS)
1209 ArrayBuildState *astate = NULL;
1210 pg_re_flags re_flags;
1211 regexp_matches_ctx *splitctx;
1213 /* Determine options */
1214 parse_re_flags(&re_flags, PG_GETARG_TEXT_PP_IF_EXISTS(2));
1215 /* User mustn't specify 'g' */
1218 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1219 errmsg("regexp_split_to_array does not support the global option")));
1220 /* But we find all the matches anyway */
1221 re_flags.glob = true;
1223 splitctx = setup_regexp_matches(PG_GETARG_TEXT_PP(0),
1224 PG_GETARG_TEXT_PP(1),
1229 while (splitctx->next_match <= splitctx->nmatches)
1231 astate = accumArrayResult(astate,
1232 build_regexp_split_result(splitctx),
1235 CurrentMemoryContext);
1236 splitctx->next_match++;
1240 * We don't call cleanup_regexp_matches here; it would try to pfree the
1241 * input string, which we didn't copy. The space is not in a long-lived
1242 * memory context anyway.
1245 PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
1248 /* This is separate to keep the opr_sanity regression test from complaining */
1250 regexp_split_to_array_no_flags(PG_FUNCTION_ARGS)
1252 return regexp_split_to_array(fcinfo);
1256 * build_regexp_split_result - build output string for current match
1258 * We return the string between the current match and the previous one,
1259 * or the string after the last match when next_match == nmatches.
1262 build_regexp_split_result(regexp_matches_ctx *splitctx)
1267 if (splitctx->next_match > 0)
1268 startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
1272 elog(ERROR, "invalid match ending position");
1274 if (splitctx->next_match < splitctx->nmatches)
1276 endpos = splitctx->match_locs[splitctx->next_match * 2];
1277 if (endpos < startpos)
1278 elog(ERROR, "invalid match starting position");
1279 return DirectFunctionCall3(text_substr,
1280 PointerGetDatum(splitctx->orig_str),
1281 Int32GetDatum(startpos + 1),
1282 Int32GetDatum(endpos - startpos));
1286 /* no more matches, return rest of string */
1287 return DirectFunctionCall2(text_substr_no_len,
1288 PointerGetDatum(splitctx->orig_str),
1289 Int32GetDatum(startpos + 1));
1294 * regexp_fixed_prefix - extract fixed prefix, if any, for a regexp
1296 * The result is NULL if there is no fixed prefix, else a palloc'd string.
1297 * If it is an exact match, not just a prefix, *exact is returned as TRUE.
1300 regexp_fixed_prefix(text *text_re, bool case_insensitive, Oid collation,
1312 *exact = false; /* default result */
1315 cflags = REG_ADVANCED;
1316 if (case_insensitive)
1317 cflags |= REG_ICASE;
1319 re = RE_compile_and_cache(text_re, cflags, collation);
1321 /* Examine it to see if there's a fixed prefix */
1322 re_result = pg_regprefix(re, &str, &slen);
1330 /* continue with wchar conversion */
1335 /* continue with wchar conversion */
1340 CHECK_FOR_INTERRUPTS();
1341 pg_regerror(re_result, re, errMsg, sizeof(errMsg));
1343 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
1344 errmsg("regular expression failed: %s", errMsg)));
1348 /* Convert pg_wchar result back to database encoding */
1349 maxlen = pg_database_encoding_max_length() * slen + 1;
1350 result = (char *) palloc(maxlen);
1351 slen = pg_wchar2mb_with_len(str, result, slen);
1352 Assert(slen < maxlen);