LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - regexp.c (source / functions) Coverage Total Hit LBC UBC GBC CBC EUB ECB
Current: b45a8d7d8b306b43f31a002f1b3f1dddc8defeaf vs 8767b449a3a1e75626dfb08f24da54933171d4c5 Lines: 90.6 % 658 596 62 596
Current Date: 2025-10-28 08:26:42 +0900 Functions: 98.0 % 51 50 1 50
Baseline: lcov-20251028-005825-baseline Branches: 78.1 % 375 293 1 81 1 292 96 48
Baseline Date: 2025-10-27 06:37:35 +0000 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 17 17 17
(360..) days: 90.3 % 641 579 62 579
Function coverage date bins:
(360..) days: 98.0 % 51 50 1 50
Branch coverage date bins:
(30,360] days: 92.9 % 14 13 1 13
(360..) days: 55.4 % 505 280 1 80 1 279 96 48

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * regexp.c
                                  4                 :                :  *    Postgres' interface to the regular expression package.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, 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 *
 5315 tgl@sss.pgh.pa.us         141                 :CBC     3596888 : RE_compile_and_cache(text *text_re, int cflags, Oid collation)
                                142                 :                : {
 6612                           143   [ -  +  -  -  :        3596888 :     int         text_re_len = VARSIZE_ANY_EXHDR(text_re);
                                     -  -  -  -  +  
                                                 + ]
                                144         [ +  + ]:        3596888 :     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                 :                :      */
 8301                           158         [ +  + ]:        3907708 :     for (i = 0; i < num_res; i++)
                                159                 :                :     {
 6612                           160         [ +  + ]:        3904554 :         if (re_array[i].cre_pat_len == text_re_len &&
                                161         [ +  + ]:        3601218 :             re_array[i].cre_flags == cflags &&
 5315                           162         [ +  + ]:        3600619 :             re_array[i].cre_collation == collation &&
 6612                           163         [ +  + ]:        3600431 :             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                 :                :              */
 8301                           168         [ +  + ]:        3593734 :             if (i > 0)
                                169                 :                :             {
                                170                 :         241335 :                 re_temp = re_array[i];
                                171                 :         241335 :                 memmove(&re_array[1], &re_array[0], i * sizeof(cached_re_str));
                                172                 :         241335 :                 re_array[0] = re_temp;
                                173                 :                :             }
                                174                 :                : 
 7315                           175                 :        3593734 :             return &re_array[0].cre_re;
                                176                 :                :         }
                                177                 :                :     }
                                178                 :                : 
                                179                 :                :     /* Set up the cache memory on first go through. */
  934 tmunro@postgresql.or      180         [ +  + ]:           3154 :     if (unlikely(RegexpCacheMemoryContext == NULL))
                                181                 :            788 :         RegexpCacheMemoryContext =
                                182                 :            788 :             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 */
 6612 tgl@sss.pgh.pa.us         192                 :           3154 :     pattern = (pg_wchar *) palloc((text_re_len + 1) * sizeof(pg_wchar));
                                193                 :           3154 :     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                 :                :      */
  934 tmunro@postgresql.or      204                 :           3154 :     re_temp.cre_context = AllocSetContextCreate(CurrentMemoryContext,
                                205                 :                :                                                 "RegexpMemoryContext",
                                206                 :                :                                                 ALLOCSET_SMALL_SIZES);
                                207                 :           3154 :     oldcontext = MemoryContextSwitchTo(re_temp.cre_context);
                                208                 :                : 
 8301 tgl@sss.pgh.pa.us         209                 :           3154 :     regcomp_result = pg_regcomp(&re_temp.cre_re,
                                210                 :                :                                 pattern,
                                211                 :                :                                 pattern_len,
                                212                 :                :                                 cflags,
                                213                 :                :                                 collation);
                                214                 :                : 
                                215                 :           3142 :     pfree(pattern);
                                216                 :                : 
 7643                           217         [ +  + ]:           3142 :     if (regcomp_result != REG_OKAY)
                                218                 :                :     {
                                219                 :                :         /* re didn't compile (no need for pg_regfree, if so) */
 8301                           220                 :             18 :         pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
 8129                           221         [ +  - ]:             18 :         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. */
  934 tmunro@postgresql.or      227                 :           3124 :     re_temp.cre_pat = palloc(text_re_len + 1);
                                228                 :           3124 :     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                 :           3124 :     re_temp.cre_pat[text_re_len] = 0;
                                235                 :           3124 :     MemoryContextSetIdentifier(re_temp.cre_context, re_temp.cre_pat);
                                236                 :                : 
 6612 tgl@sss.pgh.pa.us         237                 :           3124 :     re_temp.cre_pat_len = text_re_len;
 8301                           238                 :           3124 :     re_temp.cre_flags = cflags;
 5315                           239                 :           3124 :     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                 :                :      */
 8301                           245         [ +  + ]:           3124 :     if (num_res >= MAX_CACHED_RES)
                                246                 :                :     {
                                247                 :            444 :         --num_res;
                                248         [ -  + ]:            444 :         Assert(num_res < MAX_CACHED_RES);
                                249                 :                :         /* Delete the memory context holding the regexp and pattern. */
  934 tmunro@postgresql.or      250                 :            444 :         MemoryContextDelete(re_array[num_res].cre_context);
                                251                 :                :     }
                                252                 :                : 
                                253                 :                :     /* Re-parent the memory context to our long-lived cache context. */
                                254                 :           3124 :     MemoryContextSetParent(re_temp.cre_context, RegexpCacheMemoryContext);
                                255                 :                : 
 8301 tgl@sss.pgh.pa.us         256         [ +  + ]:           3124 :     if (num_res > 0)
                                257                 :           2336 :         memmove(&re_array[1], &re_array[0], num_res * sizeof(cached_re_str));
                                258                 :                : 
                                259                 :           3124 :     re_array[0] = re_temp;
                                260                 :           3124 :     num_res++;
                                261                 :                : 
  934 tmunro@postgresql.or      262                 :           3124 :     MemoryContextSwitchTo(oldcontext);
                                263                 :                : 
 7315 tgl@sss.pgh.pa.us         264                 :           3124 :     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
 6797 neilc@samurai.com         282                 :        4025269 : 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 */
 7315 tgl@sss.pgh.pa.us         289                 :        4025269 :     regexec_result = pg_regexec(re,
                                290                 :                :                                 data,
                                291                 :                :                                 data_len,
                                292                 :                :                                 start_search,
                                293                 :                :                                 NULL,   /* no details */
                                294                 :                :                                 nmatch,
                                295                 :                :                                 pmatch,
                                296                 :                :                                 0);
                                297                 :                : 
 7643                           298   [ +  +  -  + ]:        4025269 :     if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
                                299                 :                :     {
                                300                 :                :         /* re failed??? */
 7315 tgl@sss.pgh.pa.us         301                 :UBC           0 :         pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
 7643                           302         [ #  # ]:              0 :         ereport(ERROR,
                                303                 :                :                 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
                                304                 :                :                  errmsg("regular expression failed: %s", errMsg)));
                                305                 :                :     }
                                306                 :                : 
 7643 tgl@sss.pgh.pa.us         307                 :CBC     4025269 :     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
 6797 neilc@samurai.com         324                 :        3476565 : 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                 :        3476565 :     data = (pg_wchar *) palloc((dat_len + 1) * sizeof(pg_wchar));
                                333                 :        3476565 :     data_len = pg_mb2wchar_with_len(dat, data, dat_len);
                                334                 :                : 
                                335                 :                :     /* Perform RE match and return result */
                                336                 :        3476565 :     match = RE_wchar_execute(re, data, data_len, 0, nmatch, pmatch);
                                337                 :                : 
                                338                 :        3476565 :     pfree(data);
                                339                 :        3476565 :     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                 :        3475758 : 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 */
 1541 tgl@sss.pgh.pa.us         365         [ +  - ]:        3475758 :     if (nmatch < 2)
                                366                 :        3475758 :         cflags |= REG_NOSUB;
                                367                 :                : 
                                368                 :                :     /* Compile RE */
 5315                           369                 :        3475758 :     re = RE_compile_and_cache(text_re, cflags, collation);
                                370                 :                : 
 6797 neilc@samurai.com         371                 :        3475746 :     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
 6557 bruce@momjian.us          385                 :         104880 : parse_re_flags(pg_re_flags *flags, text *opts)
                                386                 :                : {
                                387                 :                :     /* regex flavor is always folded into the compile flags */
 5851 tgl@sss.pgh.pa.us         388                 :         104880 :     flags->cflags = REG_ADVANCED;
 6653                           389                 :         104880 :     flags->glob = false;
                                390                 :                : 
 6797 neilc@samurai.com         391         [ +  + ]:         104880 :     if (opts)
                                392                 :                :     {
 6557 bruce@momjian.us          393         [ -  + ]:           2323 :         char       *opt_p = VARDATA_ANY(opts);
                                394   [ -  +  -  -  :           2323 :         int         opt_len = VARSIZE_ANY_EXHDR(opts);
                                     -  -  -  -  -  
                                                 + ]
                                395                 :                :         int         i;
                                396                 :                : 
 6797 neilc@samurai.com         397         [ +  + ]:           5209 :         for (i = 0; i < opt_len; i++)
                                398                 :                :         {
                                399   [ +  -  +  -  :           2898 :             switch (opt_p[i])
                                     +  +  -  -  +  
                                        -  -  +  + ]
                                400                 :                :             {
                                401                 :           2162 :                 case 'g':
                                402                 :           2162 :                     flags->glob = true;
                                403                 :           2162 :                     break;
 6557 bruce@momjian.us          404                 :UBC           0 :                 case 'b':       /* BREs (but why???) */
 6653 tgl@sss.pgh.pa.us         405                 :              0 :                     flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
                                406                 :              0 :                     break;
 6557 bruce@momjian.us          407                 :CBC           5 :                 case 'c':       /* case sensitive */
 6653 tgl@sss.pgh.pa.us         408                 :              5 :                     flags->cflags &= ~REG_ICASE;
                                409                 :              5 :                     break;
 6557 bruce@momjian.us          410                 :UBC           0 :                 case 'e':       /* plain EREs */
 6653 tgl@sss.pgh.pa.us         411                 :              0 :                     flags->cflags |= REG_EXTENDED;
                                412                 :              0 :                     flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
                                413                 :              0 :                     break;
 6557 bruce@momjian.us          414                 :CBC         146 :                 case 'i':       /* case insensitive */
 6797 neilc@samurai.com         415                 :            146 :                     flags->cflags |= REG_ICASE;
                                416                 :            146 :                     break;
 6557 bruce@momjian.us          417                 :            564 :                 case 'm':       /* Perloid synonym for n */
                                418                 :                :                 case 'n':       /* \n affects ^ $ . [^ */
 6797 neilc@samurai.com         419                 :            564 :                     flags->cflags |= REG_NEWLINE;
                                420                 :            564 :                     break;
 6557 bruce@momjian.us          421                 :UBC           0 :                 case 'p':       /* ~Perl, \n affects . [^ */
 6797 neilc@samurai.com         422                 :              0 :                     flags->cflags |= REG_NLSTOP;
                                423                 :              0 :                     flags->cflags &= ~REG_NLANCH;
                                424                 :              0 :                     break;
 6557 bruce@momjian.us          425                 :              0 :                 case 'q':       /* literal string */
 6653 tgl@sss.pgh.pa.us         426                 :              0 :                     flags->cflags |= REG_QUOTE;
                                427                 :              0 :                     flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
                                428                 :              0 :                     break;
 6557 bruce@momjian.us          429                 :CBC           6 :                 case 's':       /* single line, \n ordinary */
 6653 tgl@sss.pgh.pa.us         430                 :              6 :                     flags->cflags &= ~REG_NEWLINE;
                                431                 :              6 :                     break;
 6557 bruce@momjian.us          432                 :UBC           0 :                 case 't':       /* tight syntax */
 6653 tgl@sss.pgh.pa.us         433                 :              0 :                     flags->cflags &= ~REG_EXPANDED;
                                434                 :              0 :                     break;
 6557 bruce@momjian.us          435                 :              0 :                 case 'w':       /* weird, \n affects ^ $ only */
 6797 neilc@samurai.com         436                 :              0 :                     flags->cflags &= ~REG_NLSTOP;
                                437                 :              0 :                     flags->cflags |= REG_NLANCH;
                                438                 :              0 :                     break;
 6557 bruce@momjian.us          439                 :CBC           3 :                 case 'x':       /* expanded syntax */
 6797 neilc@samurai.com         440                 :              3 :                     flags->cflags |= REG_EXPANDED;
                                441                 :              3 :                     break;
                                442                 :             12 :                 default:
                                443         [ +  - ]:             12 :                     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                 :         104868 : }
                                452                 :                : 
                                453                 :                : 
                                454                 :                : /*
                                455                 :                :  *  interface routines called by the function manager
                                456                 :                :  */
                                457                 :                : 
                                458                 :                : Datum
 9245 tgl@sss.pgh.pa.us         459                 :        3244813 : nameregexeq(PG_FUNCTION_ARGS)
                                460                 :                : {
                                461                 :        3244813 :     Name        n = PG_GETARG_NAME(0);
 6612                           462                 :        3244813 :     text       *p = PG_GETARG_TEXT_PP(1);
                                463                 :                : 
 8301                           464                 :        3244813 :     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
 9245                           473                 :          14639 : nameregexne(PG_FUNCTION_ARGS)
                                474                 :                : {
                                475                 :          14639 :     Name        n = PG_GETARG_NAME(0);
 6612                           476                 :          14639 :     text       *p = PG_GETARG_TEXT_PP(1);
                                477                 :                : 
 8301                           478                 :          14639 :     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
 9245                           487                 :         195213 : textregexeq(PG_FUNCTION_ARGS)
                                488                 :                : {
 6612                           489                 :         195213 :     text       *s = PG_GETARG_TEXT_PP(0);
                                490                 :         195213 :     text       *p = PG_GETARG_TEXT_PP(1);
                                491                 :                : 
 8301                           492   [ -  +  -  -  :         195213 :     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
 9245                           501                 :          17079 : textregexne(PG_FUNCTION_ARGS)
                                502                 :                : {
 6612                           503                 :          17079 :     text       *s = PG_GETARG_TEXT_PP(0);
                                504                 :          17079 :     text       *p = PG_GETARG_TEXT_PP(1);
                                505                 :                : 
 8301                           506   [ -  +  -  -  :          17079 :     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                 :           3737 : nameicregexeq(PG_FUNCTION_ARGS)
                                523                 :                : {
                                524                 :           3737 :     Name        n = PG_GETARG_NAME(0);
 6612                           525                 :           3737 :     text       *p = PG_GETARG_TEXT_PP(1);
                                526                 :                : 
 8301                           527                 :           3737 :     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                 :              3 : nameicregexne(PG_FUNCTION_ARGS)
                                537                 :                : {
                                538                 :              3 :     Name        n = PG_GETARG_NAME(0);
 6612                           539                 :              3 :     text       *p = PG_GETARG_TEXT_PP(1);
                                540                 :                : 
 8301                           541                 :              3 :     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                 :            110 : texticregexeq(PG_FUNCTION_ARGS)
                                551                 :                : {
 6612                           552                 :            110 :     text       *s = PG_GETARG_TEXT_PP(0);
                                553                 :            110 :     text       *p = PG_GETARG_TEXT_PP(1);
                                554                 :                : 
 8301                           555   [ -  +  -  -  :            110 :     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                 :             14 : texticregexne(PG_FUNCTION_ARGS)
                                565                 :                : {
 6612                           566                 :             14 :     text       *s = PG_GETARG_TEXT_PP(0);
                                567                 :             14 :     text       *p = PG_GETARG_TEXT_PP(1);
                                568                 :                : 
 8301                           569   [ -  +  -  -  :             14 :     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
 8540 lockhart@fourpalms.o      583                 :            819 : textregexsubstr(PG_FUNCTION_ARGS)
                                584                 :                : {
 6612 tgl@sss.pgh.pa.us         585                 :            819 :     text       *s = PG_GETARG_TEXT_PP(0);
                                586                 :            819 :     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 */
 5315                           593                 :            819 :     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                 :                :      */
 6432                           601         [ +  + ]:            819 :     if (!RE_execute(re,
                                602   [ -  +  -  -  :            819 :                     VARDATA_ANY(s), VARSIZE_ANY_EXHDR(s),
                                     -  -  -  -  -  
                                           +  -  + ]
                                603                 :                :                     2, pmatch))
                                604                 :              3 :         PG_RETURN_NULL();       /* definitely no match */
                                605                 :                : 
                                606         [ +  + ]:            816 :     if (re->re_nsub > 0)
                                607                 :                :     {
                                608                 :                :         /* has parenthesized subexpressions, use the first one */
 8437                           609                 :            761 :         so = pmatch[1].rm_so;
                                610                 :            761 :         eo = pmatch[1].rm_eo;
                                611                 :                :     }
                                612                 :                :     else
                                613                 :                :     {
                                614                 :                :         /* no parenthesized subexpression, use whole match */
 6432                           615                 :             55 :         so = pmatch[0].rm_so;
                                616                 :             55 :         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   [ +  +  -  + ]:            816 :     if (so < 0 || eo < 0)
                                626                 :              3 :         PG_RETURN_NULL();
                                627                 :                : 
                                628                 :            813 :     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
 7415 bruce@momjian.us          642                 :           7205 : textregexreplace_noopt(PG_FUNCTION_ARGS)
                                643                 :                : {
 6612 tgl@sss.pgh.pa.us         644                 :           7205 :     text       *s = PG_GETARG_TEXT_PP(0);
                                645                 :           7205 :     text       *p = PG_GETARG_TEXT_PP(1);
                                646                 :           7205 :     text       *r = PG_GETARG_TEXT_PP(2);
                                647                 :                : 
 1541                           648                 :           7205 :     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
 7415 bruce@momjian.us          658                 :           2126 : textregexreplace(PG_FUNCTION_ARGS)
                                659                 :                : {
 6612 tgl@sss.pgh.pa.us         660                 :           2126 :     text       *s = PG_GETARG_TEXT_PP(0);
                                661                 :           2126 :     text       *p = PG_GETARG_TEXT_PP(1);
                                662                 :           2126 :     text       *r = PG_GETARG_TEXT_PP(2);
                                663                 :           2126 :     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                 :                :      */
 1547                           673   [ +  -  -  -  :           2126 :     if (VARSIZE_ANY_EXHDR(opt) > 0)
                                     -  -  -  -  -  
                                           +  +  - ]
                                674                 :                :     {
                                675         [ -  + ]:           2126 :         char       *opt_p = VARDATA_ANY(opt);
                                676                 :                : 
                                677   [ +  -  +  + ]:           2126 :         if (*opt_p >= '0' && *opt_p <= '9')
                                678         [ +  - ]:              3 :             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                 :                : 
 6653                           685                 :           2123 :     parse_re_flags(&flags, opt);
                                686                 :                : 
 1541                           687                 :           2120 :     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
 1547                           699                 :             33 : textregexreplace_extended(PG_FUNCTION_ARGS)
                                700                 :                : {
                                701                 :             33 :     text       *s = PG_GETARG_TEXT_PP(0);
                                702                 :             33 :     text       *p = PG_GETARG_TEXT_PP(1);
                                703                 :             33 :     text       *r = PG_GETARG_TEXT_PP(2);
                                704                 :             33 :     int         start = 1;
                                705                 :             33 :     int         n = 1;
                                706         [ +  + ]:             33 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
                                707                 :                :     pg_re_flags re_flags;
                                708                 :                : 
                                709                 :                :     /* Collect optional parameters */
                                710         [ +  - ]:             33 :     if (PG_NARGS() > 3)
                                711                 :                :     {
                                712                 :             33 :         start = PG_GETARG_INT32(3);
                                713         [ +  + ]:             33 :         if (start <= 0)
                                714         [ +  - ]:              3 :             ereport(ERROR,
                                715                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                716                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                                717                 :                :                             "start", start)));
                                718                 :                :     }
                                719         [ +  + ]:             30 :     if (PG_NARGS() > 4)
                                720                 :                :     {
                                721                 :             27 :         n = PG_GETARG_INT32(4);
                                722         [ +  + ]:             27 :         if (n < 0)
                                723         [ +  - ]:              3 :             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                 :             27 :     parse_re_flags(&re_flags, flags);
                                731                 :                : 
                                732                 :                :     /* If N was not specified, deduce it from the 'g' flag */
                                733         [ +  + ]:             27 :     if (PG_NARGS() <= 4)
                                734                 :              3 :         n = re_flags.glob ? 0 : 1;
                                735                 :                : 
                                736                 :                :     /* Do the replacement(s) */
 1541                           737                 :             27 :     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
 1547                           744                 :              3 : textregexreplace_extended_no_n(PG_FUNCTION_ARGS)
                                745                 :                : {
                                746                 :              3 :     return textregexreplace_extended(fcinfo);
                                747                 :                : }
                                748                 :                : 
                                749                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                                750                 :                : Datum
                                751                 :              3 : textregexreplace_extended_no_flags(PG_FUNCTION_ARGS)
                                752                 :                : {
                                753                 :              3 :     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 *
 2243                           767                 :             93 : 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;
 8437                           775                 :             93 :     bool        afterescape = false;
                                776                 :             93 :     int         nquotes = 0;
   45                           777                 :             93 :     int         bracket_depth = 0;  /* square bracket nesting level */
                                778                 :             93 :     int         charclass_pos = 0;  /* position inside a character class */
                                779                 :                : 
 6612                           780         [ -  + ]:             93 :     p = VARDATA_ANY(pat_text);
                                781   [ -  +  -  -  :             93 :     plen = VARSIZE_ANY_EXHDR(pat_text);
                                     -  -  -  -  -  
                                                 + ]
 2243                           782         [ +  + ]:             93 :     if (esc_text == NULL)
                                783                 :                :     {
                                784                 :                :         /* No ESCAPE clause provided; default to backslash as escape */
 8437                           785                 :             44 :         e = "\\";
                                786                 :             44 :         elen = 1;
                                787                 :                :     }
                                788                 :                :     else
                                789                 :                :     {
 6612                           790         [ -  + ]:             49 :         e = VARDATA_ANY(esc_text);
                                791   [ -  +  -  -  :             49 :         elen = VARSIZE_ANY_EXHDR(esc_text);
                                     -  -  -  -  -  
                                                 + ]
 8437                           792         [ +  + ]:             49 :         if (elen == 0)
                                793                 :              3 :             e = NULL;           /* no escape character */
 2243                           794         [ +  + ]:             46 :         else if (elen > 1)
                                795                 :                :         {
 4080 jdavis@postgresql.or      796                 :              3 :             int         escape_mblen = pg_mbstrlen_with_len(e, elen);
                                797                 :                : 
                                798         [ +  - ]:              3 :             if (escape_mblen > 1)
                                799         [ +  - ]:              3 :                 ereport(ERROR,
                                800                 :                :                         (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
                                801                 :                :                          errmsg("invalid escape string"),
                                802                 :                :                          errhint("Escape string must be empty or one character.")));
                                803                 :                :         }
                                804                 :                :     }
                                805                 :                : 
                                806                 :                :     /*----------
                                807                 :                :      * We surround the transformed input string with
                                808                 :                :      *          ^(?: ... )$
                                809                 :                :      * which requires some explanation.  We need "^" and "$" to force
                                810                 :                :      * the pattern to match the entire input string as per the SQL spec.
                                811                 :                :      * The "(?:" and ")" are a non-capturing set of parens; we have to have
                                812                 :                :      * parens in case the string contains "|", else the "^" and "$" will
                                813                 :                :      * be bound into the first and last alternatives which is not what we
                                814                 :                :      * want, and the parens must be non capturing because we don't want them
                                815                 :                :      * to count when selecting output for SUBSTRING.
                                816                 :                :      *
                                817                 :                :      * When the pattern is divided into three parts by escape-double-quotes,
                                818                 :                :      * what we emit is
                                819                 :                :      *          ^(?:part1){1,1}?(part2){1,1}(?:part3)$
                                820                 :                :      * which requires even more explanation.  The "{1,1}?" on part1 makes it
                                821                 :                :      * non-greedy so that it will match the smallest possible amount of text
                                822                 :                :      * not the largest, as required by SQL.  The plain parens around part2
                                823                 :                :      * are capturing parens so that that part is what controls the result of
                                824                 :                :      * SUBSTRING.  The "{1,1}" forces part2 to be greedy, so that it matches
                                825                 :                :      * the largest possible amount of text; hence part3 must match the
                                826                 :                :      * smallest amount of text, as required by SQL.  We don't need an explicit
                                827                 :                :      * greediness marker on part3.  Note that this also confines the effects
                                828                 :                :      * of any "|" characters to the respective part, which is what we want.
                                829                 :                :      *
                                830                 :                :      * The SQL spec says that SUBSTRING's pattern must contain exactly two
                                831                 :                :      * escape-double-quotes, but we only complain if there's more than two.
                                832                 :                :      * With none, we act as though part1 and part3 are empty; with one, we
                                833                 :                :      * act as though part3 is empty.  Both behaviors fall out of omitting
                                834                 :                :      * the relevant part separators in the above expansion.  If the result
                                835                 :                :      * of this function is used in a plain regexp match (SIMILAR TO), the
                                836                 :                :      * escape-double-quotes have no effect on the match behavior.
                                837                 :                :      *
                                838                 :                :      * While we don't fully validate character classes (bracket expressions),
                                839                 :                :      * we do need to parse them well enough to know where they end.
                                840                 :                :      * "charclass_pos" tracks where we are in a character class.
                                841                 :                :      * Its value is uninteresting when bracket_depth is 0.
                                842                 :                :      * But when bracket_depth > 0, it will be
                                843                 :                :      *   1: right after the opening '[' (a following '^' will negate
                                844                 :                :      *      the class, while ']' is a literal character)
                                845                 :                :      *   2: right after a '^' after the opening '[' (']' is still a literal
                                846                 :                :      *      character)
                                847                 :                :      *   3 or more: further inside the character class (']' ends the class)
                                848                 :                :      *----------
                                849                 :                :      */
                                850                 :                : 
                                851                 :                :     /*
                                852                 :                :      * We need room for the prefix/postfix and part separators, plus as many
                                853                 :                :      * as 3 output bytes per input byte; since the input is at most 1GB this
                                854                 :                :      * can't overflow size_t.
                                855                 :                :      */
 2359 tgl@sss.pgh.pa.us         856                 :             90 :     result = (text *) palloc(VARHDRSZ + 23 + 3 * (size_t) plen);
 8437                           857                 :             90 :     r = VARDATA(result);
                                858                 :                : 
                                859                 :             90 :     *r++ = '^';
 7138                           860                 :             90 :     *r++ = '(';
                                861                 :             90 :     *r++ = '?';
                                862                 :             90 :     *r++ = ':';
                                863                 :                : 
 8437                           864         [ +  + ]:            920 :     while (plen > 0)
                                865                 :                :     {
 7318 bruce@momjian.us          866                 :            833 :         char        pchar = *p;
                                867                 :                : 
                                868                 :                :         /*
                                869                 :                :          * If both the escape character and the current character from the
                                870                 :                :          * pattern are multi-byte, we need to take the slow path.
                                871                 :                :          *
                                872                 :                :          * But if one of them is single-byte, we can process the pattern one
                                873                 :                :          * byte at a time, ignoring multi-byte characters.  (This works
                                874                 :                :          * because all server-encodings have the property that a valid
                                875                 :                :          * multi-byte character representation cannot contain the
                                876                 :                :          * representation of a valid single-byte character.)
                                877                 :                :          */
                                878                 :                : 
 4080 jdavis@postgresql.or      879         [ -  + ]:            833 :         if (elen > 1)
                                880                 :                :         {
 3811 bruce@momjian.us          881                 :UBC           0 :             int         mblen = pg_mblen(p);
                                882                 :                : 
 4080 jdavis@postgresql.or      883         [ #  # ]:              0 :             if (mblen > 1)
                                884                 :                :             {
                                885                 :                :                 /* slow, multi-byte path */
                                886         [ #  # ]:              0 :                 if (afterescape)
                                887                 :                :                 {
                                888                 :              0 :                     *r++ = '\\';
                                889                 :              0 :                     memcpy(r, p, mblen);
                                890                 :              0 :                     r += mblen;
                                891                 :              0 :                     afterescape = false;
                                892                 :                :                 }
                                893   [ #  #  #  #  :              0 :                 else if (e && elen == mblen && memcmp(e, p, mblen) == 0)
                                              #  # ]
                                894                 :                :                 {
                                895                 :                :                     /* SQL escape character; do not send to output */
                                896                 :              0 :                     afterescape = true;
                                897                 :                :                 }
                                898                 :                :                 else
                                899                 :                :                 {
                                900                 :                :                     /*
                                901                 :                :                      * We know it's a multi-byte character, so we don't need
                                902                 :                :                      * to do all the comparisons to single-byte characters
                                903                 :                :                      * that we do below.
                                904                 :                :                      */
                                905                 :              0 :                     memcpy(r, p, mblen);
                                906                 :              0 :                     r += mblen;
                                907                 :                :                 }
                                908                 :                : 
                                909                 :              0 :                 p += mblen;
                                910                 :              0 :                 plen -= mblen;
                                911                 :                : 
                                912                 :              0 :                 continue;
                                913                 :                :             }
                                914                 :                :         }
                                915                 :                : 
                                916                 :                :         /* fast path */
 8437 tgl@sss.pgh.pa.us         917         [ +  + ]:CBC         833 :         if (afterescape)
                                918                 :                :         {
   45                           919   [ +  +  +  - ]:             83 :             if (pchar == '"' && bracket_depth < 1)  /* escape-double-quote? */
                                920                 :                :             {
                                921                 :                :                 /* emit appropriate part separator, per notes above */
 2359                           922         [ +  + ]:             62 :                 if (nquotes == 0)
                                923                 :                :                 {
                                924                 :             31 :                     *r++ = ')';
                                925                 :             31 :                     *r++ = '{';
                                926                 :             31 :                     *r++ = '1';
                                927                 :             31 :                     *r++ = ',';
                                928                 :             31 :                     *r++ = '1';
                                929                 :             31 :                     *r++ = '}';
                                930                 :             31 :                     *r++ = '?';
                                931                 :             31 :                     *r++ = '(';
                                932                 :                :                 }
                                933         [ +  + ]:             31 :                 else if (nquotes == 1)
                                934                 :                :                 {
                                935                 :             28 :                     *r++ = ')';
                                936                 :             28 :                     *r++ = '{';
                                937                 :             28 :                     *r++ = '1';
                                938                 :             28 :                     *r++ = ',';
                                939                 :             28 :                     *r++ = '1';
                                940                 :             28 :                     *r++ = '}';
                                941                 :             28 :                     *r++ = '(';
                                942                 :             28 :                     *r++ = '?';
                                943                 :             28 :                     *r++ = ':';
                                944                 :                :                 }
                                945                 :                :                 else
                                946         [ +  - ]:              3 :                     ereport(ERROR,
                                947                 :                :                             (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
                                948                 :                :                              errmsg("SQL regular expression may not contain more than two escape-double-quote separators")));
                                949                 :             59 :                 nquotes++;
                                950                 :                :             }
                                951                 :                :             else
                                952                 :                :             {
                                953                 :                :                 /*
                                954                 :                :                  * We allow any character at all to be escaped; notably, this
                                955                 :                :                  * allows access to POSIX character-class escapes such as
                                956                 :                :                  * "\d".  The SQL spec is considerably more restrictive.
                                957                 :                :                  */
 8437                           958                 :             21 :                 *r++ = '\\';
                                959                 :             21 :                 *r++ = pchar;
                                960                 :                : 
                                961                 :                :                 /*
                                962                 :                :                  * If we encounter an escaped character in a character class,
                                963                 :                :                  * we are no longer at the beginning.
                                964                 :                :                  */
   45                           965                 :             21 :                 charclass_pos = 3;
                                966                 :                :             }
 8437                           967                 :             80 :             afterescape = false;
                                968                 :                :         }
                                969   [ +  +  +  + ]:            750 :         else if (e && pchar == *e)
                                970                 :                :         {
                                971                 :                :             /* SQL escape character; do not send to output */
                                972                 :             83 :             afterescape = true;
                                973                 :                :         }
   45                           974         [ +  + ]:            667 :         else if (bracket_depth > 0)
                                975                 :                :         {
                                976                 :                :             /* inside a character class */
 5778                           977         [ -  + ]:            306 :             if (pchar == '\\')
                                978                 :                :             {
                                979                 :                :                 /*
                                980                 :                :                  * If we're here, backslash is not the SQL escape character,
                                981                 :                :                  * so treat it as a literal class element, which requires
                                982                 :                :                  * doubling it.  (This matches our behavior for backslashes
                                983                 :                :                  * outside character classes.)
                                984                 :                :                  */
 5778 tgl@sss.pgh.pa.us         985                 :UBC           0 :                 *r++ = '\\';
                                986                 :                :             }
 5778 tgl@sss.pgh.pa.us         987                 :CBC         306 :             *r++ = pchar;
                                988                 :                : 
                                989                 :                :             /* parse the character class well enough to identify ending ']' */
   45                           990   [ +  +  +  + ]:            306 :             if (pchar == ']' && charclass_pos > 2)
                                991                 :                :             {
                                992                 :                :                 /* found the real end of a bracket pair */
                                993                 :             69 :                 bracket_depth--;
                                994                 :                :                 /* don't reset charclass_pos, this may be an inner bracket */
                                995                 :                :             }
  153 michael@paquier.xyz       996         [ +  + ]:            237 :             else if (pchar == '[')
                                997                 :                :             {
                                998                 :                :                 /* start of a nested bracket pair */
   45 tgl@sss.pgh.pa.us         999                 :             36 :                 bracket_depth++;
                               1000                 :                : 
                               1001                 :                :                 /*
                               1002                 :                :                  * We are no longer at the beginning of a character class.
                               1003                 :                :                  * (The nested bracket pair is a collating element, not a
                               1004                 :                :                  * character class in its own right.)
                               1005                 :                :                  */
                               1006                 :             36 :                 charclass_pos = 3;
                               1007                 :                :             }
                               1008         [ +  + ]:            201 :             else if (pchar == '^')
                               1009                 :                :             {
                               1010                 :                :                 /*
                               1011                 :                :                  * A caret right after the opening bracket negates the
                               1012                 :                :                  * character class.  In that case, the following will
                               1013                 :                :                  * increment charclass_pos from 1 to 2, so that a following
                               1014                 :                :                  * ']' is still a literal character and does not end the
                               1015                 :                :                  * character class.  If we are further inside a character
                               1016                 :                :                  * class, charclass_pos might get incremented past 3, which is
                               1017                 :                :                  * fine.
                               1018                 :                :                  */
                               1019                 :             30 :                 charclass_pos++;
                               1020                 :                :             }
                               1021                 :                :             else
                               1022                 :                :             {
                               1023                 :                :                 /*
                               1024                 :                :                  * Anything else (including a backslash or leading ']') is an
                               1025                 :                :                  * element of the character class, so we are no longer at the
                               1026                 :                :                  * beginning of the class.
                               1027                 :                :                  */
                               1028                 :            171 :                 charclass_pos = 3;
                               1029                 :                :             }
                               1030                 :                :         }
 5778                          1031         [ +  + ]:            361 :         else if (pchar == '[')
                               1032                 :                :         {
                               1033                 :                :             /* start of a character class */
                               1034                 :             33 :             *r++ = pchar;
   45                          1035                 :             33 :             bracket_depth = 1;
                               1036                 :             33 :             charclass_pos = 1;
                               1037                 :                :         }
 8437                          1038         [ +  + ]:            328 :         else if (pchar == '%')
                               1039                 :                :         {
                               1040                 :             66 :             *r++ = '.';
                               1041                 :             66 :             *r++ = '*';
                               1042                 :                :         }
                               1043         [ +  + ]:            262 :         else if (pchar == '_')
                               1044                 :             32 :             *r++ = '.';
 5778                          1045         [ +  + ]:            230 :         else if (pchar == '(')
                               1046                 :                :         {
                               1047                 :                :             /* convert to non-capturing parenthesis */
                               1048                 :             15 :             *r++ = '(';
                               1049                 :             15 :             *r++ = '?';
                               1050                 :             15 :             *r++ = ':';
                               1051                 :                :         }
 5862                          1052   [ +  +  +  +  :            215 :         else if (pchar == '\\' || pchar == '.' ||
                                              +  + ]
                               1053         [ +  + ]:            195 :                  pchar == '^' || pchar == '$')
                               1054                 :                :         {
 8437                          1055                 :             26 :             *r++ = '\\';
                               1056                 :             26 :             *r++ = pchar;
                               1057                 :                :         }
                               1058                 :                :         else
                               1059                 :            189 :             *r++ = pchar;
                               1060                 :            830 :         p++, plen--;
                               1061                 :                :     }
                               1062                 :                : 
 7138                          1063                 :             87 :     *r++ = ')';
 8437                          1064                 :             87 :     *r++ = '$';
                               1065                 :                : 
 6818                          1066                 :             87 :     SET_VARSIZE(result, r - ((char *) result));
                               1067                 :                : 
 2243                          1068                 :             87 :     return result;
                               1069                 :                : }
                               1070                 :                : 
                               1071                 :                : /*
                               1072                 :                :  * similar_to_escape(pattern, escape)
                               1073                 :                :  */
                               1074                 :                : Datum
                               1075                 :             49 : similar_to_escape_2(PG_FUNCTION_ARGS)
                               1076                 :                : {
                               1077                 :             49 :     text       *pat_text = PG_GETARG_TEXT_PP(0);
                               1078                 :             49 :     text       *esc_text = PG_GETARG_TEXT_PP(1);
                               1079                 :                :     text       *result;
                               1080                 :                : 
                               1081                 :             49 :     result = similar_escape_internal(pat_text, esc_text);
                               1082                 :                : 
                               1083                 :             43 :     PG_RETURN_TEXT_P(result);
                               1084                 :                : }
                               1085                 :                : 
                               1086                 :                : /*
                               1087                 :                :  * similar_to_escape(pattern)
                               1088                 :                :  * Inserts a default escape character.
                               1089                 :                :  */
                               1090                 :                : Datum
                               1091                 :             44 : similar_to_escape_1(PG_FUNCTION_ARGS)
                               1092                 :                : {
                               1093                 :             44 :     text       *pat_text = PG_GETARG_TEXT_PP(0);
                               1094                 :                :     text       *result;
                               1095                 :                : 
                               1096                 :             44 :     result = similar_escape_internal(pat_text, NULL);
                               1097                 :                : 
                               1098                 :             44 :     PG_RETURN_TEXT_P(result);
                               1099                 :                : }
                               1100                 :                : 
                               1101                 :                : /*
                               1102                 :                :  * similar_escape(pattern, escape)
                               1103                 :                :  *
                               1104                 :                :  * Legacy function for compatibility with views stored using the
                               1105                 :                :  * pre-v13 expansion of SIMILAR TO.  Unlike the above functions, this
                               1106                 :                :  * is non-strict, which leads to not-per-spec handling of "ESCAPE NULL".
                               1107                 :                :  */
                               1108                 :                : Datum
 2243 tgl@sss.pgh.pa.us        1109                 :UBC           0 : similar_escape(PG_FUNCTION_ARGS)
                               1110                 :                : {
                               1111                 :                :     text       *pat_text;
                               1112                 :                :     text       *esc_text;
                               1113                 :                :     text       *result;
                               1114                 :                : 
                               1115                 :                :     /* This function is not strict, so must test explicitly */
                               1116         [ #  # ]:              0 :     if (PG_ARGISNULL(0))
                               1117                 :              0 :         PG_RETURN_NULL();
                               1118                 :              0 :     pat_text = PG_GETARG_TEXT_PP(0);
                               1119                 :                : 
                               1120         [ #  # ]:              0 :     if (PG_ARGISNULL(1))
                               1121                 :              0 :         esc_text = NULL;        /* use default escape character */
                               1122                 :                :     else
                               1123                 :              0 :         esc_text = PG_GETARG_TEXT_PP(1);
                               1124                 :                : 
                               1125                 :              0 :     result = similar_escape_internal(pat_text, esc_text);
                               1126                 :                : 
 8437                          1127                 :              0 :     PG_RETURN_TEXT_P(result);
                               1128                 :                : }
                               1129                 :                : 
                               1130                 :                : /*
                               1131                 :                :  * regexp_count()
                               1132                 :                :  *      Return the number of matches of a pattern within a string.
                               1133                 :                :  */
                               1134                 :                : Datum
 1547 tgl@sss.pgh.pa.us        1135                 :CBC          24 : regexp_count(PG_FUNCTION_ARGS)
                               1136                 :                : {
                               1137                 :             24 :     text       *str = PG_GETARG_TEXT_PP(0);
                               1138                 :             24 :     text       *pattern = PG_GETARG_TEXT_PP(1);
                               1139                 :             24 :     int         start = 1;
                               1140         [ +  + ]:             24 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(3);
                               1141                 :                :     pg_re_flags re_flags;
                               1142                 :                :     regexp_matches_ctx *matchctx;
                               1143                 :                : 
                               1144                 :                :     /* Collect optional parameters */
                               1145         [ +  + ]:             24 :     if (PG_NARGS() > 2)
                               1146                 :                :     {
                               1147                 :             21 :         start = PG_GETARG_INT32(2);
                               1148         [ +  + ]:             21 :         if (start <= 0)
                               1149         [ +  - ]:              6 :             ereport(ERROR,
                               1150                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1151                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1152                 :                :                             "start", start)));
                               1153                 :                :     }
                               1154                 :                : 
                               1155                 :                :     /* Determine options */
                               1156                 :             18 :     parse_re_flags(&re_flags, flags);
                               1157                 :                :     /* User mustn't specify 'g' */
                               1158         [ -  + ]:             18 :     if (re_flags.glob)
 1547 tgl@sss.pgh.pa.us        1159         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1160                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1161                 :                :         /* translator: %s is a SQL function name */
                               1162                 :                :                  errmsg("%s does not support the \"global\" option",
                               1163                 :                :                         "regexp_count()")));
                               1164                 :                :     /* But we find all the matches anyway */
 1547 tgl@sss.pgh.pa.us        1165                 :CBC          18 :     re_flags.glob = true;
                               1166                 :                : 
                               1167                 :                :     /* Do the matching */
                               1168                 :             18 :     matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
                               1169                 :                :                                     PG_GET_COLLATION(),
                               1170                 :                :                                     false,  /* can ignore subexprs */
                               1171                 :                :                                     false, false);
                               1172                 :                : 
                               1173                 :             18 :     PG_RETURN_INT32(matchctx->nmatches);
                               1174                 :                : }
                               1175                 :                : 
                               1176                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1177                 :                : Datum
                               1178                 :              3 : regexp_count_no_start(PG_FUNCTION_ARGS)
                               1179                 :                : {
                               1180                 :              3 :     return regexp_count(fcinfo);
                               1181                 :                : }
                               1182                 :                : 
                               1183                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1184                 :                : Datum
                               1185                 :             15 : regexp_count_no_flags(PG_FUNCTION_ARGS)
                               1186                 :                : {
                               1187                 :             15 :     return regexp_count(fcinfo);
                               1188                 :                : }
                               1189                 :                : 
                               1190                 :                : /*
                               1191                 :                :  * regexp_instr()
                               1192                 :                :  *      Return the match's position within the string
                               1193                 :                :  */
                               1194                 :                : Datum
                               1195                 :             78 : regexp_instr(PG_FUNCTION_ARGS)
                               1196                 :                : {
                               1197                 :             78 :     text       *str = PG_GETARG_TEXT_PP(0);
                               1198                 :             78 :     text       *pattern = PG_GETARG_TEXT_PP(1);
                               1199                 :             78 :     int         start = 1;
                               1200                 :             78 :     int         n = 1;
                               1201                 :             78 :     int         endoption = 0;
                               1202         [ +  + ]:             78 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5);
                               1203                 :             78 :     int         subexpr = 0;
                               1204                 :                :     int         pos;
                               1205                 :                :     pg_re_flags re_flags;
                               1206                 :                :     regexp_matches_ctx *matchctx;
                               1207                 :                : 
                               1208                 :                :     /* Collect optional parameters */
                               1209         [ +  + ]:             78 :     if (PG_NARGS() > 2)
                               1210                 :                :     {
                               1211                 :             69 :         start = PG_GETARG_INT32(2);
                               1212         [ +  + ]:             69 :         if (start <= 0)
                               1213         [ +  - ]:              3 :             ereport(ERROR,
                               1214                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1215                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1216                 :                :                             "start", start)));
                               1217                 :                :     }
                               1218         [ +  + ]:             75 :     if (PG_NARGS() > 3)
                               1219                 :                :     {
                               1220                 :             63 :         n = PG_GETARG_INT32(3);
                               1221         [ +  + ]:             63 :         if (n <= 0)
                               1222         [ +  - ]:              3 :             ereport(ERROR,
                               1223                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1224                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1225                 :                :                             "n", n)));
                               1226                 :                :     }
                               1227         [ +  + ]:             72 :     if (PG_NARGS() > 4)
                               1228                 :                :     {
                               1229                 :             54 :         endoption = PG_GETARG_INT32(4);
                               1230   [ +  +  +  + ]:             54 :         if (endoption != 0 && endoption != 1)
                               1231         [ +  - ]:              6 :             ereport(ERROR,
                               1232                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1233                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1234                 :                :                             "endoption", endoption)));
                               1235                 :                :     }
                               1236         [ +  + ]:             66 :     if (PG_NARGS() > 6)
                               1237                 :                :     {
                               1238                 :             42 :         subexpr = PG_GETARG_INT32(6);
                               1239         [ +  + ]:             42 :         if (subexpr < 0)
                               1240         [ +  - ]:              3 :             ereport(ERROR,
                               1241                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1242                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1243                 :                :                             "subexpr", subexpr)));
                               1244                 :                :     }
                               1245                 :                : 
                               1246                 :                :     /* Determine options */
                               1247                 :             63 :     parse_re_flags(&re_flags, flags);
                               1248                 :                :     /* User mustn't specify 'g' */
                               1249         [ +  + ]:             63 :     if (re_flags.glob)
                               1250         [ +  - ]:              3 :         ereport(ERROR,
                               1251                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1252                 :                :         /* translator: %s is a SQL function name */
                               1253                 :                :                  errmsg("%s does not support the \"global\" option",
                               1254                 :                :                         "regexp_instr()")));
                               1255                 :                :     /* But we find all the matches anyway */
                               1256                 :             60 :     re_flags.glob = true;
                               1257                 :                : 
                               1258                 :                :     /* Do the matching */
                               1259                 :             60 :     matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
                               1260                 :                :                                     PG_GET_COLLATION(),
                               1261                 :                :                                     (subexpr > 0),   /* need submatches? */
                               1262                 :                :                                     false, false);
                               1263                 :                : 
                               1264                 :                :     /* When n exceeds matches return 0 (includes case of no matches) */
                               1265         [ +  + ]:             60 :     if (n > matchctx->nmatches)
                               1266                 :              6 :         PG_RETURN_INT32(0);
                               1267                 :                : 
                               1268                 :                :     /* When subexpr exceeds number of subexpressions return 0 */
                               1269         [ +  + ]:             54 :     if (subexpr > matchctx->npatterns)
                               1270                 :              6 :         PG_RETURN_INT32(0);
                               1271                 :                : 
                               1272                 :                :     /* Select the appropriate match position to return */
                               1273                 :             48 :     pos = (n - 1) * matchctx->npatterns;
                               1274         [ +  + ]:             48 :     if (subexpr > 0)
                               1275                 :             27 :         pos += subexpr - 1;
                               1276                 :             48 :     pos *= 2;
                               1277         [ +  + ]:             48 :     if (endoption == 1)
                               1278                 :             15 :         pos += 1;
                               1279                 :                : 
                               1280         [ +  + ]:             48 :     if (matchctx->match_locs[pos] >= 0)
                               1281                 :             45 :         PG_RETURN_INT32(matchctx->match_locs[pos] + 1);
                               1282                 :                :     else
                               1283                 :              3 :         PG_RETURN_INT32(0);     /* position not identifiable */
                               1284                 :                : }
                               1285                 :                : 
                               1286                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1287                 :                : Datum
                               1288                 :              9 : regexp_instr_no_start(PG_FUNCTION_ARGS)
                               1289                 :                : {
                               1290                 :              9 :     return regexp_instr(fcinfo);
                               1291                 :                : }
                               1292                 :                : 
                               1293                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1294                 :                : Datum
                               1295                 :              3 : regexp_instr_no_n(PG_FUNCTION_ARGS)
                               1296                 :                : {
                               1297                 :              3 :     return regexp_instr(fcinfo);
                               1298                 :                : }
                               1299                 :                : 
                               1300                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1301                 :                : Datum
                               1302                 :             12 : regexp_instr_no_endoption(PG_FUNCTION_ARGS)
                               1303                 :                : {
                               1304                 :             12 :     return regexp_instr(fcinfo);
                               1305                 :                : }
                               1306                 :                : 
                               1307                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1308                 :                : Datum
                               1309                 :              6 : regexp_instr_no_flags(PG_FUNCTION_ARGS)
                               1310                 :                : {
                               1311                 :              6 :     return regexp_instr(fcinfo);
                               1312                 :                : }
                               1313                 :                : 
                               1314                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1315                 :                : Datum
                               1316                 :              6 : regexp_instr_no_subexpr(PG_FUNCTION_ARGS)
                               1317                 :                : {
                               1318                 :              6 :     return regexp_instr(fcinfo);
                               1319                 :                : }
                               1320                 :                : 
                               1321                 :                : /*
                               1322                 :                :  * regexp_like()
                               1323                 :                :  *      Test for a pattern match within a string.
                               1324                 :                :  */
                               1325                 :                : Datum
                               1326                 :             15 : regexp_like(PG_FUNCTION_ARGS)
                               1327                 :                : {
                               1328                 :             15 :     text       *str = PG_GETARG_TEXT_PP(0);
                               1329                 :             15 :     text       *pattern = PG_GETARG_TEXT_PP(1);
                               1330         [ +  + ]:             15 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
                               1331                 :                :     pg_re_flags re_flags;
                               1332                 :                : 
                               1333                 :                :     /* Determine options */
                               1334                 :             15 :     parse_re_flags(&re_flags, flags);
                               1335                 :                :     /* User mustn't specify 'g' */
                               1336         [ +  + ]:             15 :     if (re_flags.glob)
                               1337         [ +  - ]:              3 :         ereport(ERROR,
                               1338                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1339                 :                :         /* translator: %s is a SQL function name */
                               1340                 :                :                  errmsg("%s does not support the \"global\" option",
                               1341                 :                :                         "regexp_like()")));
                               1342                 :                : 
                               1343                 :                :     /* Otherwise it's like textregexeq/texticregexeq */
                               1344   [ -  +  -  -  :             12 :     PG_RETURN_BOOL(RE_compile_and_execute(pattern,
                                     -  -  -  -  -  
                                           +  -  + ]
                               1345                 :                :                                           VARDATA_ANY(str),
                               1346                 :                :                                           VARSIZE_ANY_EXHDR(str),
                               1347                 :                :                                           re_flags.cflags,
                               1348                 :                :                                           PG_GET_COLLATION(),
                               1349                 :                :                                           0, NULL));
                               1350                 :                : }
                               1351                 :                : 
                               1352                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1353                 :                : Datum
                               1354                 :              3 : regexp_like_no_flags(PG_FUNCTION_ARGS)
                               1355                 :                : {
                               1356                 :              3 :     return regexp_like(fcinfo);
                               1357                 :                : }
                               1358                 :                : 
                               1359                 :                : /*
                               1360                 :                :  * regexp_match()
                               1361                 :                :  *      Return the first substring(s) matching a pattern within a string.
                               1362                 :                :  */
                               1363                 :                : Datum
 3359                          1364                 :           1270 : regexp_match(PG_FUNCTION_ARGS)
                               1365                 :                : {
                               1366                 :           1270 :     text       *orig_str = PG_GETARG_TEXT_PP(0);
                               1367                 :           1270 :     text       *pattern = PG_GETARG_TEXT_PP(1);
                               1368         [ +  + ]:           1270 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
                               1369                 :                :     pg_re_flags re_flags;
                               1370                 :                :     regexp_matches_ctx *matchctx;
                               1371                 :                : 
                               1372                 :                :     /* Determine options */
                               1373                 :           1270 :     parse_re_flags(&re_flags, flags);
                               1374                 :                :     /* User mustn't specify 'g' */
                               1375         [ +  + ]:           1270 :     if (re_flags.glob)
                               1376         [ +  - ]:              4 :         ereport(ERROR,
                               1377                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1378                 :                :         /* translator: %s is a SQL function name */
                               1379                 :                :                  errmsg("%s does not support the \"global\" option",
                               1380                 :                :                         "regexp_match()"),
                               1381                 :                :                  errhint("Use the regexp_matches function instead.")));
                               1382                 :                : 
 1547                          1383                 :           1266 :     matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, 0,
                               1384                 :                :                                     PG_GET_COLLATION(), true, false, false);
                               1385                 :                : 
 3359                          1386         [ +  + ]:           1266 :     if (matchctx->nmatches == 0)
                               1387                 :             65 :         PG_RETURN_NULL();
                               1388                 :                : 
                               1389         [ -  + ]:           1201 :     Assert(matchctx->nmatches == 1);
                               1390                 :                : 
                               1391                 :                :     /* Create workspace that build_regexp_match_result needs */
                               1392                 :           1201 :     matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
                               1393                 :           1201 :     matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
                               1394                 :                : 
                               1395                 :           1201 :     PG_RETURN_DATUM(PointerGetDatum(build_regexp_match_result(matchctx)));
                               1396                 :                : }
                               1397                 :                : 
                               1398                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1399                 :                : Datum
                               1400                 :           1255 : regexp_match_no_flags(PG_FUNCTION_ARGS)
                               1401                 :                : {
                               1402                 :           1255 :     return regexp_match(fcinfo);
                               1403                 :                : }
                               1404                 :                : 
                               1405                 :                : /*
                               1406                 :                :  * regexp_matches()
                               1407                 :                :  *      Return a table of all matches of a pattern within a string.
                               1408                 :                :  */
                               1409                 :                : Datum
 6797 neilc@samurai.com        1410                 :           1305 : regexp_matches(PG_FUNCTION_ARGS)
                               1411                 :                : {
                               1412                 :                :     FuncCallContext *funcctx;
                               1413                 :                :     regexp_matches_ctx *matchctx;
                               1414                 :                : 
                               1415         [ +  + ]:           1305 :     if (SRF_IS_FIRSTCALL())
                               1416                 :                :     {
 6557 bruce@momjian.us         1417                 :            975 :         text       *pattern = PG_GETARG_TEXT_PP(1);
                               1418         [ +  + ]:            975 :         text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
                               1419                 :                :         pg_re_flags re_flags;
                               1420                 :                :         MemoryContext oldcontext;
                               1421                 :                : 
 6797 neilc@samurai.com        1422                 :            975 :         funcctx = SRF_FIRSTCALL_INIT();
                               1423                 :            975 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                               1424                 :                : 
                               1425                 :                :         /* Determine options */
 3359 tgl@sss.pgh.pa.us        1426                 :            975 :         parse_re_flags(&re_flags, flags);
                               1427                 :                : 
                               1428                 :                :         /* be sure to copy the input string into the multi-call ctx */
 6789 neilc@samurai.com        1429                 :            972 :         matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
                               1430                 :                :                                         &re_flags, 0,
                               1431                 :                :                                         PG_GET_COLLATION(),
                               1432                 :                :                                         true, false, false);
                               1433                 :                : 
                               1434                 :                :         /* Pre-create workspace that build_regexp_match_result needs */
 6653 tgl@sss.pgh.pa.us        1435                 :            966 :         matchctx->elems = (Datum *) palloc(sizeof(Datum) * matchctx->npatterns);
                               1436                 :            966 :         matchctx->nulls = (bool *) palloc(sizeof(bool) * matchctx->npatterns);
                               1437                 :                : 
 6797 neilc@samurai.com        1438                 :            966 :         MemoryContextSwitchTo(oldcontext);
  334 peter@eisentraut.org     1439                 :            966 :         funcctx->user_fctx = matchctx;
                               1440                 :                :     }
                               1441                 :                : 
 6797 neilc@samurai.com        1442                 :           1296 :     funcctx = SRF_PERCALL_SETUP();
                               1443                 :           1296 :     matchctx = (regexp_matches_ctx *) funcctx->user_fctx;
                               1444                 :                : 
 6653 tgl@sss.pgh.pa.us        1445         [ +  + ]:           1296 :     if (matchctx->next_match < matchctx->nmatches)
                               1446                 :                :     {
                               1447                 :                :         ArrayType  *result_ary;
                               1448                 :                : 
 3359                          1449                 :            330 :         result_ary = build_regexp_match_result(matchctx);
 6653                          1450                 :            330 :         matchctx->next_match++;
                               1451                 :            330 :         SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
                               1452                 :                :     }
                               1453                 :                : 
 6797 neilc@samurai.com        1454                 :            966 :     SRF_RETURN_DONE(funcctx);
                               1455                 :                : }
                               1456                 :                : 
                               1457                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1458                 :                : Datum
                               1459                 :           1143 : regexp_matches_no_flags(PG_FUNCTION_ARGS)
                               1460                 :                : {
                               1461                 :           1143 :     return regexp_matches(fcinfo);
                               1462                 :                : }
                               1463                 :                : 
                               1464                 :                : /*
                               1465                 :                :  * setup_regexp_matches --- do the initial matching for regexp_match,
                               1466                 :                :  *      regexp_split, and related functions
                               1467                 :                :  *
                               1468                 :                :  * To avoid having to re-find the compiled pattern on each call, we do
                               1469                 :                :  * all the matching in one swoop.  The returned regexp_matches_ctx contains
                               1470                 :                :  * the locations of all the substrings matching the pattern.
                               1471                 :                :  *
                               1472                 :                :  * start_search: the character (not byte) offset in orig_str at which to
                               1473                 :                :  * begin the search.  Returned positions are relative to orig_str anyway.
                               1474                 :                :  * use_subpatterns: collect data about matches to parenthesized subexpressions.
                               1475                 :                :  * ignore_degenerate: ignore zero-length matches.
                               1476                 :                :  * fetching_unmatched: caller wants to fetch unmatched substrings.
                               1477                 :                :  *
                               1478                 :                :  * We don't currently assume that fetching_unmatched is exclusive of fetching
                               1479                 :                :  * the matched text too; if it's set, the conversion buffer is large enough to
                               1480                 :                :  * fetch any single matched or unmatched string, but not any larger
                               1481                 :                :  * substring.  (In practice, when splitting the matches are usually small
                               1482                 :                :  * anyway, and it didn't seem worth complicating the code further.)
                               1483                 :                :  */
                               1484                 :                : static regexp_matches_ctx *
 3359 tgl@sss.pgh.pa.us        1485                 :         102690 : setup_regexp_matches(text *orig_str, text *pattern, pg_re_flags *re_flags,
                               1486                 :                :                      int start_search,
                               1487                 :                :                      Oid collation,
                               1488                 :                :                      bool use_subpatterns,
                               1489                 :                :                      bool ignore_degenerate,
                               1490                 :                :                      bool fetching_unmatched)
                               1491                 :                : {
 6653                          1492                 :         102690 :     regexp_matches_ctx *matchctx = palloc0(sizeof(regexp_matches_ctx));
 2618 rhodiumtoad@postgres     1493                 :         102690 :     int         eml = pg_database_encoding_max_length();
                               1494                 :                :     int         orig_len;
                               1495                 :                :     pg_wchar   *wide_str;
                               1496                 :                :     int         wide_len;
                               1497                 :                :     int         cflags;
                               1498                 :                :     regex_t    *cpattern;
                               1499                 :                :     regmatch_t *pmatch;
                               1500                 :                :     int         pmatch_len;
                               1501                 :                :     int         array_len;
                               1502                 :                :     int         array_idx;
                               1503                 :                :     int         prev_match_end;
                               1504                 :                :     int         prev_valid_match_end;
                               1505                 :         102690 :     int         maxlen = 0;     /* largest fetch length in characters */
                               1506                 :                : 
                               1507                 :                :     /* save original string --- we'll extract result substrings from it */
 6797 neilc@samurai.com        1508                 :         102690 :     matchctx->orig_str = orig_str;
                               1509                 :                : 
                               1510                 :                :     /* convert string to pg_wchar form for matching */
 6612 tgl@sss.pgh.pa.us        1511   [ -  +  -  -  :         102690 :     orig_len = VARSIZE_ANY_EXHDR(orig_str);
                                     -  -  -  -  +  
                                                 + ]
 6653                          1512                 :         102690 :     wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
 6612                          1513         [ +  + ]:         102690 :     wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
                               1514                 :                : 
                               1515                 :                :     /* set up the compiled pattern */
 1541                          1516                 :         102690 :     cflags = re_flags->cflags;
                               1517         [ +  + ]:         102690 :     if (!use_subpatterns)
                               1518                 :         100401 :         cflags |= REG_NOSUB;
                               1519                 :         102690 :     cpattern = RE_compile_and_cache(pattern, cflags, collation);
                               1520                 :                : 
                               1521                 :                :     /* do we want to remember subpatterns? */
 6653                          1522   [ +  +  +  + ]:         102684 :     if (use_subpatterns && cpattern->re_nsub > 0)
                               1523                 :                :     {
                               1524                 :           1347 :         matchctx->npatterns = cpattern->re_nsub;
                               1525                 :           1347 :         pmatch_len = cpattern->re_nsub + 1;
                               1526                 :                :     }
                               1527                 :                :     else
                               1528                 :                :     {
                               1529                 :         101337 :         use_subpatterns = false;
                               1530                 :         101337 :         matchctx->npatterns = 1;
                               1531                 :         101337 :         pmatch_len = 1;
                               1532                 :                :     }
                               1533                 :                : 
                               1534                 :                :     /* temporary output space for RE package */
                               1535                 :         102684 :     pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
                               1536                 :                : 
                               1537                 :                :     /*
                               1538                 :                :      * the real output space (grown dynamically if needed)
                               1539                 :                :      *
                               1540                 :                :      * use values 2^n-1, not 2^n, so that we hit the limit at 2^28-1 rather
                               1541                 :                :      * than at 2^27
                               1542                 :                :      */
 2618 rhodiumtoad@postgres     1543         [ +  + ]:         102684 :     array_len = re_flags->glob ? 255 : 31;
 6653 tgl@sss.pgh.pa.us        1544                 :         102684 :     matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
                               1545                 :         102684 :     array_idx = 0;
                               1546                 :                : 
                               1547                 :                :     /* search for the pattern, perhaps repeatedly */
                               1548                 :         102684 :     prev_match_end = 0;
 2603 rhodiumtoad@postgres     1549                 :         102684 :     prev_valid_match_end = 0;
 6653 tgl@sss.pgh.pa.us        1550         [ +  + ]:         548704 :     while (RE_wchar_execute(cpattern, wide_str, wide_len, start_search,
                               1551                 :                :                             pmatch_len, pmatch))
                               1552                 :                :     {
                               1553                 :                :         /*
                               1554                 :                :          * If requested, ignore degenerate matches, which are zero-length
                               1555                 :                :          * matches occurring at the start or end of a string or just after a
                               1556                 :                :          * previous match.
                               1557                 :                :          */
                               1558         [ +  + ]:         447474 :         if (!ignore_degenerate ||
                               1559         [ +  + ]:         445745 :             (pmatch[0].rm_so < wide_len &&
                               1560         [ +  + ]:         445724 :              pmatch[0].rm_eo > prev_match_end))
                               1561                 :                :         {
                               1562                 :                :             /* enlarge output space if needed */
 2618 rhodiumtoad@postgres     1563         [ +  + ]:         447564 :             while (array_idx + matchctx->npatterns * 2 + 1 > array_len)
                               1564                 :                :             {
 2351 tgl@sss.pgh.pa.us        1565                 :            180 :                 array_len += array_len + 1; /* 2^n-1 => 2^(n+1)-1 */
                               1566         [ -  + ]:            180 :                 if (array_len > MaxAllocSize / sizeof(int))
 2618 rhodiumtoad@postgres     1567         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               1568                 :                :                             (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               1569                 :                :                              errmsg("too many regular expression matches")));
 6653 tgl@sss.pgh.pa.us        1570                 :CBC         180 :                 matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
                               1571                 :                :                                                         sizeof(int) * array_len);
                               1572                 :                :             }
                               1573                 :                : 
                               1574                 :                :             /* save this match's locations */
                               1575         [ +  + ]:         447384 :             if (use_subpatterns)
                               1576                 :                :             {
                               1577                 :                :                 int         i;
                               1578                 :                : 
                               1579         [ +  + ]:           3972 :                 for (i = 1; i <= matchctx->npatterns; i++)
                               1580                 :                :                 {
 2351                          1581                 :           2681 :                     int         so = pmatch[i].rm_so;
                               1582                 :           2681 :                     int         eo = pmatch[i].rm_eo;
                               1583                 :                : 
 2618 rhodiumtoad@postgres     1584                 :           2681 :                     matchctx->match_locs[array_idx++] = so;
                               1585                 :           2681 :                     matchctx->match_locs[array_idx++] = eo;
                               1586   [ +  +  +  -  :           2681 :                     if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
                                              +  + ]
                               1587                 :           1722 :                         maxlen = (eo - so);
                               1588                 :                :                 }
                               1589                 :                :             }
                               1590                 :                :             else
                               1591                 :                :             {
 2351 tgl@sss.pgh.pa.us        1592                 :         446093 :                 int         so = pmatch[0].rm_so;
                               1593                 :         446093 :                 int         eo = pmatch[0].rm_eo;
                               1594                 :                : 
 2618 rhodiumtoad@postgres     1595                 :         446093 :                 matchctx->match_locs[array_idx++] = so;
                               1596                 :         446093 :                 matchctx->match_locs[array_idx++] = eo;
                               1597   [ +  -  +  -  :         446093 :                 if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
                                              +  + ]
                               1598                 :         100558 :                     maxlen = (eo - so);
                               1599                 :                :             }
 6653 tgl@sss.pgh.pa.us        1600                 :         447384 :             matchctx->nmatches++;
                               1601                 :                : 
                               1602                 :                :             /*
                               1603                 :                :              * check length of unmatched portion between end of previous valid
                               1604                 :                :              * (nondegenerate, or degenerate but not ignored) match and start
                               1605                 :                :              * of current one
                               1606                 :                :              */
 2618 rhodiumtoad@postgres     1607         [ +  + ]:         447384 :             if (fetching_unmatched &&
                               1608         [ +  - ]:         445655 :                 pmatch[0].rm_so >= 0 &&
 2603                          1609         [ +  + ]:         445655 :                 (pmatch[0].rm_so - prev_valid_match_end) > maxlen)
                               1610                 :         190532 :                 maxlen = (pmatch[0].rm_so - prev_valid_match_end);
                               1611                 :         447384 :             prev_valid_match_end = pmatch[0].rm_eo;
                               1612                 :                :         }
 6653 tgl@sss.pgh.pa.us        1613                 :         447474 :         prev_match_end = pmatch[0].rm_eo;
                               1614                 :                : 
                               1615                 :                :         /* if not glob, stop after one match */
 3359                          1616         [ +  + ]:         447474 :         if (!re_flags->glob)
 6653                          1617                 :           1421 :             break;
                               1618                 :                : 
                               1619                 :                :         /*
                               1620                 :                :          * Advance search position.  Normally we start the next search at the
                               1621                 :                :          * end of the previous match; but if the match was of zero length, we
                               1622                 :                :          * have to advance by one character, or we'd just find the same match
                               1623                 :                :          * again.
                               1624                 :                :          */
 4472                          1625                 :         446053 :         start_search = prev_match_end;
                               1626         [ +  + ]:         446053 :         if (pmatch[0].rm_so == pmatch[0].rm_eo)
 6653                          1627                 :            588 :             start_search++;
                               1628         [ +  + ]:         446053 :         if (start_search > wide_len)
                               1629                 :             33 :             break;
                               1630                 :                :     }
                               1631                 :                : 
                               1632                 :                :     /*
                               1633                 :                :      * check length of unmatched portion between end of last match and end of
                               1634                 :                :      * input string
                               1635                 :                :      */
 2618 rhodiumtoad@postgres     1636         [ +  + ]:         102684 :     if (fetching_unmatched &&
 2603                          1637         [ +  + ]:         100332 :         (wide_len - prev_valid_match_end) > maxlen)
                               1638                 :             93 :         maxlen = (wide_len - prev_valid_match_end);
                               1639                 :                : 
                               1640                 :                :     /*
                               1641                 :                :      * Keep a note of the end position of the string for the benefit of
                               1642                 :                :      * splitting code.
                               1643                 :                :      */
 2618                          1644                 :         102684 :     matchctx->match_locs[array_idx] = wide_len;
                               1645                 :                : 
                               1646         [ +  - ]:         102684 :     if (eml > 1)
                               1647                 :                :     {
                               1648                 :         102684 :         int64       maxsiz = eml * (int64) maxlen;
                               1649                 :                :         int         conv_bufsiz;
                               1650                 :                : 
                               1651                 :                :         /*
                               1652                 :                :          * Make the conversion buffer large enough for any substring of
                               1653                 :                :          * interest.
                               1654                 :                :          *
                               1655                 :                :          * Worst case: assume we need the maximum size (maxlen*eml), but take
                               1656                 :                :          * advantage of the fact that the original string length in bytes is
                               1657                 :                :          * an upper bound on the byte length of any fetched substring (and we
                               1658                 :                :          * know that len+1 is safe to allocate because the varlena header is
                               1659                 :                :          * longer than 1 byte).
                               1660                 :                :          */
                               1661         [ +  + ]:         102684 :         if (maxsiz > orig_len)
                               1662                 :         100513 :             conv_bufsiz = orig_len + 1;
                               1663                 :                :         else
                               1664                 :           2171 :             conv_bufsiz = maxsiz + 1;   /* safe since maxsiz < 2^30 */
                               1665                 :                : 
                               1666                 :         102684 :         matchctx->conv_buf = palloc(conv_bufsiz);
                               1667                 :         102684 :         matchctx->conv_bufsiz = conv_bufsiz;
                               1668                 :         102684 :         matchctx->wide_str = wide_str;
                               1669                 :                :     }
                               1670                 :                :     else
                               1671                 :                :     {
                               1672                 :                :         /* No need to keep the wide string if we're in a single-byte charset. */
 2618 rhodiumtoad@postgres     1673                 :UBC           0 :         pfree(wide_str);
                               1674                 :              0 :         matchctx->wide_str = NULL;
                               1675                 :              0 :         matchctx->conv_buf = NULL;
                               1676                 :              0 :         matchctx->conv_bufsiz = 0;
                               1677                 :                :     }
                               1678                 :                : 
                               1679                 :                :     /* Clean up temp storage */
 6653 tgl@sss.pgh.pa.us        1680                 :CBC      102684 :     pfree(pmatch);
                               1681                 :                : 
                               1682                 :         102684 :     return matchctx;
                               1683                 :                : }
                               1684                 :                : 
                               1685                 :                : /*
                               1686                 :                :  * build_regexp_match_result - build output array for current match
                               1687                 :                :  */
                               1688                 :                : static ArrayType *
 3359                          1689                 :           1531 : build_regexp_match_result(regexp_matches_ctx *matchctx)
                               1690                 :                : {
 2618 rhodiumtoad@postgres     1691                 :           1531 :     char       *buf = matchctx->conv_buf;
 6653 tgl@sss.pgh.pa.us        1692                 :           1531 :     Datum      *elems = matchctx->elems;
                               1693                 :           1531 :     bool       *nulls = matchctx->nulls;
                               1694                 :                :     int         dims[1];
                               1695                 :                :     int         lbs[1];
                               1696                 :                :     int         loc;
                               1697                 :                :     int         i;
                               1698                 :                : 
                               1699                 :                :     /* Extract matching substrings from the original string */
                               1700                 :           1531 :     loc = matchctx->next_match * matchctx->npatterns * 2;
                               1701         [ +  + ]:           4317 :     for (i = 0; i < matchctx->npatterns; i++)
                               1702                 :                :     {
 6557 bruce@momjian.us         1703                 :           2786 :         int         so = matchctx->match_locs[loc++];
                               1704                 :           2786 :         int         eo = matchctx->match_locs[loc++];
                               1705                 :                : 
 6653 tgl@sss.pgh.pa.us        1706   [ +  +  -  + ]:           2786 :         if (so < 0 || eo < 0)
                               1707                 :                :         {
                               1708                 :              3 :             elems[i] = (Datum) 0;
                               1709                 :              3 :             nulls[i] = true;
                               1710                 :                :         }
 2618 rhodiumtoad@postgres     1711         [ +  - ]:           2783 :         else if (buf)
                               1712                 :                :         {
 2351 tgl@sss.pgh.pa.us        1713                 :           2783 :             int         len = pg_wchar2mb_with_len(matchctx->wide_str + so,
                               1714                 :                :                                                    buf,
                               1715                 :                :                                                    eo - so);
                               1716                 :                : 
 2112                          1717         [ -  + ]:           2783 :             Assert(len < matchctx->conv_bufsiz);
 2618 rhodiumtoad@postgres     1718                 :           2783 :             elems[i] = PointerGetDatum(cstring_to_text_with_len(buf, len));
                               1719                 :           2783 :             nulls[i] = false;
                               1720                 :                :         }
                               1721                 :                :         else
                               1722                 :                :         {
 6653 tgl@sss.pgh.pa.us        1723                 :UBC           0 :             elems[i] = DirectFunctionCall3(text_substr,
                               1724                 :                :                                            PointerGetDatum(matchctx->orig_str),
                               1725                 :                :                                            Int32GetDatum(so + 1),
                               1726                 :                :                                            Int32GetDatum(eo - so));
                               1727                 :              0 :             nulls[i] = false;
                               1728                 :                :         }
                               1729                 :                :     }
                               1730                 :                : 
                               1731                 :                :     /* And form an array */
 6653 tgl@sss.pgh.pa.us        1732                 :CBC        1531 :     dims[0] = matchctx->npatterns;
                               1733                 :           1531 :     lbs[0] = 1;
                               1734                 :                :     /* XXX: this hardcodes assumptions about the text type */
                               1735                 :           1531 :     return construct_md_array(elems, nulls, 1, dims, lbs,
                               1736                 :                :                               TEXTOID, -1, false, TYPALIGN_INT);
                               1737                 :                : }
                               1738                 :                : 
                               1739                 :                : /*
                               1740                 :                :  * regexp_split_to_table()
                               1741                 :                :  *      Split the string at matches of the pattern, returning the
                               1742                 :                :  *      split-out substrings as a table.
                               1743                 :                :  */
                               1744                 :                : Datum
 6797 neilc@samurai.com        1745                 :            311 : regexp_split_to_table(PG_FUNCTION_ARGS)
                               1746                 :                : {
                               1747                 :                :     FuncCallContext *funcctx;
                               1748                 :                :     regexp_matches_ctx *splitctx;
                               1749                 :                : 
                               1750         [ +  + ]:            311 :     if (SRF_IS_FIRSTCALL())
                               1751                 :                :     {
 6557 bruce@momjian.us         1752                 :             26 :         text       *pattern = PG_GETARG_TEXT_PP(1);
                               1753         [ +  + ]:             26 :         text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(2);
                               1754                 :                :         pg_re_flags re_flags;
                               1755                 :                :         MemoryContext oldcontext;
                               1756                 :                : 
 6797 neilc@samurai.com        1757                 :             26 :         funcctx = SRF_FIRSTCALL_INIT();
                               1758                 :             26 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                               1759                 :                : 
                               1760                 :                :         /* Determine options */
 3359 tgl@sss.pgh.pa.us        1761                 :             26 :         parse_re_flags(&re_flags, flags);
                               1762                 :                :         /* User mustn't specify 'g' */
                               1763         [ +  + ]:             23 :         if (re_flags.glob)
                               1764         [ +  - ]:              3 :             ereport(ERROR,
                               1765                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1766                 :                :             /* translator: %s is a SQL function name */
                               1767                 :                :                      errmsg("%s does not support the \"global\" option",
                               1768                 :                :                             "regexp_split_to_table()")));
                               1769                 :                :         /* But we find all the matches anyway */
                               1770                 :             20 :         re_flags.glob = true;
                               1771                 :                : 
                               1772                 :                :         /* be sure to copy the input string into the multi-call ctx */
 6653                          1773                 :             20 :         splitctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
                               1774                 :                :                                         &re_flags, 0,
                               1775                 :                :                                         PG_GET_COLLATION(),
                               1776                 :                :                                         false, true, true);
                               1777                 :                : 
 6797 neilc@samurai.com        1778                 :             20 :         MemoryContextSwitchTo(oldcontext);
  334 peter@eisentraut.org     1779                 :             20 :         funcctx->user_fctx = splitctx;
                               1780                 :                :     }
                               1781                 :                : 
 6797 neilc@samurai.com        1782                 :            305 :     funcctx = SRF_PERCALL_SETUP();
 6653 tgl@sss.pgh.pa.us        1783                 :            305 :     splitctx = (regexp_matches_ctx *) funcctx->user_fctx;
                               1784                 :                : 
                               1785         [ +  + ]:            305 :     if (splitctx->next_match <= splitctx->nmatches)
                               1786                 :                :     {
 6557 bruce@momjian.us         1787                 :            285 :         Datum       result = build_regexp_split_result(splitctx);
                               1788                 :                : 
 6653 tgl@sss.pgh.pa.us        1789                 :            285 :         splitctx->next_match++;
                               1790                 :            285 :         SRF_RETURN_NEXT(funcctx, result);
                               1791                 :                :     }
                               1792                 :                : 
                               1793                 :             20 :     SRF_RETURN_DONE(funcctx);
                               1794                 :                : }
                               1795                 :                : 
                               1796                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1797                 :                : Datum
 6557 bruce@momjian.us         1798                 :            276 : regexp_split_to_table_no_flags(PG_FUNCTION_ARGS)
                               1799                 :                : {
 6797 neilc@samurai.com        1800                 :            276 :     return regexp_split_to_table(fcinfo);
                               1801                 :                : }
                               1802                 :                : 
                               1803                 :                : /*
                               1804                 :                :  * regexp_split_to_array()
                               1805                 :                :  *      Split the string at matches of the pattern, returning the
                               1806                 :                :  *      split-out substrings as an array.
                               1807                 :                :  */
                               1808                 :                : Datum
 6557 bruce@momjian.us         1809                 :         100318 : regexp_split_to_array(PG_FUNCTION_ARGS)
                               1810                 :                : {
                               1811                 :         100318 :     ArrayBuildState *astate = NULL;
                               1812                 :                :     pg_re_flags re_flags;
                               1813                 :                :     regexp_matches_ctx *splitctx;
                               1814                 :                : 
                               1815                 :                :     /* Determine options */
 3359 tgl@sss.pgh.pa.us        1816         [ +  + ]:         100318 :     parse_re_flags(&re_flags, PG_GETARG_TEXT_PP_IF_EXISTS(2));
                               1817                 :                :     /* User mustn't specify 'g' */
                               1818         [ +  + ]:         100315 :     if (re_flags.glob)
                               1819         [ +  - ]:              3 :         ereport(ERROR,
                               1820                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1821                 :                :         /* translator: %s is a SQL function name */
                               1822                 :                :                  errmsg("%s does not support the \"global\" option",
                               1823                 :                :                         "regexp_split_to_array()")));
                               1824                 :                :     /* But we find all the matches anyway */
                               1825                 :         100312 :     re_flags.glob = true;
                               1826                 :                : 
 6612                          1827                 :         100312 :     splitctx = setup_regexp_matches(PG_GETARG_TEXT_PP(0),
                               1828                 :         100312 :                                     PG_GETARG_TEXT_PP(1),
                               1829                 :                :                                     &re_flags, 0,
                               1830                 :                :                                     PG_GET_COLLATION(),
                               1831                 :                :                                     false, true, true);
                               1832                 :                : 
 6653                          1833         [ +  + ]:         646014 :     while (splitctx->next_match <= splitctx->nmatches)
                               1834                 :                :     {
 6797 neilc@samurai.com        1835                 :         545702 :         astate = accumArrayResult(astate,
                               1836                 :                :                                   build_regexp_split_result(splitctx),
                               1837                 :                :                                   false,
                               1838                 :                :                                   TEXTOID,
                               1839                 :                :                                   CurrentMemoryContext);
 6653 tgl@sss.pgh.pa.us        1840                 :         545702 :         splitctx->next_match++;
                               1841                 :                :     }
                               1842                 :                : 
 1157 peter@eisentraut.org     1843                 :         100312 :     PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
                               1844                 :                : }
                               1845                 :                : 
                               1846                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1847                 :                : Datum
 6557 bruce@momjian.us         1848                 :         100297 : regexp_split_to_array_no_flags(PG_FUNCTION_ARGS)
                               1849                 :                : {
 6797 neilc@samurai.com        1850                 :         100297 :     return regexp_split_to_array(fcinfo);
                               1851                 :                : }
                               1852                 :                : 
                               1853                 :                : /*
                               1854                 :                :  * build_regexp_split_result - build output string for current match
                               1855                 :                :  *
                               1856                 :                :  * We return the string between the current match and the previous one,
                               1857                 :                :  * or the string after the last match when next_match == nmatches.
                               1858                 :                :  */
                               1859                 :                : static Datum
 6557 bruce@momjian.us         1860                 :         545987 : build_regexp_split_result(regexp_matches_ctx *splitctx)
                               1861                 :                : {
 2618 rhodiumtoad@postgres     1862                 :         545987 :     char       *buf = splitctx->conv_buf;
                               1863                 :                :     int         startpos;
                               1864                 :                :     int         endpos;
                               1865                 :                : 
 6653 tgl@sss.pgh.pa.us        1866         [ +  + ]:         545987 :     if (splitctx->next_match > 0)
                               1867                 :         445655 :         startpos = splitctx->match_locs[splitctx->next_match * 2 - 1];
                               1868                 :                :     else
                               1869                 :         100332 :         startpos = 0;
                               1870         [ -  + ]:         545987 :     if (startpos < 0)
 6653 tgl@sss.pgh.pa.us        1871         [ #  # ]:UBC           0 :         elog(ERROR, "invalid match ending position");
                               1872                 :                : 
 2112 tgl@sss.pgh.pa.us        1873                 :CBC      545987 :     endpos = splitctx->match_locs[splitctx->next_match * 2];
                               1874         [ -  + ]:         545987 :     if (endpos < startpos)
 2112 tgl@sss.pgh.pa.us        1875         [ #  # ]:UBC           0 :         elog(ERROR, "invalid match starting position");
                               1876                 :                : 
 2618 rhodiumtoad@postgres     1877         [ +  - ]:CBC      545987 :     if (buf)
                               1878                 :                :     {
                               1879                 :                :         int         len;
                               1880                 :                : 
                               1881                 :         545987 :         len = pg_wchar2mb_with_len(splitctx->wide_str + startpos,
                               1882                 :                :                                    buf,
                               1883                 :                :                                    endpos - startpos);
 2112 tgl@sss.pgh.pa.us        1884         [ -  + ]:         545987 :         Assert(len < splitctx->conv_bufsiz);
 2618 rhodiumtoad@postgres     1885                 :         545987 :         return PointerGetDatum(cstring_to_text_with_len(buf, len));
                               1886                 :                :     }
                               1887                 :                :     else
                               1888                 :                :     {
 2618 rhodiumtoad@postgres     1889                 :UBC           0 :         return DirectFunctionCall3(text_substr,
                               1890                 :                :                                    PointerGetDatum(splitctx->orig_str),
                               1891                 :                :                                    Int32GetDatum(startpos + 1),
                               1892                 :                :                                    Int32GetDatum(endpos - startpos));
                               1893                 :                :     }
                               1894                 :                : }
                               1895                 :                : 
                               1896                 :                : /*
                               1897                 :                :  * regexp_substr()
                               1898                 :                :  *      Return the substring that matches a regular expression pattern
                               1899                 :                :  */
                               1900                 :                : Datum
 1547 tgl@sss.pgh.pa.us        1901                 :CBC          54 : regexp_substr(PG_FUNCTION_ARGS)
                               1902                 :                : {
                               1903                 :             54 :     text       *str = PG_GETARG_TEXT_PP(0);
                               1904                 :             54 :     text       *pattern = PG_GETARG_TEXT_PP(1);
                               1905                 :             54 :     int         start = 1;
                               1906                 :             54 :     int         n = 1;
                               1907         [ +  + ]:             54 :     text       *flags = PG_GETARG_TEXT_PP_IF_EXISTS(4);
                               1908                 :             54 :     int         subexpr = 0;
                               1909                 :                :     int         so,
                               1910                 :                :                 eo,
                               1911                 :                :                 pos;
                               1912                 :                :     pg_re_flags re_flags;
                               1913                 :                :     regexp_matches_ctx *matchctx;
                               1914                 :                : 
                               1915                 :                :     /* Collect optional parameters */
                               1916         [ +  + ]:             54 :     if (PG_NARGS() > 2)
                               1917                 :                :     {
                               1918                 :             45 :         start = PG_GETARG_INT32(2);
                               1919         [ +  + ]:             45 :         if (start <= 0)
                               1920         [ +  - ]:              3 :             ereport(ERROR,
                               1921                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1922                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1923                 :                :                             "start", start)));
                               1924                 :                :     }
                               1925         [ +  + ]:             51 :     if (PG_NARGS() > 3)
                               1926                 :                :     {
                               1927                 :             39 :         n = PG_GETARG_INT32(3);
                               1928         [ +  + ]:             39 :         if (n <= 0)
                               1929         [ +  - ]:              3 :             ereport(ERROR,
                               1930                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1931                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1932                 :                :                             "n", n)));
                               1933                 :                :     }
                               1934         [ +  + ]:             48 :     if (PG_NARGS() > 5)
                               1935                 :                :     {
                               1936                 :             24 :         subexpr = PG_GETARG_INT32(5);
                               1937         [ +  + ]:             24 :         if (subexpr < 0)
                               1938         [ +  - ]:              3 :             ereport(ERROR,
                               1939                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1940                 :                :                      errmsg("invalid value for parameter \"%s\": %d",
                               1941                 :                :                             "subexpr", subexpr)));
                               1942                 :                :     }
                               1943                 :                : 
                               1944                 :                :     /* Determine options */
                               1945                 :             45 :     parse_re_flags(&re_flags, flags);
                               1946                 :                :     /* User mustn't specify 'g' */
                               1947         [ +  + ]:             45 :     if (re_flags.glob)
                               1948         [ +  - ]:              3 :         ereport(ERROR,
                               1949                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               1950                 :                :         /* translator: %s is a SQL function name */
                               1951                 :                :                  errmsg("%s does not support the \"global\" option",
                               1952                 :                :                         "regexp_substr()")));
                               1953                 :                :     /* But we find all the matches anyway */
                               1954                 :             42 :     re_flags.glob = true;
                               1955                 :                : 
                               1956                 :                :     /* Do the matching */
                               1957                 :             42 :     matchctx = setup_regexp_matches(str, pattern, &re_flags, start - 1,
                               1958                 :                :                                     PG_GET_COLLATION(),
                               1959                 :                :                                     (subexpr > 0),   /* need submatches? */
                               1960                 :                :                                     false, false);
                               1961                 :                : 
                               1962                 :                :     /* When n exceeds matches return NULL (includes case of no matches) */
                               1963         [ +  + ]:             42 :     if (n > matchctx->nmatches)
                               1964                 :              6 :         PG_RETURN_NULL();
                               1965                 :                : 
                               1966                 :                :     /* When subexpr exceeds number of subexpressions return NULL */
                               1967         [ +  + ]:             36 :     if (subexpr > matchctx->npatterns)
                               1968                 :              3 :         PG_RETURN_NULL();
                               1969                 :                : 
                               1970                 :                :     /* Select the appropriate match position to return */
                               1971                 :             33 :     pos = (n - 1) * matchctx->npatterns;
                               1972         [ +  + ]:             33 :     if (subexpr > 0)
                               1973                 :             15 :         pos += subexpr - 1;
                               1974                 :             33 :     pos *= 2;
                               1975                 :             33 :     so = matchctx->match_locs[pos];
                               1976                 :             33 :     eo = matchctx->match_locs[pos + 1];
                               1977                 :                : 
                               1978   [ +  +  -  + ]:             33 :     if (so < 0 || eo < 0)
                               1979                 :              3 :         PG_RETURN_NULL();       /* unidentifiable location */
                               1980                 :                : 
                               1981                 :             30 :     PG_RETURN_DATUM(DirectFunctionCall3(text_substr,
                               1982                 :                :                                         PointerGetDatum(matchctx->orig_str),
                               1983                 :                :                                         Int32GetDatum(so + 1),
                               1984                 :                :                                         Int32GetDatum(eo - so)));
                               1985                 :                : }
                               1986                 :                : 
                               1987                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1988                 :                : Datum
                               1989                 :              9 : regexp_substr_no_start(PG_FUNCTION_ARGS)
                               1990                 :                : {
                               1991                 :              9 :     return regexp_substr(fcinfo);
                               1992                 :                : }
                               1993                 :                : 
                               1994                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               1995                 :                : Datum
                               1996                 :              3 : regexp_substr_no_n(PG_FUNCTION_ARGS)
                               1997                 :                : {
                               1998                 :              3 :     return regexp_substr(fcinfo);
                               1999                 :                : }
                               2000                 :                : 
                               2001                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               2002                 :                : Datum
                               2003                 :             12 : regexp_substr_no_flags(PG_FUNCTION_ARGS)
                               2004                 :                : {
                               2005                 :             12 :     return regexp_substr(fcinfo);
                               2006                 :                : }
                               2007                 :                : 
                               2008                 :                : /* This is separate to keep the opr_sanity regression test from complaining */
                               2009                 :                : Datum
                               2010                 :              6 : regexp_substr_no_subexpr(PG_FUNCTION_ARGS)
                               2011                 :                : {
                               2012                 :              6 :     return regexp_substr(fcinfo);
                               2013                 :                : }
                               2014                 :                : 
                               2015                 :                : /*
                               2016                 :                :  * regexp_fixed_prefix - extract fixed prefix, if any, for a regexp
                               2017                 :                :  *
                               2018                 :                :  * The result is NULL if there is no fixed prefix, else a palloc'd string.
                               2019                 :                :  * If it is an exact match, not just a prefix, *exact is returned as true.
                               2020                 :                :  */
                               2021                 :                : char *
 4858                          2022                 :           8269 : regexp_fixed_prefix(text *text_re, bool case_insensitive, Oid collation,
                               2023                 :                :                     bool *exact)
                               2024                 :                : {
                               2025                 :                :     char       *result;
                               2026                 :                :     regex_t    *re;
                               2027                 :                :     int         cflags;
                               2028                 :                :     int         re_result;
                               2029                 :                :     pg_wchar   *str;
                               2030                 :                :     size_t      slen;
                               2031                 :                :     size_t      maxlen;
                               2032                 :                :     char        errMsg[100];
                               2033                 :                : 
                               2034                 :           8269 :     *exact = false;             /* default result */
                               2035                 :                : 
                               2036                 :                :     /* Compile RE */
                               2037                 :           8269 :     cflags = REG_ADVANCED;
                               2038         [ +  + ]:           8269 :     if (case_insensitive)
                               2039                 :             31 :         cflags |= REG_ICASE;
                               2040                 :                : 
 1541                          2041                 :           8269 :     re = RE_compile_and_cache(text_re, cflags | REG_NOSUB, collation);
                               2042                 :                : 
                               2043                 :                :     /* Examine it to see if there's a fixed prefix */
 4858                          2044                 :           8257 :     re_result = pg_regprefix(re, &str, &slen);
                               2045                 :                : 
                               2046   [ +  +  +  - ]:           8257 :     switch (re_result)
                               2047                 :                :     {
                               2048                 :            382 :         case REG_NOMATCH:
                               2049                 :            382 :             return NULL;
                               2050                 :                : 
                               2051                 :           1474 :         case REG_PREFIX:
                               2052                 :                :             /* continue with wchar conversion */
                               2053                 :           1474 :             break;
                               2054                 :                : 
                               2055                 :           6401 :         case REG_EXACT:
                               2056                 :           6401 :             *exact = true;
                               2057                 :                :             /* continue with wchar conversion */
                               2058                 :           6401 :             break;
                               2059                 :                : 
 4858 tgl@sss.pgh.pa.us        2060                 :UBC           0 :         default:
                               2061                 :                :             /* re failed??? */
                               2062                 :              0 :             pg_regerror(re_result, re, errMsg, sizeof(errMsg));
                               2063         [ #  # ]:              0 :             ereport(ERROR,
                               2064                 :                :                     (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
                               2065                 :                :                      errmsg("regular expression failed: %s", errMsg)));
                               2066                 :                :             break;
                               2067                 :                :     }
                               2068                 :                : 
                               2069                 :                :     /* Convert pg_wchar result back to database encoding */
 4858 tgl@sss.pgh.pa.us        2070                 :CBC        7875 :     maxlen = pg_database_encoding_max_length() * slen + 1;
                               2071                 :           7875 :     result = (char *) palloc(maxlen);
                               2072                 :           7875 :     slen = pg_wchar2mb_with_len(str, result, slen);
                               2073         [ -  + ]:           7875 :     Assert(slen < maxlen);
                               2074                 :                : 
  934 tmunro@postgresql.or     2075                 :           7875 :     pfree(str);
                               2076                 :                : 
 4858 tgl@sss.pgh.pa.us        2077                 :           7875 :     return result;
                               2078                 :                : }
        

Generated by: LCOV version 2.4-beta