LCOV - code coverage report
Current view: top level - src/backend/utils/adt - regexp.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 582 644 90.4 %
Date: 2024-11-21 08:14:44 Functions: 50 51 98.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * regexp.c
       4             :  *    Postgres' interface to the regular expression package.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, 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 "regex/regex.h"
      35             : #include "utils/array.h"
      36             : #include "utils/builtins.h"
      37             : #include "utils/memutils.h"
      38             : #include "utils/varlena.h"
      39             : 
      40             : #define PG_GETARG_TEXT_PP_IF_EXISTS(_n) \
      41             :     (PG_NARGS() > (_n) ? PG_GETARG_TEXT_PP(_n) : NULL)
      42             : 
      43             : 
      44             : /* all the options of interest for regex functions */
      45             : typedef struct pg_re_flags
      46             : {
      47             :     int         cflags;         /* compile flags for Spencer's regex code */
      48             :     bool        glob;           /* do it globally (for each occurrence) */
      49             : } pg_re_flags;
      50             : 
      51             : /* cross-call state for regexp_match and regexp_split functions */
      52             : typedef struct regexp_matches_ctx
      53             : {
      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 */
      64             :     pg_wchar   *wide_str;       /* wide-char version of original string */
      65             :     char       *conv_buf;       /* conversion buffer, if needed */
      66             :     int         conv_bufsiz;    /* size thereof */
      67             : } regexp_matches_ctx;
      68             : 
      69             : /*
      70             :  * We cache precompiled regular expressions using a "self organizing list"
      71             :  * structure, in which recently-used items tend to be near the front.
      72             :  * Whenever we use an entry, it's moved up to the front of the list.
      73             :  * Over time, an item's average position corresponds to its frequency of use.
      74             :  *
      75             :  * When we first create an entry, it's inserted at the front of
      76             :  * the array, dropping the entry at the end of the array if necessary to
      77             :  * make room.  (This might seem to be weighting the new entry too heavily,
      78             :  * but if we insert new entries further back, we'll be unable to adjust to
      79             :  * a sudden shift in the query mix where we are presented with MAX_CACHED_RES
      80             :  * never-before-seen items used circularly.  We ought to be able to handle
      81             :  * that case, so we have to insert at the front.)
      82             :  *
      83             :  * Knuth mentions a variant strategy in which a used item is moved up just
      84             :  * one place in the list.  Although he says this uses fewer comparisons on
      85             :  * average, it seems not to adapt very well to the situation where you have
      86             :  * both some reusable patterns and a steady stream of non-reusable patterns.
      87             :  * A reusable pattern that isn't used at least as often as non-reusable
      88             :  * patterns are seen will "fail to keep up" and will drop off the end of the
      89             :  * cache.  With move-to-front, a reusable pattern is guaranteed to stay in
      90             :  * the cache as long as it's used at least once in every MAX_CACHED_RES uses.
      91             :  */
      92             : 
      93             : /* this is the maximum number of cached regular expressions */
      94             : #ifndef MAX_CACHED_RES
      95             : #define MAX_CACHED_RES  32
      96             : #endif
      97             : 
      98             : /* A parent memory context for regular expressions. */
      99             : static MemoryContext RegexpCacheMemoryContext;
     100             : 
     101             : /* this structure describes one cached regular expression */
     102             : typedef struct cached_re_str
     103             : {
     104             :     MemoryContext cre_context;  /* memory context for this regexp */
     105             :     char       *cre_pat;        /* original RE (not null terminated!) */
     106             :     int         cre_pat_len;    /* length of original RE, in bytes */
     107             :     int         cre_flags;      /* compile flags: extended,icase etc */
     108             :     Oid         cre_collation;  /* collation to use */
     109             :     regex_t     cre_re;         /* the compiled regular expression */
     110             : } cached_re_str;
     111             : 
     112             : static int  num_res = 0;        /* # of cached re's */
     113             : static cached_re_str re_array[MAX_CACHED_RES];  /* cached re's */
     114             : 
     115             : 
     116             : /* Local functions */
     117             : static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern,
     118             :                                                 pg_re_flags *re_flags,
     119             :                                                 int start_search,
     120             :                                                 Oid collation,
     121             :                                                 bool use_subpatterns,
     122             :                                                 bool ignore_degenerate,
     123             :                                                 bool fetching_unmatched);
     124             : static ArrayType *build_regexp_match_result(regexp_matches_ctx *matchctx);
     125             : static Datum build_regexp_split_result(regexp_matches_ctx *splitctx);
     126             : 
     127             : 
     128             : /*
     129             :  * RE_compile_and_cache - compile a RE, caching if possible
     130             :  *
     131             :  * Returns regex_t *
     132             :  *
     133             :  *  text_re --- the pattern, expressed as a TEXT object
     134             :  *  cflags --- compile options for the pattern
     135             :  *  collation --- collation to use for LC_CTYPE-dependent behavior
     136             :  *
     137             :  * Pattern is given in the database encoding.  We internally convert to
     138             :  * an array of pg_wchar, which is what Spencer's regex package wants.
     139             :  */
     140             : regex_t *
     141     1470286 : RE_compile_and_cache(text *text_re, int cflags, Oid collation)
     142             : {
     143     1470286 :     int         text_re_len = VARSIZE_ANY_EXHDR(text_re);
     144     1470286 :     char       *text_re_val = VARDATA_ANY(text_re);
     145             :     pg_wchar   *pattern;
     146             :     int         pattern_len;
     147             :     int         i;
     148             :     int         regcomp_result;
     149             :     cached_re_str re_temp;
     150             :     char        errMsg[100];
     151             :     MemoryContext oldcontext;
     152             : 
     153             :     /*
     154             :      * Look for a match among previously compiled REs.  Since the data
     155             :      * structure is self-organizing with most-used entries at the front, our
     156             :      * search strategy can just be to scan from the front.
     157             :      */
     158     1995098 :     for (i = 0; i < num_res; i++)
     159             :     {
     160     1989538 :         if (re_array[i].cre_pat_len == text_re_len &&
     161     1478590 :             re_array[i].cre_flags == cflags &&
     162     1477386 :             re_array[i].cre_collation == collation &&
     163     1477190 :             memcmp(re_array[i].cre_pat, text_re_val, text_re_len) == 0)
     164             :         {
     165             :             /*
     166             :              * Found a match; move it to front if not there already.
     167             :              */
     168     1464726 :             if (i > 0)
     169             :             {
     170      430142 :                 re_temp = re_array[i];
     171      430142 :                 memmove(&re_array[1], &re_array[0], i * sizeof(cached_re_str));
     172      430142 :                 re_array[0] = re_temp;
     173             :             }
     174             : 
     175     1464726 :             return &re_array[0].cre_re;
     176             :         }
     177             :     }
     178             : 
     179             :     /* Set up the cache memory on first go through. */
     180        5560 :     if (unlikely(RegexpCacheMemoryContext == NULL))
     181        1320 :         RegexpCacheMemoryContext =
     182        1320 :             AllocSetContextCreate(TopMemoryContext,
     183             :                                   "RegexpCacheMemoryContext",
     184             :                                   ALLOCSET_SMALL_SIZES);
     185             : 
     186             :     /*
     187             :      * Couldn't find it, so try to compile the new RE.  To avoid leaking
     188             :      * resources on failure, we build into the re_temp local.
     189             :      */
     190             : 
     191             :     /* Convert pattern string to wide characters */
     192        5560 :     pattern = (pg_wchar *) palloc((text_re_len + 1) * sizeof(pg_wchar));
     193        5560 :     pattern_len = pg_mb2wchar_with_len(text_re_val,
     194             :                                        pattern,
     195             :                                        text_re_len);
     196             : 
     197             :     /*
     198             :      * Make a memory context for this compiled regexp.  This is initially a
     199             :      * child of the current memory context, so it will be cleaned up
     200             :      * automatically if compilation is interrupted and throws an ERROR. We'll
     201             :      * re-parent it under the longer lived cache context if we make it to the
     202             :      * bottom of this function.
     203             :      */
     204        5560 :     re_temp.cre_context = AllocSetContextCreate(CurrentMemoryContext,
     205             :                                                 "RegexpMemoryContext",
     206             :                                                 ALLOCSET_SMALL_SIZES);
     207        5560 :     oldcontext = MemoryContextSwitchTo(re_temp.cre_context);
     208             : 
     209        5560 :     regcomp_result = pg_regcomp(&re_temp.cre_re,
     210             :                                 pattern,
     211             :                                 pattern_len,
     212             :                                 cflags,
     213             :                                 collation);
     214             : 
     215        5536 :     pfree(pattern);
     216             : 
     217        5536 :     if (regcomp_result != REG_OKAY)
     218             :     {
     219             :         /* re didn't compile (no need for pg_regfree, if so) */
     220          36 :         pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
     221          36 :         ereport(ERROR,
     222             :                 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
     223             :                  errmsg("invalid regular expression: %s", errMsg)));
     224             :     }
     225             : 
     226             :     /* Copy the pattern into the per-regexp memory context. */
     227        5500 :     re_temp.cre_pat = palloc(text_re_len + 1);
     228        5500 :     memcpy(re_temp.cre_pat, text_re_val, text_re_len);
     229             : 
     230             :     /*
     231             :      * NUL-terminate it only for the benefit of the identifier used for the
     232             :      * memory context, visible in the pg_backend_memory_contexts view.
     233             :      */
     234        5500 :     re_temp.cre_pat[text_re_len] = 0;
     235        5500 :     MemoryContextSetIdentifier(re_temp.cre_context, re_temp.cre_pat);
     236             : 
     237        5500 :     re_temp.cre_pat_len = text_re_len;
     238        5500 :     re_temp.cre_flags = cflags;
     239        5500 :     re_temp.cre_collation = collation;
     240             : 
     241             :     /*
     242             :      * Okay, we have a valid new item in re_temp; insert it into the storage
     243             :      * array.  Discard last entry if needed.
     244             :      */
     245        5500 :     if (num_res >= MAX_CACHED_RES)
     246             :     {
     247         792 :         --num_res;
     248             :         Assert(num_res < MAX_CACHED_RES);
     249             :         /* Delete the memory context holding the regexp and pattern. */
     250         792 :         MemoryContextDelete(re_array[num_res].cre_context);
     251             :     }
     252             : 
     253             :     /* Re-parent the memory context to our long-lived cache context. */
     254        5500 :     MemoryContextSetParent(re_temp.cre_context, RegexpCacheMemoryContext);
     255             : 
     256        5500 :     if (num_res > 0)
     257        4180 :         memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
     258             : 
     259        5500 :     re_array[0] = re_temp;
     260        5500 :     num_res++;
     261             : 
     262        5500 :     MemoryContextSwitchTo(oldcontext);
     263             : 
     264        5500 :     return &re_array[0].cre_re;
     265             : }
     266             : 
     267             : /*
     268             :  * RE_wchar_execute - execute a RE on pg_wchar data
     269             :  *
     270             :  * Returns true on match, false on no match
     271             :  *
     272             :  *  re --- the compiled pattern as returned by RE_compile_and_cache
     273             :  *  data --- the data to match against (need not be null-terminated)
     274             :  *  data_len --- the length of the data string
     275             :  *  start_search -- the offset in the data to start searching
     276             :  *  nmatch, pmatch  --- optional return area for match details
     277             :  *
     278             :  * Data is given as array of pg_wchar which is what Spencer's regex package
     279             :  * wants.
     280             :  */
     281             : static bool
     282     2334392 : RE_wchar_execute(regex_t *re, pg_wchar *data, int data_len,
     283             :                  int start_search, int nmatch, regmatch_t *pmatch)
     284             : {
     285             :     int         regexec_result;
     286             :     char        errMsg[100];
     287             : 
     288             :     /* Perform RE match and return result */
     289     2334392 :     regexec_result = pg_regexec(re,
     290             :                                 data,
     291             :                                 data_len,
     292             :                                 start_search,
     293             :                                 NULL,   /* no details */
     294             :                                 nmatch,
     295             :                                 pmatch,
     296             :                                 0);
     297             : 
     298     2334392 :     if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
     299             :     {
     300             :         /* re failed??? */
     301           0 :         pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
     302           0 :         ereport(ERROR,
     303             :                 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
     304             :                  errmsg("regular expression failed: %s", errMsg)));
     305             :     }
     306             : 
     307     2334392 :     return (regexec_result == REG_OKAY);
     308             : }
     309             : 
     310             : /*
     311             :  * RE_execute - execute a RE
     312             :  *
     313             :  * Returns true on match, false on no match
     314             :  *
     315             :  *  re --- the compiled pattern as returned by RE_compile_and_cache
     316             :  *  dat --- the data to match against (need not be null-terminated)
     317             :  *  dat_len --- the length of the data string
     318             :  *  nmatch, pmatch  --- optional return area for match details
     319             :  *
     320             :  * Data is given in the database encoding.  We internally
     321             :  * convert to array of pg_wchar which is what Spencer's regex package wants.
     322             :  */
     323             : static bool
     324     1239074 : RE_execute(regex_t *re, char *dat, int dat_len,
     325             :            int nmatch, regmatch_t *pmatch)
     326             : {
     327             :     pg_wchar   *data;
     328             :     int         data_len;
     329             :     bool        match;
     330             : 
     331             :     /* Convert data string to wide characters */
     332     1239074 :     data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
     333     1239074 :     data_len = pg_mb2wchar_with_len(dat, data, dat_len);
     334             : 
     335             :     /* Perform RE match and return result */
     336     1239074 :     match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
     337             : 
     338     1239074 :     pfree(data);
     339     1239074 :     return match;
     340             : }
     341             : 
     342             : /*
     343             :  * RE_compile_and_execute - compile and execute a RE
     344             :  *
     345             :  * Returns true on match, false on no match
     346             :  *
     347             :  *  text_re --- the pattern, expressed as a TEXT object
     348             :  *  dat --- the data to match against (need not be null-terminated)
     349             :  *  dat_len --- the length of the data string
     350             :  *  cflags --- compile options for the pattern
     351             :  *  collation --- collation to use for LC_CTYPE-dependent behavior
     352             :  *  nmatch, pmatch  --- optional return area for match details
     353             :  *
     354             :  * Both pattern and data are given in the database encoding.  We internally
     355             :  * convert to array of pg_wchar which is what Spencer's regex package wants.
     356             :  */
     357             : bool
     358     1237470 : RE_compile_and_execute(text *text_re, char *dat, int dat_len,
     359             :                        int cflags, Oid collation,
     360             :                        int nmatch, regmatch_t *pmatch)
     361             : {
     362             :     regex_t    *re;
     363             : 
     364             :     /* Use REG_NOSUB if caller does not want sub-match details */
     365     1237470 :     if (nmatch < 2)
     366     1237470 :         cflags |= REG_NOSUB;
     367             : 
     368             :     /* Compile RE */
     369     1237470 :     re = RE_compile_and_cache(text_re, cflags, collation);
     370             : 
     371     1237446 :     return RE_execute(re, dat, dat_len, nmatch, pmatch);
     372             : }
     373             : 
     374             : 
     375             : /*
     376             :  * parse_re_flags - parse the options argument of regexp_match and friends
     377             :  *
     378             :  *  flags --- output argument, filled with desired options
     379             :  *  opts --- TEXT object, or NULL for defaults
     380             :  *
     381             :  * This accepts all the options allowed by any of the callers; callers that
     382             :  * don't want some have to reject them after the fact.
     383             :  */
     384             : static void
     385      206486 : parse_re_flags(pg_re_flags *flags, text *opts)
     386             : {
     387             :     /* regex flavor is always folded into the compile flags */
     388      206486 :     flags->cflags = REG_ADVANCED;
     389      206486 :     flags->glob = false;
     390             : 
     391      206486 :     if (opts)
     392             :     {
     393        3230 :         char       *opt_p = VARDATA_ANY(opts);
     394        3230 :         int         opt_len = VARSIZE_ANY_EXHDR(opts);
     395             :         int         i;
     396             : 
     397        7424 :         for (i = 0; i < opt_len; i++)
     398             :         {
     399        4218 :             switch (opt_p[i])
     400             :             {
     401        2908 :                 case 'g':
     402        2908 :                     flags->glob = true;
     403        2908 :                     break;
     404           0 :                 case 'b':       /* BREs (but why???) */
     405           0 :                     flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
     406           0 :                     break;
     407          10 :                 case 'c':       /* case sensitive */
     408          10 :                     flags->cflags &= ~REG_ICASE;
     409          10 :                     break;
     410           0 :                 case 'e':       /* plain EREs */
     411           0 :                     flags->cflags |= REG_EXTENDED;
     412           0 :                     flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
     413           0 :                     break;
     414         292 :                 case 'i':       /* case insensitive */
     415         292 :                     flags->cflags |= REG_ICASE;
     416         292 :                     break;
     417         966 :                 case 'm':       /* Perloid synonym for n */
     418             :                 case 'n':       /* \n affects ^ $ . [^ */
     419         966 :                     flags->cflags |= REG_NEWLINE;
     420         966 :                     break;
     421           0 :                 case 'p':       /* ~Perl, \n affects . [^ */
     422           0 :                     flags->cflags |= REG_NLSTOP;
     423           0 :                     flags->cflags &= ~REG_NLANCH;
     424           0 :                     break;
     425           0 :                 case 'q':       /* literal string */
     426           0 :                     flags->cflags |= REG_QUOTE;
     427           0 :                     flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
     428           0 :                     break;
     429          12 :                 case 's':       /* single line, \n ordinary */
     430          12 :                     flags->cflags &= ~REG_NEWLINE;
     431          12 :                     break;
     432           0 :                 case 't':       /* tight syntax */
     433           0 :                     flags->cflags &= ~REG_EXPANDED;
     434           0 :                     break;
     435           0 :                 case 'w':       /* weird, \n affects ^ $ only */
     436           0 :                     flags->cflags &= ~REG_NLSTOP;
     437           0 :                     flags->cflags |= REG_NLANCH;
     438           0 :                     break;
     439           6 :                 case 'x':       /* expanded syntax */
     440           6 :                     flags->cflags |= REG_EXPANDED;
     441           6 :                     break;
     442          24 :                 default:
     443          24 :                     ereport(ERROR,
     444             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     445             :                              errmsg("invalid regular expression option: \"%.*s\"",
     446             :                                     pg_mblen(opt_p + i), opt_p + i)));
     447             :                     break;
     448             :             }
     449             :         }
     450             :     }
     451      206462 : }
     452             : 
     453             : 
     454             : /*
     455             :  *  interface routines called by the function manager
     456             :  */
     457             : 
     458             : Datum
     459      775956 : nameregexeq(PG_FUNCTION_ARGS)
     460             : {
     461      775956 :     Name        n = PG_GETARG_NAME(0);
     462      775956 :     text       *p = PG_GETARG_TEXT_PP(1);
     463             : 
     464      775956 :     PG_RETURN_BOOL(RE_compile_and_execute(p,
     465             :                                           NameStr(*n),
     466             :                                           strlen(NameStr(*n)),
     467             :                                           REG_ADVANCED,
     468             :                                           PG_GET_COLLATION(),
     469             :                                           0, NULL));
     470             : }
     471             : 
     472             : Datum
     473       22372 : nameregexne(PG_FUNCTION_ARGS)
     474             : {
     475       22372 :     Name        n = PG_GETARG_NAME(0);
     476       22372 :     text       *p = PG_GETARG_TEXT_PP(1);
     477             : 
     478       22372 :     PG_RETURN_BOOL(!RE_compile_and_execute(p,
     479             :                                            NameStr(*n),
     480             :                                            strlen(NameStr(*n)),
     481             :                                            REG_ADVANCED,
     482             :                                            PG_GET_COLLATION(),
     483             :                                            0, NULL));
     484             : }
     485             : 
     486             : Datum
     487      397328 : textregexeq(PG_FUNCTION_ARGS)
     488             : {
     489      397328 :     text       *s = PG_GETARG_TEXT_PP(0);
     490      397328 :     text       *p = PG_GETARG_TEXT_PP(1);
     491             : 
     492      397328 :     PG_RETURN_BOOL(RE_compile_and_execute(p,
     493             :                                           VARDATA_ANY(s),
     494             :                                           VARSIZE_ANY_EXHDR(s),
     495             :                                           REG_ADVANCED,
     496             :                                           PG_GET_COLLATION(),
     497             :                                           0, NULL));
     498             : }
     499             : 
     500             : Datum
     501       34134 : textregexne(PG_FUNCTION_ARGS)
     502             : {
     503       34134 :     text       *s = PG_GETARG_TEXT_PP(0);
     504       34134 :     text       *p = PG_GETARG_TEXT_PP(1);
     505             : 
     506       34134 :     PG_RETURN_BOOL(!RE_compile_and_execute(p,
     507             :                                            VARDATA_ANY(s),
     508             :                                            VARSIZE_ANY_EXHDR(s),
     509             :                                            REG_ADVANCED,
     510             :                                            PG_GET_COLLATION(),
     511             :                                            0, NULL));
     512             : }
     513             : 
     514             : 
     515             : /*
     516             :  *  routines that use the regexp stuff, but ignore the case.
     517             :  *  for this, we use the REG_ICASE flag to pg_regcomp
     518             :  */
     519             : 
     520             : 
     521             : Datum
     522        7156 : nameicregexeq(PG_FUNCTION_ARGS)
     523             : {
     524        7156 :     Name        n = PG_GETARG_NAME(0);
     525        7156 :     text       *p = PG_GETARG_TEXT_PP(1);
     526             : 
     527        7156 :     PG_RETURN_BOOL(RE_compile_and_execute(p,
     528             :                                           NameStr(*n),
     529             :                                           strlen(NameStr(*n)),
     530             :                                           REG_ADVANCED | REG_ICASE,
     531             :                                           PG_GET_COLLATION(),
     532             :                                           0, NULL));
     533             : }
     534             : 
     535             : Datum
     536           6 : nameicregexne(PG_FUNCTION_ARGS)
     537             : {
     538           6 :     Name        n = PG_GETARG_NAME(0);
     539           6 :     text       *p = PG_GETARG_TEXT_PP(1);
     540             : 
     541           6 :     PG_RETURN_BOOL(!RE_compile_and_execute(p,
     542             :                                            NameStr(*n),
     543             :                                            strlen(NameStr(*n)),
     544             :                                            REG_ADVANCED | REG_ICASE,
     545             :                                            PG_GET_COLLATION(),
     546             :                                            0, NULL));
     547             : }
     548             : 
     549             : Datum
     550         196 : texticregexeq(PG_FUNCTION_ARGS)
     551             : {
     552         196 :     text       *s = PG_GETARG_TEXT_PP(0);
     553         196 :     text       *p = PG_GETARG_TEXT_PP(1);
     554             : 
     555         196 :     PG_RETURN_BOOL(RE_compile_and_execute(p,
     556             :                                           VARDATA_ANY(s),
     557             :                                           VARSIZE_ANY_EXHDR(s),
     558             :                                           REG_ADVANCED | REG_ICASE,
     559             :                                           PG_GET_COLLATION(),
     560             :                                           0, NULL));
     561             : }
     562             : 
     563             : Datum
     564          22 : texticregexne(PG_FUNCTION_ARGS)
     565             : {
     566          22 :     text       *s = PG_GETARG_TEXT_PP(0);
     567          22 :     text       *p = PG_GETARG_TEXT_PP(1);
     568             : 
     569          22 :     PG_RETURN_BOOL(!RE_compile_and_execute(p,
     570             :                                            VARDATA_ANY(s),
     571             :                                            VARSIZE_ANY_EXHDR(s),
     572             :                                            REG_ADVANCED | REG_ICASE,
     573             :                                            PG_GET_COLLATION(),
     574             :                                            0, NULL));
     575             : }
     576             : 
     577             : 
     578             : /*
     579             :  * textregexsubstr()
     580             :  *      Return a substring matched by a regular expression.
     581             :  */
     582             : Datum
     583        1628 : textregexsubstr(PG_FUNCTION_ARGS)
     584             : {
     585        1628 :     text       *s = PG_GETARG_TEXT_PP(0);
     586        1628 :     text       *p = PG_GETARG_TEXT_PP(1);
     587             :     regex_t    *re;
     588             :     regmatch_t  pmatch[2];
     589             :     int         so,
     590             :                 eo;
     591             : 
     592             :     /* Compile RE */
     593        1628 :     re = RE_compile_and_cache(p, REG_ADVANCED, PG_GET_COLLATION());
     594             : 
     595             :     /*
     596             :      * We pass two regmatch_t structs to get info about the overall match and
     597             :      * the match for the first parenthesized subexpression (if any). If there
     598             :      * is a parenthesized subexpression, we return what it matched; else
     599             :      * return what the whole regexp matched.
     600             :      */
     601        3256 :     if (!RE_execute(re,
     602        3256 :                     VARDATA_ANY(s), VARSIZE_ANY_EXHDR(s),
     603             :                     2, pmatch))
     604           6 :         PG_RETURN_NULL();       /* definitely no match */
     605             : 
     606        1622 :     if (re->re_nsub > 0)
     607             :     {
     608             :         /* has parenthesized subexpressions, use the first one */
     609        1522 :         so = pmatch[1].rm_so;
     610        1522 :         eo = pmatch[1].rm_eo;
     611             :     }
     612             :     else
     613             :     {
     614             :         /* no parenthesized subexpression, use whole match */
     615         100 :         so = pmatch[0].rm_so;
     616         100 :         eo = pmatch[0].rm_eo;
     617             :     }
     618             : 
     619             :     /*
     620             :      * It is possible to have a match to the whole pattern but no match for a
     621             :      * subexpression; for example 'foo(bar)?' is considered to match 'foo' but
     622             :      * there is no subexpression match.  So this extra test for match failure
     623             :      * is not redundant.
     624             :      */
     625        1622 :     if (so < 0 || eo < 0)
     626           6 :         PG_RETURN_NULL();
     627             : 
     628        1616 :     return DirectFunctionCall3(text_substr,
     629             :                                PointerGetDatum(s),
     630             :                                Int32GetDatum(so + 1),
     631             :                                Int32GetDatum(eo - so));
     632             : }
     633             : 
     634             : /*
     635             :  * textregexreplace_noopt()
     636             :  *      Return a string matched by a regular expression, with replacement.
     637             :  *
     638             :  * This version doesn't have an option argument: we default to case
     639             :  * sensitive match, replace the first instance only.
     640             :  */
     641             : Datum
     642        9942 : textregexreplace_noopt(PG_FUNCTION_ARGS)
     643             : {
     644        9942 :     text       *s = PG_GETARG_TEXT_PP(0);
     645        9942 :     text       *p = PG_GETARG_TEXT_PP(1);
     646        9942 :     text       *r = PG_GETARG_TEXT_PP(2);
     647             : 
     648        9942 :     PG_RETURN_TEXT_P(replace_text_regexp(s, p, r,
     649             :                                          REG_ADVANCED, PG_GET_COLLATION(),
     650             :                                          0, 1));
     651             : }
     652             : 
     653             : /*
     654             :  * textregexreplace()
     655             :  *      Return a string matched by a regular expression, with replacement.
     656             :  */
     657             : Datum
     658        2836 : textregexreplace(PG_FUNCTION_ARGS)
     659             : {
     660        2836 :     text       *s = PG_GETARG_TEXT_PP(0);
     661        2836 :     text       *p = PG_GETARG_TEXT_PP(1);
     662        2836 :     text       *r = PG_GETARG_TEXT_PP(2);
     663        2836 :     text       *opt = PG_GETARG_TEXT_PP(3);
     664             :     pg_re_flags flags;
     665             : 
     666             :     /*
     667             :      * regexp_replace() with four arguments will be preferentially resolved as
     668             :      * this form when the fourth argument is of type UNKNOWN.  However, the
     669             :      * user might have intended to call textregexreplace_extended_no_n.  If we
     670             :      * see flags that look like an integer, emit the same error that
     671             :      * parse_re_flags would, but add a HINT about how to fix it.
     672             :      */
     673        2836 :     if (VARSIZE_ANY_EXHDR(opt) > 0)
     674             :     {
     675        2836 :         char       *opt_p = VARDATA_ANY(opt);
     676             : 
     677        2836 :         if (*opt_p >= '0' && *opt_p <= '9')
     678           6 :             ereport(ERROR,
     679             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     680             :                      errmsg("invalid regular expression option: \"%.*s\"",
     681             :                             pg_mblen(opt_p), opt_p),
     682             :                      errhint("If you meant to use regexp_replace() with a start parameter, cast the fourth argument to integer explicitly.")));
     683             :     }
     684             : 
     685        2830 :     parse_re_flags(&flags, opt);
     686             : 
     687        2824 :     PG_RETURN_TEXT_P(replace_text_regexp(s, p, r,
     688             :                                          flags.cflags, PG_GET_COLLATION(),
     689             :                                          0, flags.glob ? 0 : 1));
     690             : }
     691             : 
     692             : /*
     693             :  * textregexreplace_extended()
     694             :  *      Return a string matched by a regular expression, with replacement.
     695             :  *      Extends textregexreplace by allowing a start position and the
     696             :  *      choice of the occurrence to replace (0 means all occurrences).
     697             :  */
     698             : Datum
     699          66 : textregexreplace_extended(PG_FUNCTION_ARGS)
     700             : {
     701          66 :     text       *s = PG_GETARG_TEXT_PP(0);
     702          66 :     text       *p = PG_GETARG_TEXT_PP(1);
     703          66 :     text       *r = PG_GETARG_TEXT_PP(2);
     704          66 :     int         start = 1;
     705          66 :     int         n = 1;
     706          66 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
     707             :     pg_re_flags re_flags;
     708             : 
     709             :     /* Collect optional parameters */
     710          66 :     if (PG_NARGS() > 3)
     711             :     {
     712          66 :         start = PG_GETARG_INT32(3);
     713          66 :         if (start <= 0)
     714           6 :             ereport(ERROR,
     715             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     716             :                      errmsg("invalid value for parameter \"%s\": %d",
     717             :                             "start", start)));
     718             :     }
     719          60 :     if (PG_NARGS() > 4)
     720             :     {
     721          54 :         n = PG_GETARG_INT32(4);
     722          54 :         if (n < 0)
     723           6 :             ereport(ERROR,
     724             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     725             :                      errmsg("invalid value for parameter \"%s\": %d",
     726             :                             "n", n)));
     727             :     }
     728             : 
     729             :     /* Determine options */
     730          54 :     parse_re_flags(&re_flags, flags);
     731             : 
     732             :     /* If N was not specified, deduce it from the 'g' flag */
     733          54 :     if (PG_NARGS() <= 4)
     734           6 :         n = re_flags.glob ? 0 : 1;
     735             : 
     736             :     /* Do the replacement(s) */
     737          54 :     PG_RETURN_TEXT_P(replace_text_regexp(s, p, r,
     738             :                                          re_flags.cflags, PG_GET_COLLATION(),
     739             :                                          start - 1, n));
     740             : }
     741             : 
     742             : /* This is separate to keep the opr_sanity regression test from complaining */
     743             : Datum
     744           6 : textregexreplace_extended_no_n(PG_FUNCTION_ARGS)
     745             : {
     746           6 :     return textregexreplace_extended(fcinfo);
     747             : }
     748             : 
     749             : /* This is separate to keep the opr_sanity regression test from complaining */
     750             : Datum
     751           6 : textregexreplace_extended_no_flags(PG_FUNCTION_ARGS)
     752             : {
     753           6 :     return textregexreplace_extended(fcinfo);
     754             : }
     755             : 
     756             : /*
     757             :  * similar_to_escape(), similar_escape()
     758             :  *
     759             :  * Convert a SQL "SIMILAR TO" regexp pattern to POSIX style, so it can be
     760             :  * used by our regexp engine.
     761             :  *
     762             :  * similar_escape_internal() is the common workhorse for three SQL-exposed
     763             :  * functions.  esc_text can be passed as NULL to select the default escape
     764             :  * (which is '\'), or as an empty string to select no escape character.
     765             :  */
     766             : static text *
     767         138 : similar_escape_internal(text *pat_text, text *esc_text)
     768             : {
     769             :     text       *result;
     770             :     char       *p,
     771             :                *e,
     772             :                *r;
     773             :     int         plen,
     774             :                 elen;
     775         138 :     bool        afterescape = false;
     776         138 :     bool        incharclass = false;
     777         138 :     int         nquotes = 0;
     778             : 
     779         138 :     p = VARDATA_ANY(pat_text);
     780         138 :     plen = VARSIZE_ANY_EXHDR(pat_text);
     781         138 :     if (esc_text == NULL)
     782             :     {
     783             :         /* No ESCAPE clause provided; default to backslash as escape */
     784          40 :         e = "\\";
     785          40 :         elen = 1;
     786             :     }
     787             :     else
     788             :     {
     789          98 :         e = VARDATA_ANY(esc_text);
     790          98 :         elen = VARSIZE_ANY_EXHDR(esc_text);
     791          98 :         if (elen == 0)
     792           6 :             e = NULL;           /* no escape character */
     793          92 :         else if (elen > 1)
     794             :         {
     795           6 :             int         escape_mblen = pg_mbstrlen_with_len(e, elen);
     796             : 
     797           6 :             if (escape_mblen > 1)
     798           6 :                 ereport(ERROR,
     799             :                         (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
     800             :                          errmsg("invalid escape string"),
     801             :                          errhint("Escape string must be empty or one character.")));
     802             :         }
     803             :     }
     804             : 
     805             :     /*----------
     806             :      * We surround the transformed input string with
     807             :      *          ^(?: ... )$
     808             :      * which requires some explanation.  We need "^" and "$" to force
     809             :      * the pattern to match the entire input string as per the SQL spec.
     810             :      * The "(?:" and ")" are a non-capturing set of parens; we have to have
     811             :      * parens in case the string contains "|", else the "^" and "$" will
     812             :      * be bound into the first and last alternatives which is not what we
     813             :      * want, and the parens must be non capturing because we don't want them
     814             :      * to count when selecting output for SUBSTRING.
     815             :      *
     816             :      * When the pattern is divided into three parts by escape-double-quotes,
     817             :      * what we emit is
     818             :      *          ^(?:part1){1,1}?(part2){1,1}(?:part3)$
     819             :      * which requires even more explanation.  The "{1,1}?" on part1 makes it
     820             :      * non-greedy so that it will match the smallest possible amount of text
     821             :      * not the largest, as required by SQL.  The plain parens around part2
     822             :      * are capturing parens so that that part is what controls the result of
     823             :      * SUBSTRING.  The "{1,1}" forces part2 to be greedy, so that it matches
     824             :      * the largest possible amount of text; hence part3 must match the
     825             :      * smallest amount of text, as required by SQL.  We don't need an explicit
     826             :      * greediness marker on part3.  Note that this also confines the effects
     827             :      * of any "|" characters to the respective part, which is what we want.
     828             :      *
     829             :      * The SQL spec says that SUBSTRING's pattern must contain exactly two
     830             :      * escape-double-quotes, but we only complain if there's more than two.
     831             :      * With none, we act as though part1 and part3 are empty; with one, we
     832             :      * act as though part3 is empty.  Both behaviors fall out of omitting
     833             :      * the relevant part separators in the above expansion.  If the result
     834             :      * of this function is used in a plain regexp match (SIMILAR TO), the
     835             :      * escape-double-quotes have no effect on the match behavior.
     836             :      *----------
     837             :      */
     838             : 
     839             :     /*
     840             :      * We need room for the prefix/postfix and part separators, plus as many
     841             :      * as 3 output bytes per input byte; since the input is at most 1GB this
     842             :      * can't overflow size_t.
     843             :      */
     844         132 :     result = (text *) palloc(VARHDRSZ + 23 + 3 * (size_t) plen);
     845         132 :     r = VARDATA(result);
     846             : 
     847         132 :     *r++ = '^';
     848         132 :     *r++ = '(';
     849         132 :     *r++ = '?';
     850         132 :     *r++ = ':';
     851             : 
     852        1096 :     while (plen > 0)
     853             :     {
     854         970 :         char        pchar = *p;
     855             : 
     856             :         /*
     857             :          * If both the escape character and the current character from the
     858             :          * pattern are multi-byte, we need to take the slow path.
     859             :          *
     860             :          * But if one of them is single-byte, we can process the pattern one
     861             :          * byte at a time, ignoring multi-byte characters.  (This works
     862             :          * because all server-encodings have the property that a valid
     863             :          * multi-byte character representation cannot contain the
     864             :          * representation of a valid single-byte character.)
     865             :          */
     866             : 
     867         970 :         if (elen > 1)
     868             :         {
     869           0 :             int         mblen = pg_mblen(p);
     870             : 
     871           0 :             if (mblen > 1)
     872             :             {
     873             :                 /* slow, multi-byte path */
     874           0 :                 if (afterescape)
     875             :                 {
     876           0 :                     *r++ = '\\';
     877           0 :                     memcpy(r, p, mblen);
     878           0 :                     r += mblen;
     879           0 :                     afterescape = false;
     880             :                 }
     881           0 :                 else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
     882             :                 {
     883             :                     /* SQL escape character; do not send to output */
     884           0 :                     afterescape = true;
     885             :                 }
     886             :                 else
     887             :                 {
     888             :                     /*
     889             :                      * We know it's a multi-byte character, so we don't need
     890             :                      * to do all the comparisons to single-byte characters
     891             :                      * that we do below.
     892             :                      */
     893           0 :                     memcpy(r, p, mblen);
     894           0 :                     r += mblen;
     895             :                 }
     896             : 
     897           0 :                 p += mblen;
     898           0 :                 plen -= mblen;
     899             : 
     900           0 :                 continue;
     901             :             }
     902             :         }
     903             : 
     904             :         /* fast path */
     905         970 :         if (afterescape)
     906             :         {
     907         154 :             if (pchar == '"' && !incharclass)  /* escape-double-quote? */
     908             :             {
     909             :                 /* emit appropriate part separator, per notes above */
     910         136 :                 if (nquotes == 0)
     911             :                 {
     912          68 :                     *r++ = ')';
     913          68 :                     *r++ = '{';
     914          68 :                     *r++ = '1';
     915          68 :                     *r++ = ',';
     916          68 :                     *r++ = '1';
     917          68 :                     *r++ = '}';
     918          68 :                     *r++ = '?';
     919          68 :                     *r++ = '(';
     920             :                 }
     921          68 :                 else if (nquotes == 1)
     922             :                 {
     923          62 :                     *r++ = ')';
     924          62 :                     *r++ = '{';
     925          62 :                     *r++ = '1';
     926          62 :                     *r++ = ',';
     927          62 :                     *r++ = '1';
     928          62 :                     *r++ = '}';
     929          62 :                     *r++ = '(';
     930          62 :                     *r++ = '?';
     931          62 :                     *r++ = ':';
     932             :                 }
     933             :                 else
     934           6 :                     ereport(ERROR,
     935             :                             (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
     936             :                              errmsg("SQL regular expression may not contain more than two escape-double-quote separators")));
     937         130 :                 nquotes++;
     938             :             }
     939             :             else
     940             :             {
     941             :                 /*
     942             :                  * We allow any character at all to be escaped; notably, this
     943             :                  * allows access to POSIX character-class escapes such as
     944             :                  * "\d".  The SQL spec is considerably more restrictive.
     945             :                  */
     946          18 :                 *r++ = '\\';
     947          18 :                 *r++ = pchar;
     948             :             }
     949         148 :             afterescape = false;
     950             :         }
     951         816 :         else if (e && pchar == *e)
     952             :         {
     953             :             /* SQL escape character; do not send to output */
     954         154 :             afterescape = true;
     955             :         }
     956         662 :         else if (incharclass)
     957             :         {
     958          36 :             if (pchar == '\\')
     959           0 :                 *r++ = '\\';
     960          36 :             *r++ = pchar;
     961          36 :             if (pchar == ']')
     962           6 :                 incharclass = false;
     963             :         }
     964         626 :         else if (pchar == '[')
     965             :         {
     966           6 :             *r++ = pchar;
     967           6 :             incharclass = true;
     968             :         }
     969         620 :         else if (pchar == '%')
     970             :         {
     971         114 :             *r++ = '.';
     972         114 :             *r++ = '*';
     973             :         }
     974         506 :         else if (pchar == '_')
     975          52 :             *r++ = '.';
     976         454 :         else if (pchar == '(')
     977             :         {
     978             :             /* convert to non-capturing parenthesis */
     979          18 :             *r++ = '(';
     980          18 :             *r++ = '?';
     981          18 :             *r++ = ':';
     982             :         }
     983         436 :         else if (pchar == '\\' || pchar == '.' ||
     984         426 :                  pchar == '^' || pchar == '$')
     985             :         {
     986          10 :             *r++ = '\\';
     987          10 :             *r++ = pchar;
     988             :         }
     989             :         else
     990         426 :             *r++ = pchar;
     991         964 :         p++, plen--;
     992             :     }
     993             : 
     994         126 :     *r++ = ')';
     995         126 :     *r++ = '$';
     996             : 
     997         126 :     SET_VARSIZE(result, r - ((char *) result));
     998             : 
     999         126 :     return result;
    1000             : }
    1001             : 
    1002             : /*
    1003             :  * similar_to_escape(pattern, escape)
    1004             :  */
    1005             : Datum
    1006          98 : similar_to_escape_2(PG_FUNCTION_ARGS)
    1007             : {
    1008          98 :     text       *pat_text = PG_GETARG_TEXT_PP(0);
    1009          98 :     text       *esc_text = PG_GETARG_TEXT_PP(1);
    1010             :     text       *result;
    1011             : 
    1012          98 :     result = similar_escape_internal(pat_text, esc_text);
    1013             : 
    1014          86 :     PG_RETURN_TEXT_P(result);
    1015             : }
    1016             : 
    1017             : /*
    1018             :  * similar_to_escape(pattern)
    1019             :  * Inserts a default escape character.
    1020             :  */
    1021             : Datum
    1022          40 : similar_to_escape_1(PG_FUNCTION_ARGS)
    1023             : {
    1024          40 :     text       *pat_text = PG_GETARG_TEXT_PP(0);
    1025             :     text       *result;
    1026             : 
    1027          40 :     result = similar_escape_internal(pat_text, NULL);
    1028             : 
    1029          40 :     PG_RETURN_TEXT_P(result);
    1030             : }
    1031             : 
    1032             : /*
    1033             :  * similar_escape(pattern, escape)
    1034             :  *
    1035             :  * Legacy function for compatibility with views stored using the
    1036             :  * pre-v13 expansion of SIMILAR TO.  Unlike the above functions, this
    1037             :  * is non-strict, which leads to not-per-spec handling of "ESCAPE NULL".
    1038             :  */
    1039             : Datum
    1040           0 : similar_escape(PG_FUNCTION_ARGS)
    1041             : {
    1042             :     text       *pat_text;
    1043             :     text       *esc_text;
    1044             :     text       *result;
    1045             : 
    1046             :     /* This function is not strict, so must test explicitly */
    1047           0 :     if (PG_ARGISNULL(0))
    1048           0 :         PG_RETURN_NULL();
    1049           0 :     pat_text = PG_GETARG_TEXT_PP(0);
    1050             : 
    1051           0 :     if (PG_ARGISNULL(1))
    1052           0 :         esc_text = NULL;        /* use default escape character */
    1053             :     else
    1054           0 :         esc_text = PG_GETARG_TEXT_PP(1);
    1055             : 
    1056           0 :     result = similar_escape_internal(pat_text, esc_text);
    1057             : 
    1058           0 :     PG_RETURN_TEXT_P(result);
    1059             : }
    1060             : 
    1061             : /*
    1062             :  * regexp_count()
    1063             :  *      Return the number of matches of a pattern within a string.
    1064             :  */
    1065             : Datum
    1066          48 : regexp_count(PG_FUNCTION_ARGS)
    1067             : {
    1068          48 :     text       *str = PG_GETARG_TEXT_PP(0);
    1069          48 :     text       *pattern = PG_GETARG_TEXT_PP(1);
    1070          48 :     int         start = 1;
    1071          48 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(3);
    1072             :     pg_re_flags re_flags;
    1073             :     regexp_matches_ctx *matchctx;
    1074             : 
    1075             :     /* Collect optional parameters */
    1076          48 :     if (PG_NARGS() > 2)
    1077             :     {
    1078          42 :         start = PG_GETARG_INT32(2);
    1079          42 :         if (start <= 0)
    1080          12 :             ereport(ERROR,
    1081             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1082             :                      errmsg("invalid value for parameter \"%s\": %d",
    1083             :                             "start", start)));
    1084             :     }
    1085             : 
    1086             :     /* Determine options */
    1087          36 :     parse_re_flags(&re_flags, flags);
    1088             :     /* User mustn't specify 'g' */
    1089          36 :     if (re_flags.glob)
    1090           0 :         ereport(ERROR,
    1091             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1092             :         /* translator: %s is a SQL function name */
    1093             :                  errmsg("%s does not support the \"global\" option",
    1094             :                         "regexp_count()")));
    1095             :     /* But we find all the matches anyway */
    1096          36 :     re_flags.glob = true;
    1097             : 
    1098             :     /* Do the matching */
    1099          36 :     matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
    1100             :                                     PG_GET_COLLATION(),
    1101             :                                     false,  /* can ignore subexprs */
    1102             :                                     false, false);
    1103             : 
    1104          36 :     PG_RETURN_INT32(matchctx->nmatches);
    1105             : }
    1106             : 
    1107             : /* This is separate to keep the opr_sanity regression test from complaining */
    1108             : Datum
    1109           6 : regexp_count_no_start(PG_FUNCTION_ARGS)
    1110             : {
    1111           6 :     return regexp_count(fcinfo);
    1112             : }
    1113             : 
    1114             : /* This is separate to keep the opr_sanity regression test from complaining */
    1115             : Datum
    1116          30 : regexp_count_no_flags(PG_FUNCTION_ARGS)
    1117             : {
    1118          30 :     return regexp_count(fcinfo);
    1119             : }
    1120             : 
    1121             : /*
    1122             :  * regexp_instr()
    1123             :  *      Return the match's position within the string
    1124             :  */
    1125             : Datum
    1126         156 : regexp_instr(PG_FUNCTION_ARGS)
    1127             : {
    1128         156 :     text       *str = PG_GETARG_TEXT_PP(0);
    1129         156 :     text       *pattern = PG_GETARG_TEXT_PP(1);
    1130         156 :     int         start = 1;
    1131         156 :     int         n = 1;
    1132         156 :     int         endoption = 0;
    1133         156 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
    1134         156 :     int         subexpr = 0;
    1135             :     int         pos;
    1136             :     pg_re_flags re_flags;
    1137             :     regexp_matches_ctx *matchctx;
    1138             : 
    1139             :     /* Collect optional parameters */
    1140         156 :     if (PG_NARGS() > 2)
    1141             :     {
    1142         138 :         start = PG_GETARG_INT32(2);
    1143         138 :         if (start <= 0)
    1144           6 :             ereport(ERROR,
    1145             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1146             :                      errmsg("invalid value for parameter \"%s\": %d",
    1147             :                             "start", start)));
    1148             :     }
    1149         150 :     if (PG_NARGS() > 3)
    1150             :     {
    1151         126 :         n = PG_GETARG_INT32(3);
    1152         126 :         if (n <= 0)
    1153           6 :             ereport(ERROR,
    1154             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1155             :                      errmsg("invalid value for parameter \"%s\": %d",
    1156             :                             "n", n)));
    1157             :     }
    1158         144 :     if (PG_NARGS() > 4)
    1159             :     {
    1160         108 :         endoption = PG_GETARG_INT32(4);
    1161         108 :         if (endoption != 0 && endoption != 1)
    1162          12 :             ereport(ERROR,
    1163             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1164             :                      errmsg("invalid value for parameter \"%s\": %d",
    1165             :                             "endoption", endoption)));
    1166             :     }
    1167         132 :     if (PG_NARGS() > 6)
    1168             :     {
    1169          84 :         subexpr = PG_GETARG_INT32(6);
    1170          84 :         if (subexpr < 0)
    1171           6 :             ereport(ERROR,
    1172             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1173             :                      errmsg("invalid value for parameter \"%s\": %d",
    1174             :                             "subexpr", subexpr)));
    1175             :     }
    1176             : 
    1177             :     /* Determine options */
    1178         126 :     parse_re_flags(&re_flags, flags);
    1179             :     /* User mustn't specify 'g' */
    1180         126 :     if (re_flags.glob)
    1181           6 :         ereport(ERROR,
    1182             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1183             :         /* translator: %s is a SQL function name */
    1184             :                  errmsg("%s does not support the \"global\" option",
    1185             :                         "regexp_instr()")));
    1186             :     /* But we find all the matches anyway */
    1187         120 :     re_flags.glob = true;
    1188             : 
    1189             :     /* Do the matching */
    1190         120 :     matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
    1191             :                                     PG_GET_COLLATION(),
    1192             :                                     (subexpr > 0),   /* need submatches? */
    1193             :                                     false, false);
    1194             : 
    1195             :     /* When n exceeds matches return 0 (includes case of no matches) */
    1196         120 :     if (n > matchctx->nmatches)
    1197          12 :         PG_RETURN_INT32(0);
    1198             : 
    1199             :     /* When subexpr exceeds number of subexpressions return 0 */
    1200         108 :     if (subexpr > matchctx->npatterns)
    1201          12 :         PG_RETURN_INT32(0);
    1202             : 
    1203             :     /* Select the appropriate match position to return */
    1204          96 :     pos = (n - 1) * matchctx->npatterns;
    1205          96 :     if (subexpr > 0)
    1206          54 :         pos += subexpr - 1;
    1207          96 :     pos *= 2;
    1208          96 :     if (endoption == 1)
    1209          30 :         pos += 1;
    1210             : 
    1211          96 :     if (matchctx->match_locs[pos] >= 0)
    1212          90 :         PG_RETURN_INT32(matchctx->match_locs[pos] + 1);
    1213             :     else
    1214           6 :         PG_RETURN_INT32(0);     /* position not identifiable */
    1215             : }
    1216             : 
    1217             : /* This is separate to keep the opr_sanity regression test from complaining */
    1218             : Datum
    1219          18 : regexp_instr_no_start(PG_FUNCTION_ARGS)
    1220             : {
    1221          18 :     return regexp_instr(fcinfo);
    1222             : }
    1223             : 
    1224             : /* This is separate to keep the opr_sanity regression test from complaining */
    1225             : Datum
    1226           6 : regexp_instr_no_n(PG_FUNCTION_ARGS)
    1227             : {
    1228           6 :     return regexp_instr(fcinfo);
    1229             : }
    1230             : 
    1231             : /* This is separate to keep the opr_sanity regression test from complaining */
    1232             : Datum
    1233          24 : regexp_instr_no_endoption(PG_FUNCTION_ARGS)
    1234             : {
    1235          24 :     return regexp_instr(fcinfo);
    1236             : }
    1237             : 
    1238             : /* This is separate to keep the opr_sanity regression test from complaining */
    1239             : Datum
    1240          12 : regexp_instr_no_flags(PG_FUNCTION_ARGS)
    1241             : {
    1242          12 :     return regexp_instr(fcinfo);
    1243             : }
    1244             : 
    1245             : /* This is separate to keep the opr_sanity regression test from complaining */
    1246             : Datum
    1247          12 : regexp_instr_no_subexpr(PG_FUNCTION_ARGS)
    1248             : {
    1249          12 :     return regexp_instr(fcinfo);
    1250             : }
    1251             : 
    1252             : /*
    1253             :  * regexp_like()
    1254             :  *      Test for a pattern match within a string.
    1255             :  */
    1256             : Datum
    1257          30 : regexp_like(PG_FUNCTION_ARGS)
    1258             : {
    1259          30 :     text       *str = PG_GETARG_TEXT_PP(0);
    1260          30 :     text       *pattern = PG_GETARG_TEXT_PP(1);
    1261          30 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
    1262             :     pg_re_flags re_flags;
    1263             : 
    1264             :     /* Determine options */
    1265          30 :     parse_re_flags(&re_flags, flags);
    1266             :     /* User mustn't specify 'g' */
    1267          30 :     if (re_flags.glob)
    1268           6 :         ereport(ERROR,
    1269             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1270             :         /* translator: %s is a SQL function name */
    1271             :                  errmsg("%s does not support the \"global\" option",
    1272             :                         "regexp_like()")));
    1273             : 
    1274             :     /* Otherwise it's like textregexeq/texticregexeq */
    1275          24 :     PG_RETURN_BOOL(RE_compile_and_execute(pattern,
    1276             :                                           VARDATA_ANY(str),
    1277             :                                           VARSIZE_ANY_EXHDR(str),
    1278             :                                           re_flags.cflags,
    1279             :                                           PG_GET_COLLATION(),
    1280             :                                           0, NULL));
    1281             : }
    1282             : 
    1283             : /* This is separate to keep the opr_sanity regression test from complaining */
    1284             : Datum
    1285           6 : regexp_like_no_flags(PG_FUNCTION_ARGS)
    1286             : {
    1287           6 :     return regexp_like(fcinfo);
    1288             : }
    1289             : 
    1290             : /*
    1291             :  * regexp_match()
    1292             :  *      Return the first substring(s) matching a pattern within a string.
    1293             :  */
    1294             : Datum
    1295        2556 : regexp_match(PG_FUNCTION_ARGS)
    1296             : {
    1297        2556 :     text       *orig_str = PG_GETARG_TEXT_PP(0);
    1298        2556 :     text       *pattern = PG_GETARG_TEXT_PP(1);
    1299        2556 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
    1300             :     pg_re_flags re_flags;
    1301             :     regexp_matches_ctx *matchctx;
    1302             : 
    1303             :     /* Determine options */
    1304        2556 :     parse_re_flags(&re_flags, flags);
    1305             :     /* User mustn't specify 'g' */
    1306        2556 :     if (re_flags.glob)
    1307           8 :         ereport(ERROR,
    1308             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1309             :         /* translator: %s is a SQL function name */
    1310             :                  errmsg("%s does not support the \"global\" option",
    1311             :                         "regexp_match()"),
    1312             :                  errhint("Use the regexp_matches function instead.")));
    1313             : 
    1314        2548 :     matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, 0,
    1315             :                                     PG_GET_COLLATION(), true, false, false);
    1316             : 
    1317        2548 :     if (matchctx->nmatches == 0)
    1318         170 :         PG_RETURN_NULL();
    1319             : 
    1320             :     Assert(matchctx->nmatches == 1);
    1321             : 
    1322             :     /* Create workspace that build_regexp_match_result needs */
    1323        2378 :     matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
    1324        2378 :     matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
    1325             : 
    1326        2378 :     PG_RETURN_DATUM(PointerGetDatum(build_regexp_match_result(matchctx)));
    1327             : }
    1328             : 
    1329             : /* This is separate to keep the opr_sanity regression test from complaining */
    1330             : Datum
    1331        2526 : regexp_match_no_flags(PG_FUNCTION_ARGS)
    1332             : {
    1333        2526 :     return regexp_match(fcinfo);
    1334             : }
    1335             : 
    1336             : /*
    1337             :  * regexp_matches()
    1338             :  *      Return a table of all matches of a pattern within a string.
    1339             :  */
    1340             : Datum
    1341         678 : regexp_matches(PG_FUNCTION_ARGS)
    1342             : {
    1343             :     FuncCallContext *funcctx;
    1344             :     regexp_matches_ctx *matchctx;
    1345             : 
    1346         678 :     if (SRF_IS_FIRSTCALL())
    1347             :     {
    1348         288 :         text       *pattern = PG_GETARG_TEXT_PP(1);
    1349         288 :         text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
    1350             :         pg_re_flags re_flags;
    1351             :         MemoryContext oldcontext;
    1352             : 
    1353         288 :         funcctx = SRF_FIRSTCALL_INIT();
    1354         288 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
    1355             : 
    1356             :         /* Determine options */
    1357         288 :         parse_re_flags(&re_flags, flags);
    1358             : 
    1359             :         /* be sure to copy the input string into the multi-call ctx */
    1360         282 :         matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
    1361             :                                         &re_flags, 0,
    1362             :                                         PG_GET_COLLATION(),
    1363             :                                         true, false, false);
    1364             : 
    1365             :         /* Pre-create workspace that build_regexp_match_result needs */
    1366         270 :         matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
    1367         270 :         matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
    1368             : 
    1369         270 :         MemoryContextSwitchTo(oldcontext);
    1370         270 :         funcctx->user_fctx = (void *) matchctx;
    1371             :     }
    1372             : 
    1373         660 :     funcctx = SRF_PERCALL_SETUP();
    1374         660 :     matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
    1375             : 
    1376         660 :     if (matchctx->next_match < matchctx->nmatches)
    1377             :     {
    1378             :         ArrayType  *result_ary;
    1379             : 
    1380         390 :         result_ary = build_regexp_match_result(matchctx);
    1381         390 :         matchctx->next_match++;
    1382         390 :         SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
    1383             :     }
    1384             : 
    1385         270 :     SRF_RETURN_DONE(funcctx);
    1386             : }
    1387             : 
    1388             : /* This is separate to keep the opr_sanity regression test from complaining */
    1389             : Datum
    1390         354 : regexp_matches_no_flags(PG_FUNCTION_ARGS)
    1391             : {
    1392         354 :     return regexp_matches(fcinfo);
    1393             : }
    1394             : 
    1395             : /*
    1396             :  * setup_regexp_matches --- do the initial matching for regexp_match,
    1397             :  *      regexp_split, and related functions
    1398             :  *
    1399             :  * To avoid having to re-find the compiled pattern on each call, we do
    1400             :  * all the matching in one swoop.  The returned regexp_matches_ctx contains
    1401             :  * the locations of all the substrings matching the pattern.
    1402             :  *
    1403             :  * start_search: the character (not byte) offset in orig_str at which to
    1404             :  * begin the search.  Returned positions are relative to orig_str anyway.
    1405             :  * use_subpatterns: collect data about matches to parenthesized subexpressions.
    1406             :  * ignore_degenerate: ignore zero-length matches.
    1407             :  * fetching_unmatched: caller wants to fetch unmatched substrings.
    1408             :  *
    1409             :  * We don't currently assume that fetching_unmatched is exclusive of fetching
    1410             :  * the matched text too; if it's set, the conversion buffer is large enough to
    1411             :  * fetch any single matched or unmatched string, but not any larger
    1412             :  * substring.  (In practice, when splitting the matches are usually small
    1413             :  * anyway, and it didn't seem worth complicating the code further.)
    1414             :  */
    1415             : static regexp_matches_ctx *
    1416      203522 : setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *re_flags,
    1417             :                      int start_search,
    1418             :                      Oid collation,
    1419             :                      bool use_subpatterns,
    1420             :                      bool ignore_degenerate,
    1421             :                      bool fetching_unmatched)
    1422             : {
    1423      203522 :     regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
    1424      203522 :     int         eml = pg_database_encoding_max_length();
    1425             :     int         orig_len;
    1426             :     pg_wchar   *wide_str;
    1427             :     int         wide_len;
    1428             :     int         cflags;
    1429             :     regex_t    *cpattern;
    1430             :     regmatch_t *pmatch;
    1431             :     int         pmatch_len;
    1432             :     int         array_len;
    1433             :     int         array_idx;
    1434             :     int         prev_match_end;
    1435             :     int         prev_valid_match_end;
    1436      203522 :     int         maxlen = 0;     /* largest fetch length in characters */
    1437             : 
    1438             :     /* save original string --- we'll extract result substrings from it */
    1439      203522 :     matchctx->orig_str = orig_str;
    1440             : 
    1441             :     /* convert string to pg_wchar form for matching */
    1442      203522 :     orig_len = VARSIZE_ANY_EXHDR(orig_str);
    1443      203522 :     wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
    1444      203522 :     wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
    1445             : 
    1446             :     /* set up the compiled pattern */
    1447      203522 :     cflags = re_flags->cflags;
    1448      203522 :     if (!use_subpatterns)
    1449      200590 :         cflags |= REG_NOSUB;
    1450      203522 :     cpattern = RE_compile_and_cache(pattern, cflags, collation);
    1451             : 
    1452             :     /* do we want to remember subpatterns? */
    1453      203510 :     if (use_subpatterns && cpattern->re_nsub > 0)
    1454             :     {
    1455        2710 :         matchctx->npatterns = cpattern->re_nsub;
    1456        2710 :         pmatch_len = cpattern->re_nsub + 1;
    1457             :     }
    1458             :     else
    1459             :     {
    1460      200800 :         use_subpatterns = false;
    1461      200800 :         matchctx->npatterns = 1;
    1462      200800 :         pmatch_len = 1;
    1463             :     }
    1464             : 
    1465             :     /* temporary output space for RE package */
    1466      203510 :     pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
    1467             : 
    1468             :     /*
    1469             :      * the real output space (grown dynamically if needed)
    1470             :      *
    1471             :      * use values 2^n-1, not 2^n, so that we hit the limit at 2^28-1 rather
    1472             :      * than at 2^27
    1473             :      */
    1474      203510 :     array_len = re_flags->glob ? 255 : 31;
    1475      203510 :     matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
    1476      203510 :     array_idx = 0;
    1477             : 
    1478             :     /* search for the pattern, perhaps repeatedly */
    1479      203510 :     prev_match_end = 0;
    1480      203510 :     prev_valid_match_end = 0;
    1481     1095318 :     while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
    1482             :                             pmatch_len, pmatch))
    1483             :     {
    1484             :         /*
    1485             :          * If requested, ignore degenerate matches, which are zero-length
    1486             :          * matches occurring at the start or end of a string or just after a
    1487             :          * previous match.
    1488             :          */
    1489      894422 :         if (!ignore_degenerate ||
    1490      891258 :             (pmatch[0].rm_so < wide_len &&
    1491      891216 :              pmatch[0].rm_eo > prev_match_end))
    1492             :         {
    1493             :             /* enlarge output space if needed */
    1494      894602 :             while (array_idx + matchctx->npatterns * 2 + 1 > array_len)
    1495             :             {
    1496         360 :                 array_len += array_len + 1; /* 2^n-1 => 2^(n+1)-1 */
    1497         360 :                 if (array_len > MaxAllocSize / sizeof(int))
    1498           0 :                     ereport(ERROR,
    1499             :                             (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    1500             :                              errmsg("too many regular expression matches")));
    1501         360 :                 matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
    1502             :                                                         sizeof(int) * array_len);
    1503             :             }
    1504             : 
    1505             :             /* save this match's locations */
    1506      894242 :             if (use_subpatterns)
    1507             :             {
    1508             :                 int         i;
    1509             : 
    1510        7872 :                 for (i = 1; i <= matchctx->npatterns; i++)
    1511             :                 {
    1512        5314 :                     int         so = pmatch[i].rm_so;
    1513        5314 :                     int         eo = pmatch[i].rm_eo;
    1514             : 
    1515        5314 :                     matchctx->match_locs[array_idx++] = so;
    1516        5314 :                     matchctx->match_locs[array_idx++] = eo;
    1517        5314 :                     if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
    1518        3420 :                         maxlen = (eo - so);
    1519             :                 }
    1520             :             }
    1521             :             else
    1522             :             {
    1523      891684 :                 int         so = pmatch[0].rm_so;
    1524      891684 :                 int         eo = pmatch[0].rm_eo;
    1525             : 
    1526      891684 :                 matchctx->match_locs[array_idx++] = so;
    1527      891684 :                 matchctx->match_locs[array_idx++] = eo;
    1528      891684 :                 if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
    1529      200634 :                     maxlen = (eo - so);
    1530             :             }
    1531      894242 :             matchctx->nmatches++;
    1532             : 
    1533             :             /*
    1534             :              * check length of unmatched portion between end of previous valid
    1535             :              * (nondegenerate, or degenerate but not ignored) match and start
    1536             :              * of current one
    1537             :              */
    1538      894242 :             if (fetching_unmatched &&
    1539      891078 :                 pmatch[0].rm_so >= 0 &&
    1540      891078 :                 (pmatch[0].rm_so - prev_valid_match_end) > maxlen)
    1541      380878 :                 maxlen = (pmatch[0].rm_so - prev_valid_match_end);
    1542      894242 :             prev_valid_match_end = pmatch[0].rm_eo;
    1543             :         }
    1544      894422 :         prev_match_end = pmatch[0].rm_eo;
    1545             : 
    1546             :         /* if not glob, stop after one match */
    1547      894422 :         if (!re_flags->glob)
    1548        2548 :             break;
    1549             : 
    1550             :         /*
    1551             :          * Advance search position.  Normally we start the next search at the
    1552             :          * end of the previous match; but if the match was of zero length, we
    1553             :          * have to advance by one character, or we'd just find the same match
    1554             :          * again.
    1555             :          */
    1556      891874 :         start_search = prev_match_end;
    1557      891874 :         if (pmatch[0].rm_so == pmatch[0].rm_eo)
    1558        1176 :             start_search++;
    1559      891874 :         if (start_search > wide_len)
    1560          66 :             break;
    1561             :     }
    1562             : 
    1563             :     /*
    1564             :      * check length of unmatched portion between end of last match and end of
    1565             :      * input string
    1566             :      */
    1567      203510 :     if (fetching_unmatched &&
    1568      200452 :         (wide_len - prev_valid_match_end) > maxlen)
    1569          76 :         maxlen = (wide_len - prev_valid_match_end);
    1570             : 
    1571             :     /*
    1572             :      * Keep a note of the end position of the string for the benefit of
    1573             :      * splitting code.
    1574             :      */
    1575      203510 :     matchctx->match_locs[array_idx] = wide_len;
    1576             : 
    1577      203510 :     if (eml > 1)
    1578             :     {
    1579      203510 :         int64       maxsiz = eml * (int64) maxlen;
    1580             :         int         conv_bufsiz;
    1581             : 
    1582             :         /*
    1583             :          * Make the conversion buffer large enough for any substring of
    1584             :          * interest.
    1585             :          *
    1586             :          * Worst case: assume we need the maximum size (maxlen*eml), but take
    1587             :          * advantage of the fact that the original string length in bytes is
    1588             :          * an upper bound on the byte length of any fetched substring (and we
    1589             :          * know that len+1 is safe to allocate because the varlena header is
    1590             :          * longer than 1 byte).
    1591             :          */
    1592      203510 :         if (maxsiz > orig_len)
    1593      200548 :             conv_bufsiz = orig_len + 1;
    1594             :         else
    1595        2962 :             conv_bufsiz = maxsiz + 1;   /* safe since maxsiz < 2^30 */
    1596             : 
    1597      203510 :         matchctx->conv_buf = palloc(conv_bufsiz);
    1598      203510 :         matchctx->conv_bufsiz = conv_bufsiz;
    1599      203510 :         matchctx->wide_str = wide_str;
    1600             :     }
    1601             :     else
    1602             :     {
    1603             :         /* No need to keep the wide string if we're in a single-byte charset. */
    1604           0 :         pfree(wide_str);
    1605           0 :         matchctx->wide_str = NULL;
    1606           0 :         matchctx->conv_buf = NULL;
    1607           0 :         matchctx->conv_bufsiz = 0;
    1608             :     }
    1609             : 
    1610             :     /* Clean up temp storage */
    1611      203510 :     pfree(pmatch);
    1612             : 
    1613      203510 :     return matchctx;
    1614             : }
    1615             : 
    1616             : /*
    1617             :  * build_regexp_match_result - build output array for current match
    1618             :  */
    1619             : static ArrayType *
    1620        2768 : build_regexp_match_result(regexp_matches_ctx *matchctx)
    1621             : {
    1622        2768 :     char       *buf = matchctx->conv_buf;
    1623        2768 :     Datum      *elems = matchctx->elems;
    1624        2768 :     bool       *nulls = matchctx->nulls;
    1625             :     int         dims[1];
    1626             :     int         lbs[1];
    1627             :     int         loc;
    1628             :     int         i;
    1629             : 
    1630             :     /* Extract matching substrings from the original string */
    1631        2768 :     loc = matchctx->next_match * matchctx->npatterns * 2;
    1632        8022 :     for (i = 0; i < matchctx->npatterns; i++)
    1633             :     {
    1634        5254 :         int         so = matchctx->match_locs[loc++];
    1635        5254 :         int         eo = matchctx->match_locs[loc++];
    1636             : 
    1637        5254 :         if (so < 0 || eo < 0)
    1638             :         {
    1639           6 :             elems[i] = (Datum) 0;
    1640           6 :             nulls[i] = true;
    1641             :         }
    1642        5248 :         else if (buf)
    1643             :         {
    1644        5248 :             int         len = pg_wchar2mb_with_len(matchctx->wide_str + so,
    1645             :                                                    buf,
    1646             :                                                    eo - so);
    1647             : 
    1648             :             Assert(len < matchctx->conv_bufsiz);
    1649        5248 :             elems[i] = PointerGetDatum(cstring_to_text_with_len(buf, len));
    1650        5248 :             nulls[i] = false;
    1651             :         }
    1652             :         else
    1653             :         {
    1654           0 :             elems[i] = DirectFunctionCall3(text_substr,
    1655             :                                            PointerGetDatum(matchctx->orig_str),
    1656             :                                            Int32GetDatum(so + 1),
    1657             :                                            Int32GetDatum(eo - so));
    1658           0 :             nulls[i] = false;
    1659             :         }
    1660             :     }
    1661             : 
    1662             :     /* And form an array */
    1663        2768 :     dims[0] = matchctx->npatterns;
    1664        2768 :     lbs[0] = 1;
    1665             :     /* XXX: this hardcodes assumptions about the text type */
    1666        2768 :     return construct_md_array(elems, nulls, 1, dims, lbs,
    1667             :                               TEXTOID, -1, false, TYPALIGN_INT);
    1668             : }
    1669             : 
    1670             : /*
    1671             :  * regexp_split_to_table()
    1672             :  *      Split the string at matches of the pattern, returning the
    1673             :  *      split-out substrings as a table.
    1674             :  */
    1675             : Datum
    1676         622 : regexp_split_to_table(PG_FUNCTION_ARGS)
    1677             : {
    1678             :     FuncCallContext *funcctx;
    1679             :     regexp_matches_ctx *splitctx;
    1680             : 
    1681         622 :     if (SRF_IS_FIRSTCALL())
    1682             :     {
    1683          52 :         text       *pattern = PG_GETARG_TEXT_PP(1);
    1684          52 :         text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
    1685             :         pg_re_flags re_flags;
    1686             :         MemoryContext oldcontext;
    1687             : 
    1688          52 :         funcctx = SRF_FIRSTCALL_INIT();
    1689          52 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
    1690             : 
    1691             :         /* Determine options */
    1692          52 :         parse_re_flags(&re_flags, flags);
    1693             :         /* User mustn't specify 'g' */
    1694          46 :         if (re_flags.glob)
    1695           6 :             ereport(ERROR,
    1696             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1697             :             /* translator: %s is a SQL function name */
    1698             :                      errmsg("%s does not support the \"global\" option",
    1699             :                             "regexp_split_to_table()")));
    1700             :         /* But we find all the matches anyway */
    1701          40 :         re_flags.glob = true;
    1702             : 
    1703             :         /* be sure to copy the input string into the multi-call ctx */
    1704          40 :         splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
    1705             :                                         &re_flags, 0,
    1706             :                                         PG_GET_COLLATION(),
    1707             :                                         false, true, true);
    1708             : 
    1709          40 :         MemoryContextSwitchTo(oldcontext);
    1710          40 :         funcctx->user_fctx = (void *) splitctx;
    1711             :     }
    1712             : 
    1713         610 :     funcctx = SRF_PERCALL_SETUP();
    1714         610 :     splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
    1715             : 
    1716         610 :     if (splitctx->next_match <= splitctx->nmatches)
    1717             :     {
    1718         570 :         Datum       result = build_regexp_split_result(splitctx);
    1719             : 
    1720         570 :         splitctx->next_match++;
    1721         570 :         SRF_RETURN_NEXT(funcctx, result);
    1722             :     }
    1723             : 
    1724          40 :     SRF_RETURN_DONE(funcctx);
    1725             : }
    1726             : 
    1727             : /* This is separate to keep the opr_sanity regression test from complaining */
    1728             : Datum
    1729         552 : regexp_split_to_table_no_flags(PG_FUNCTION_ARGS)
    1730             : {
    1731         552 :     return regexp_split_to_table(fcinfo);
    1732             : }
    1733             : 
    1734             : /*
    1735             :  * regexp_split_to_array()
    1736             :  *      Split the string at matches of the pattern, returning the
    1737             :  *      split-out substrings as an array.
    1738             :  */
    1739             : Datum
    1740      200424 : regexp_split_to_array(PG_FUNCTION_ARGS)
    1741             : {
    1742      200424 :     ArrayBuildState *astate = NULL;
    1743             :     pg_re_flags re_flags;
    1744             :     regexp_matches_ctx *splitctx;
    1745             : 
    1746             :     /* Determine options */
    1747      200424 :     parse_re_flags(&re_flags, PG_GETARG_TEXT_PP_IF_EXISTS(2));
    1748             :     /* User mustn't specify 'g' */
    1749      200418 :     if (re_flags.glob)
    1750           6 :         ereport(ERROR,
    1751             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1752             :         /* translator: %s is a SQL function name */
    1753             :                  errmsg("%s does not support the \"global\" option",
    1754             :                         "regexp_split_to_array()")));
    1755             :     /* But we find all the matches anyway */
    1756      200412 :     re_flags.glob = true;
    1757             : 
    1758      200412 :     splitctx = setup_regexp_matches(PG_GETARG_TEXT_PP(0),
    1759      200412 :                                     PG_GETARG_TEXT_PP(1),
    1760             :                                     &re_flags, 0,
    1761             :                                     PG_GET_COLLATION(),
    1762             :                                     false, true, true);
    1763             : 
    1764     1291372 :     while (splitctx->next_match <= splitctx->nmatches)
    1765             :     {
    1766     1090960 :         astate = accumArrayResult(astate,
    1767             :                                   build_regexp_split_result(splitctx),
    1768             :                                   false,
    1769             :                                   TEXTOID,
    1770             :                                   CurrentMemoryContext);
    1771     1090960 :         splitctx->next_match++;
    1772             :     }
    1773             : 
    1774      200412 :     PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
    1775             : }
    1776             : 
    1777             : /* This is separate to keep the opr_sanity regression test from complaining */
    1778             : Datum
    1779      200382 : regexp_split_to_array_no_flags(PG_FUNCTION_ARGS)
    1780             : {
    1781      200382 :     return regexp_split_to_array(fcinfo);
    1782             : }
    1783             : 
    1784             : /*
    1785             :  * build_regexp_split_result - build output string for current match
    1786             :  *
    1787             :  * We return the string between the current match and the previous one,
    1788             :  * or the string after the last match when next_match == nmatches.
    1789             :  */
    1790             : static Datum
    1791     1091530 : build_regexp_split_result(regexp_matches_ctx *splitctx)
    1792             : {
    1793     1091530 :     char       *buf = splitctx->conv_buf;
    1794             :     int         startpos;
    1795             :     int         endpos;
    1796             : 
    1797     1091530 :     if (splitctx->next_match > 0)
    1798      891078 :         startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
    1799             :     else
    1800      200452 :         startpos = 0;
    1801     1091530 :     if (startpos < 0)
    1802           0 :         elog(ERROR, "invalid match ending position");
    1803             : 
    1804     1091530 :     endpos = splitctx->match_locs[splitctx->next_match * 2];
    1805     1091530 :     if (endpos < startpos)
    1806           0 :         elog(ERROR, "invalid match starting position");
    1807             : 
    1808     1091530 :     if (buf)
    1809             :     {
    1810             :         int         len;
    1811             : 
    1812     1091530 :         len = pg_wchar2mb_with_len(splitctx->wide_str + startpos,
    1813             :                                    buf,
    1814             :                                    endpos - startpos);
    1815             :         Assert(len < splitctx->conv_bufsiz);
    1816     1091530 :         return PointerGetDatum(cstring_to_text_with_len(buf, len));
    1817             :     }
    1818             :     else
    1819             :     {
    1820           0 :         return DirectFunctionCall3(text_substr,
    1821             :                                    PointerGetDatum(splitctx->orig_str),
    1822             :                                    Int32GetDatum(startpos + 1),
    1823             :                                    Int32GetDatum(endpos - startpos));
    1824             :     }
    1825             : }
    1826             : 
    1827             : /*
    1828             :  * regexp_substr()
    1829             :  *      Return the substring that matches a regular expression pattern
    1830             :  */
    1831             : Datum
    1832         108 : regexp_substr(PG_FUNCTION_ARGS)
    1833             : {
    1834         108 :     text       *str = PG_GETARG_TEXT_PP(0);
    1835         108 :     text       *pattern = PG_GETARG_TEXT_PP(1);
    1836         108 :     int         start = 1;
    1837         108 :     int         n = 1;
    1838         108 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(4);
    1839         108 :     int         subexpr = 0;
    1840             :     int         so,
    1841             :                 eo,
    1842             :                 pos;
    1843             :     pg_re_flags re_flags;
    1844             :     regexp_matches_ctx *matchctx;
    1845             : 
    1846             :     /* Collect optional parameters */
    1847         108 :     if (PG_NARGS() > 2)
    1848             :     {
    1849          90 :         start = PG_GETARG_INT32(2);
    1850          90 :         if (start <= 0)
    1851           6 :             ereport(ERROR,
    1852             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1853             :                      errmsg("invalid value for parameter \"%s\": %d",
    1854             :                             "start", start)));
    1855             :     }
    1856         102 :     if (PG_NARGS() > 3)
    1857             :     {
    1858          78 :         n = PG_GETARG_INT32(3);
    1859          78 :         if (n <= 0)
    1860           6 :             ereport(ERROR,
    1861             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1862             :                      errmsg("invalid value for parameter \"%s\": %d",
    1863             :                             "n", n)));
    1864             :     }
    1865          96 :     if (PG_NARGS() > 5)
    1866             :     {
    1867          48 :         subexpr = PG_GETARG_INT32(5);
    1868          48 :         if (subexpr < 0)
    1869           6 :             ereport(ERROR,
    1870             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1871             :                      errmsg("invalid value for parameter \"%s\": %d",
    1872             :                             "subexpr", subexpr)));
    1873             :     }
    1874             : 
    1875             :     /* Determine options */
    1876          90 :     parse_re_flags(&re_flags, flags);
    1877             :     /* User mustn't specify 'g' */
    1878          90 :     if (re_flags.glob)
    1879           6 :         ereport(ERROR,
    1880             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1881             :         /* translator: %s is a SQL function name */
    1882             :                  errmsg("%s does not support the \"global\" option",
    1883             :                         "regexp_substr()")));
    1884             :     /* But we find all the matches anyway */
    1885          84 :     re_flags.glob = true;
    1886             : 
    1887             :     /* Do the matching */
    1888          84 :     matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
    1889             :                                     PG_GET_COLLATION(),
    1890             :                                     (subexpr > 0),   /* need submatches? */
    1891             :                                     false, false);
    1892             : 
    1893             :     /* When n exceeds matches return NULL (includes case of no matches) */
    1894          84 :     if (n > matchctx->nmatches)
    1895          12 :         PG_RETURN_NULL();
    1896             : 
    1897             :     /* When subexpr exceeds number of subexpressions return NULL */
    1898          72 :     if (subexpr > matchctx->npatterns)
    1899           6 :         PG_RETURN_NULL();
    1900             : 
    1901             :     /* Select the appropriate match position to return */
    1902          66 :     pos = (n - 1) * matchctx->npatterns;
    1903          66 :     if (subexpr > 0)
    1904          30 :         pos += subexpr - 1;
    1905          66 :     pos *= 2;
    1906          66 :     so = matchctx->match_locs[pos];
    1907          66 :     eo = matchctx->match_locs[pos + 1];
    1908             : 
    1909          66 :     if (so < 0 || eo < 0)
    1910           6 :         PG_RETURN_NULL();       /* unidentifiable location */
    1911             : 
    1912          60 :     PG_RETURN_DATUM(DirectFunctionCall3(text_substr,
    1913             :                                         PointerGetDatum(matchctx->orig_str),
    1914             :                                         Int32GetDatum(so + 1),
    1915             :                                         Int32GetDatum(eo - so)));
    1916             : }
    1917             : 
    1918             : /* This is separate to keep the opr_sanity regression test from complaining */
    1919             : Datum
    1920          18 : regexp_substr_no_start(PG_FUNCTION_ARGS)
    1921             : {
    1922          18 :     return regexp_substr(fcinfo);
    1923             : }
    1924             : 
    1925             : /* This is separate to keep the opr_sanity regression test from complaining */
    1926             : Datum
    1927           6 : regexp_substr_no_n(PG_FUNCTION_ARGS)
    1928             : {
    1929           6 :     return regexp_substr(fcinfo);
    1930             : }
    1931             : 
    1932             : /* This is separate to keep the opr_sanity regression test from complaining */
    1933             : Datum
    1934          24 : regexp_substr_no_flags(PG_FUNCTION_ARGS)
    1935             : {
    1936          24 :     return regexp_substr(fcinfo);
    1937             : }
    1938             : 
    1939             : /* This is separate to keep the opr_sanity regression test from complaining */
    1940             : Datum
    1941          12 : regexp_substr_no_subexpr(PG_FUNCTION_ARGS)
    1942             : {
    1943          12 :     return regexp_substr(fcinfo);
    1944             : }
    1945             : 
    1946             : /*
    1947             :  * regexp_fixed_prefix - extract fixed prefix, if any, for a regexp
    1948             :  *
    1949             :  * The result is NULL if there is no fixed prefix, else a palloc'd string.
    1950             :  * If it is an exact match, not just a prefix, *exact is returned as true.
    1951             :  */
    1952             : char *
    1953       14846 : regexp_fixed_prefix(text *text_re, bool case_insensitive, Oid collation,
    1954             :                     bool *exact)
    1955             : {
    1956             :     char       *result;
    1957             :     regex_t    *re;
    1958             :     int         cflags;
    1959             :     int         re_result;
    1960             :     pg_wchar   *str;
    1961             :     size_t      slen;
    1962             :     size_t      maxlen;
    1963             :     char        errMsg[100];
    1964             : 
    1965       14846 :     *exact = false;             /* default result */
    1966             : 
    1967             :     /* Compile RE */
    1968       14846 :     cflags = REG_ADVANCED;
    1969       14846 :     if (case_insensitive)
    1970          62 :         cflags |= REG_ICASE;
    1971             : 
    1972       14846 :     re = RE_compile_and_cache(text_re, cflags | REG_NOSUB, collation);
    1973             : 
    1974             :     /* Examine it to see if there's a fixed prefix */
    1975       14822 :     re_result = pg_regprefix(re, &str, &slen);
    1976             : 
    1977       14822 :     switch (re_result)
    1978             :     {
    1979         726 :         case REG_NOMATCH:
    1980         726 :             return NULL;
    1981             : 
    1982        1910 :         case REG_PREFIX:
    1983             :             /* continue with wchar conversion */
    1984        1910 :             break;
    1985             : 
    1986       12186 :         case REG_EXACT:
    1987       12186 :             *exact = true;
    1988             :             /* continue with wchar conversion */
    1989       12186 :             break;
    1990             : 
    1991           0 :         default:
    1992             :             /* re failed??? */
    1993           0 :             pg_regerror(re_result, re, errMsg, sizeof(errMsg));
    1994           0 :             ereport(ERROR,
    1995             :                     (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
    1996             :                      errmsg("regular expression failed: %s", errMsg)));
    1997             :             break;
    1998             :     }
    1999             : 
    2000             :     /* Convert pg_wchar result back to database encoding */
    2001       14096 :     maxlen = pg_database_encoding_max_length() * slen + 1;
    2002       14096 :     result = (char *) palloc(maxlen);
    2003       14096 :     slen = pg_wchar2mb_with_len(str, result, slen);
    2004             :     Assert(slen < maxlen);
    2005             : 
    2006       14096 :     pfree(str);
    2007             : 
    2008       14096 :     return result;
    2009             : }

Generated by: LCOV version 1.14