LCOV - code coverage report
Current view: top level - src/test/modules/test_regex - test_regex.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 267 309 86.4 %
Date: 2024-11-21 08:14:44 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*--------------------------------------------------------------------------
       2             :  *
       3             :  * test_regex.c
       4             :  *      Test harness for the regular expression package.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *      src/test/modules/test_regex/test_regex.c
      11             :  *
      12             :  * -------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres.h"
      16             : 
      17             : #include "funcapi.h"
      18             : #include "regex/regex.h"
      19             : #include "utils/array.h"
      20             : #include "utils/builtins.h"
      21             : 
      22           4 : PG_MODULE_MAGIC;
      23             : 
      24             : 
      25             : /* all the options of interest for regex functions */
      26             : typedef struct test_re_flags
      27             : {
      28             :     int         cflags;         /* compile flags for Spencer's regex code */
      29             :     int         eflags;         /* execute flags for Spencer's regex code */
      30             :     long        info;           /* expected re_info bits */
      31             :     bool        glob;           /* do it globally (for each occurrence) */
      32             :     bool        indices;        /* report indices not actual strings */
      33             :     bool        partial;        /* expect partial match */
      34             : } test_re_flags;
      35             : 
      36             : /* cross-call state for test_regex() */
      37             : typedef struct test_regex_ctx
      38             : {
      39             :     test_re_flags re_flags;     /* flags */
      40             :     rm_detail_t details;        /* "details" from execution */
      41             :     text       *orig_str;       /* data string in original TEXT form */
      42             :     int         nmatches;       /* number of places where pattern matched */
      43             :     int         npatterns;      /* number of capturing subpatterns */
      44             :     /* We store start char index and end+1 char index for each match */
      45             :     /* so the number of entries in match_locs is nmatches * npatterns * 2 */
      46             :     int        *match_locs;     /* 0-based character indexes */
      47             :     int         next_match;     /* 0-based index of next match to process */
      48             :     /* workspace for build_test_match_result() */
      49             :     Datum      *elems;          /* has npatterns+1 elements */
      50             :     bool       *nulls;          /* has npatterns+1 elements */
      51             :     pg_wchar   *wide_str;       /* wide-char version of original string */
      52             :     char       *conv_buf;       /* conversion buffer, if needed */
      53             :     int         conv_bufsiz;    /* size thereof */
      54             : } test_regex_ctx;
      55             : 
      56             : /* Local functions */
      57             : static void test_re_compile(text *text_re, int cflags, Oid collation,
      58             :                             regex_t *result_re);
      59             : static void parse_test_flags(test_re_flags *flags, text *opts);
      60             : static test_regex_ctx *setup_test_matches(text *orig_str,
      61             :                                           regex_t *cpattern,
      62             :                                           test_re_flags *re_flags,
      63             :                                           Oid collation,
      64             :                                           bool use_subpatterns);
      65             : static ArrayType *build_test_info_result(regex_t *cpattern,
      66             :                                          test_re_flags *flags);
      67             : static ArrayType *build_test_match_result(test_regex_ctx *matchctx);
      68             : 
      69             : 
      70             : /*
      71             :  * test_regex(pattern text, string text, flags text) returns setof text[]
      72             :  *
      73             :  * This is largely based on regexp.c's regexp_matches, with additions
      74             :  * for debugging purposes.
      75             :  */
      76           6 : PG_FUNCTION_INFO_V1(test_regex);
      77             : 
      78             : Datum
      79        3534 : test_regex(PG_FUNCTION_ARGS)
      80             : {
      81             :     FuncCallContext *funcctx;
      82             :     test_regex_ctx *matchctx;
      83             :     ArrayType  *result_ary;
      84             : 
      85        3534 :     if (SRF_IS_FIRSTCALL())
      86             :     {
      87        1392 :         text       *pattern = PG_GETARG_TEXT_PP(0);
      88        1392 :         text       *flags = PG_GETARG_TEXT_PP(2);
      89        1392 :         Oid         collation = PG_GET_COLLATION();
      90             :         test_re_flags re_flags;
      91             :         regex_t     cpattern;
      92             :         MemoryContext oldcontext;
      93             : 
      94        1392 :         funcctx = SRF_FIRSTCALL_INIT();
      95        1392 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
      96             : 
      97             :         /* Determine options */
      98        1392 :         parse_test_flags(&re_flags, flags);
      99             : 
     100             :         /* set up the compiled pattern */
     101        1392 :         test_re_compile(pattern, re_flags.cflags, collation, &cpattern);
     102             : 
     103             :         /* be sure to copy the input string into the multi-call ctx */
     104        1180 :         matchctx = setup_test_matches(PG_GETARG_TEXT_P_COPY(1), &cpattern,
     105             :                                       &re_flags,
     106             :                                       collation,
     107             :                                       true);
     108             : 
     109             :         /* Pre-create workspace that build_test_match_result needs */
     110        2360 :         matchctx->elems = (Datum *) palloc(sizeof(Datum) *
     111        1180 :                                            (matchctx->npatterns + 1));
     112        2360 :         matchctx->nulls = (bool *) palloc(sizeof(bool) *
     113        1180 :                                           (matchctx->npatterns + 1));
     114             : 
     115        1180 :         MemoryContextSwitchTo(oldcontext);
     116        1180 :         funcctx->user_fctx = (void *) matchctx;
     117             : 
     118             :         /*
     119             :          * Return the first result row, which is info equivalent to Tcl's
     120             :          * "regexp -about" output
     121             :          */
     122        1180 :         result_ary = build_test_info_result(&cpattern, &re_flags);
     123             : 
     124        1180 :         pg_regfree(&cpattern);
     125             : 
     126        1180 :         SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
     127             :     }
     128             :     else
     129             :     {
     130             :         /* Each subsequent row describes one match */
     131        2142 :         funcctx = SRF_PERCALL_SETUP();
     132        2142 :         matchctx = (test_regex_ctx *) funcctx->user_fctx;
     133             : 
     134        2142 :         if (matchctx->next_match < matchctx->nmatches)
     135             :         {
     136         962 :             result_ary = build_test_match_result(matchctx);
     137         962 :             matchctx->next_match++;
     138         962 :             SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
     139             :         }
     140             :     }
     141             : 
     142        1180 :     SRF_RETURN_DONE(funcctx);
     143             : }
     144             : 
     145             : 
     146             : /*
     147             :  * test_re_compile - compile a RE
     148             :  *
     149             :  *  text_re --- the pattern, expressed as a TEXT object
     150             :  *  cflags --- compile options for the pattern
     151             :  *  collation --- collation to use for LC_CTYPE-dependent behavior
     152             :  *  result_re --- output, compiled RE is stored here
     153             :  *
     154             :  * Pattern is given in the database encoding.  We internally convert to
     155             :  * an array of pg_wchar, which is what Spencer's regex package wants.
     156             :  *
     157             :  * Caller must eventually pg_regfree the resulting RE to avoid memory leaks.
     158             :  */
     159             : static void
     160        1392 : test_re_compile(text *text_re, int cflags, Oid collation,
     161             :                 regex_t *result_re)
     162             : {
     163        1392 :     int         text_re_len = VARSIZE_ANY_EXHDR(text_re);
     164        1392 :     char       *text_re_val = VARDATA_ANY(text_re);
     165             :     pg_wchar   *pattern;
     166             :     int         pattern_len;
     167             :     int         regcomp_result;
     168             :     char        errMsg[100];
     169             : 
     170             :     /* Convert pattern string to wide characters */
     171        1392 :     pattern = (pg_wchar *) palloc((text_re_len + 1) * sizeof(pg_wchar));
     172        1392 :     pattern_len = pg_mb2wchar_with_len(text_re_val,
     173             :                                        pattern,
     174             :                                        text_re_len);
     175             : 
     176        1392 :     regcomp_result = pg_regcomp(result_re,
     177             :                                 pattern,
     178             :                                 pattern_len,
     179             :                                 cflags,
     180             :                                 collation);
     181             : 
     182        1392 :     pfree(pattern);
     183             : 
     184        1392 :     if (regcomp_result != REG_OKAY)
     185             :     {
     186             :         /* re didn't compile (no need for pg_regfree, if so) */
     187         212 :         pg_regerror(regcomp_result, result_re, errMsg, sizeof(errMsg));
     188         212 :         ereport(ERROR,
     189             :                 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
     190             :                  errmsg("invalid regular expression: %s", errMsg)));
     191             :     }
     192        1180 : }
     193             : 
     194             : /*
     195             :  * test_re_execute - execute a RE on pg_wchar data
     196             :  *
     197             :  * Returns true on match, false on no match
     198             :  * Arguments are as for pg_regexec
     199             :  */
     200             : static bool
     201        1180 : test_re_execute(regex_t *re, pg_wchar *data, int data_len,
     202             :                 int start_search,
     203             :                 rm_detail_t *details,
     204             :                 int nmatch, regmatch_t *pmatch,
     205             :                 int eflags)
     206             : {
     207             :     int         regexec_result;
     208             :     char        errMsg[100];
     209             : 
     210             :     /* Initialize match locations in case engine doesn't */
     211        1180 :     details->rm_extend.rm_so = -1;
     212        1180 :     details->rm_extend.rm_eo = -1;
     213        2932 :     for (int i = 0; i < nmatch; i++)
     214             :     {
     215        1752 :         pmatch[i].rm_so = -1;
     216        1752 :         pmatch[i].rm_eo = -1;
     217             :     }
     218             : 
     219             :     /* Perform RE match and return result */
     220        1180 :     regexec_result = pg_regexec(re,
     221             :                                 data,
     222             :                                 data_len,
     223             :                                 start_search,
     224             :                                 details,
     225             :                                 nmatch,
     226             :                                 pmatch,
     227             :                                 eflags);
     228             : 
     229        1180 :     if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
     230             :     {
     231             :         /* re failed??? */
     232           0 :         pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
     233           0 :         ereport(ERROR,
     234             :                 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
     235             :                  errmsg("regular expression failed: %s", errMsg)));
     236             :     }
     237             : 
     238        1180 :     return (regexec_result == REG_OKAY);
     239             : }
     240             : 
     241             : 
     242             : /*
     243             :  * parse_test_flags - parse the flags argument
     244             :  *
     245             :  *  flags --- output argument, filled with desired options
     246             :  *  opts --- TEXT object, or NULL for defaults
     247             :  */
     248             : static void
     249        1392 : parse_test_flags(test_re_flags *flags, text *opts)
     250             : {
     251             :     /* these defaults must match Tcl's */
     252        1392 :     int         cflags = REG_ADVANCED;
     253        1392 :     int         eflags = 0;
     254        1392 :     long        info = 0;
     255             : 
     256        1392 :     flags->glob = false;
     257        1392 :     flags->indices = false;
     258        1392 :     flags->partial = false;
     259             : 
     260        1392 :     if (opts)
     261             :     {
     262        1392 :         char       *opt_p = VARDATA_ANY(opts);
     263        1392 :         int         opt_len = VARSIZE_ANY_EXHDR(opts);
     264             :         int         i;
     265             : 
     266        3782 :         for (i = 0; i < opt_len; i++)
     267             :         {
     268        2390 :             switch (opt_p[i])
     269             :             {
     270         156 :                 case '-':
     271             :                     /* allowed, no-op */
     272         156 :                     break;
     273          14 :                 case '!':
     274          14 :                     flags->partial = true;
     275          14 :                     break;
     276           2 :                 case '*':
     277             :                     /* test requires Unicode --- ignored here */
     278           2 :                     break;
     279         106 :                 case '0':
     280         106 :                     flags->indices = true;
     281         106 :                     break;
     282             : 
     283             :                     /* These flags correspond to user-exposed RE options: */
     284           0 :                 case 'g':       /* global match */
     285           0 :                     flags->glob = true;
     286           0 :                     break;
     287          40 :                 case 'i':       /* case insensitive */
     288          40 :                     cflags |= REG_ICASE;
     289          40 :                     break;
     290          70 :                 case 'n':       /* \n affects ^ $ . [^ */
     291          70 :                     cflags |= REG_NEWLINE;
     292          70 :                     break;
     293           4 :                 case 'p':       /* ~Perl, \n affects . [^ */
     294           4 :                     cflags |= REG_NLSTOP;
     295           4 :                     cflags &= ~REG_NLANCH;
     296           4 :                     break;
     297           4 :                 case 'w':       /* weird, \n affects ^ $ only */
     298           4 :                     cflags &= ~REG_NLSTOP;
     299           4 :                     cflags |= REG_NLANCH;
     300           4 :                     break;
     301          28 :                 case 'x':       /* expanded syntax */
     302          28 :                     cflags |= REG_EXPANDED;
     303          28 :                     break;
     304             : 
     305             :                     /* These flags correspond to Tcl's -xflags options: */
     306           4 :                 case 'a':
     307           4 :                     cflags |= REG_ADVF;
     308           4 :                     break;
     309         262 :                 case 'b':
     310         262 :                     cflags &= ~REG_ADVANCED;
     311         262 :                     break;
     312          22 :                 case 'c':
     313             : 
     314             :                     /*
     315             :                      * Tcl calls this TCL_REG_CANMATCH, but it's really
     316             :                      * REG_EXPECT.  In this implementation we must also set
     317             :                      * the partial and indices flags, so that
     318             :                      * setup_test_matches and build_test_match_result will
     319             :                      * emit the desired data.  (They'll emit more fields than
     320             :                      * Tcl would, but that's fine.)
     321             :                      */
     322          22 :                     cflags |= REG_EXPECT;
     323          22 :                     flags->partial = true;
     324          22 :                     flags->indices = true;
     325          22 :                     break;
     326          20 :                 case 'e':
     327          20 :                     cflags &= ~REG_ADVANCED;
     328          20 :                     cflags |= REG_EXTENDED;
     329          20 :                     break;
     330          12 :                 case 'q':
     331          12 :                     cflags &= ~REG_ADVANCED;
     332          12 :                     cflags |= REG_QUOTE;
     333          12 :                     break;
     334           4 :                 case 'o':       /* o for opaque */
     335           4 :                     cflags |= REG_NOSUB;
     336           4 :                     break;
     337           4 :                 case 's':       /* s for start */
     338           4 :                     cflags |= REG_BOSONLY;
     339           4 :                     break;
     340          12 :                 case '+':
     341          12 :                     cflags |= REG_FAKE;
     342          12 :                     break;
     343           0 :                 case ',':
     344           0 :                     cflags |= REG_PROGRESS;
     345           0 :                     break;
     346           0 :                 case '.':
     347           0 :                     cflags |= REG_DUMP;
     348           0 :                     break;
     349           0 :                 case ':':
     350           0 :                     eflags |= REG_MTRACE;
     351           0 :                     break;
     352           0 :                 case ';':
     353           0 :                     eflags |= REG_FTRACE;
     354           0 :                     break;
     355          12 :                 case '^':
     356          12 :                     eflags |= REG_NOTBOL;
     357          12 :                     break;
     358           8 :                 case '$':
     359           8 :                     eflags |= REG_NOTEOL;
     360           8 :                     break;
     361          34 :                 case 't':
     362          34 :                     cflags |= REG_EXPECT;
     363          34 :                     break;
     364          10 :                 case '%':
     365          10 :                     eflags |= REG_SMALL;
     366          10 :                     break;
     367             : 
     368             :                     /* These flags define expected info bits: */
     369          10 :                 case 'A':
     370          10 :                     info |= REG_UBSALNUM;
     371          10 :                     break;
     372           8 :                 case 'B':
     373           8 :                     info |= REG_UBRACES;
     374           8 :                     break;
     375          84 :                 case 'E':
     376          84 :                     info |= REG_UBBS;
     377          84 :                     break;
     378          68 :                 case 'H':
     379          68 :                     info |= REG_ULOOKAROUND;
     380          68 :                     break;
     381          22 :                 case 'I':
     382          22 :                     info |= REG_UIMPOSSIBLE;
     383          22 :                     break;
     384         328 :                 case 'L':
     385         328 :                     info |= REG_ULOCALE;
     386         328 :                     break;
     387          86 :                 case 'M':
     388          86 :                     info |= REG_UUNPORT;
     389          86 :                     break;
     390          94 :                 case 'N':
     391          94 :                     info |= REG_UEMPTYMATCH;
     392          94 :                     break;
     393         614 :                 case 'P':
     394         614 :                     info |= REG_UNONPOSIX;
     395         614 :                     break;
     396          72 :                 case 'Q':
     397          72 :                     info |= REG_UBOUNDS;
     398          72 :                     break;
     399          84 :                 case 'R':
     400          84 :                     info |= REG_UBACKREF;
     401          84 :                     break;
     402          50 :                 case 'S':
     403          50 :                     info |= REG_UUNSPEC;
     404          50 :                     break;
     405          40 :                 case 'T':
     406          40 :                     info |= REG_USHORTEST;
     407          40 :                     break;
     408           2 :                 case 'U':
     409           2 :                     info |= REG_UPBOTCH;
     410           2 :                     break;
     411             : 
     412           0 :                 default:
     413           0 :                     ereport(ERROR,
     414             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     415             :                              errmsg("invalid regular expression test option: \"%.*s\"",
     416             :                                     pg_mblen(opt_p + i), opt_p + i)));
     417             :                     break;
     418             :             }
     419             :         }
     420             :     }
     421        1392 :     flags->cflags = cflags;
     422        1392 :     flags->eflags = eflags;
     423        1392 :     flags->info = info;
     424        1392 : }
     425             : 
     426             : /*
     427             :  * setup_test_matches --- do the initial matching
     428             :  *
     429             :  * To simplify memory management, we do all the matching in one swoop.
     430             :  * The returned test_regex_ctx contains the locations of all the substrings
     431             :  * matching the pattern.
     432             :  */
     433             : static test_regex_ctx *
     434        1180 : setup_test_matches(text *orig_str,
     435             :                    regex_t *cpattern, test_re_flags *re_flags,
     436             :                    Oid collation,
     437             :                    bool use_subpatterns)
     438             : {
     439        1180 :     test_regex_ctx *matchctx = palloc0(sizeof(test_regex_ctx));
     440        1180 :     int         eml = pg_database_encoding_max_length();
     441             :     int         orig_len;
     442             :     pg_wchar   *wide_str;
     443             :     int         wide_len;
     444             :     regmatch_t *pmatch;
     445             :     int         pmatch_len;
     446             :     int         array_len;
     447             :     int         array_idx;
     448             :     int         prev_match_end;
     449             :     int         start_search;
     450        1180 :     int         maxlen = 0;     /* largest fetch length in characters */
     451             : 
     452             :     /* save flags */
     453        1180 :     matchctx->re_flags = *re_flags;
     454             : 
     455             :     /* save original string --- we'll extract result substrings from it */
     456        1180 :     matchctx->orig_str = orig_str;
     457             : 
     458             :     /* convert string to pg_wchar form for matching */
     459        1180 :     orig_len = VARSIZE_ANY_EXHDR(orig_str);
     460        1180 :     wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
     461        1180 :     wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
     462             : 
     463             :     /* do we want to remember subpatterns? */
     464        1180 :     if (use_subpatterns && cpattern->re_nsub > 0)
     465             :     {
     466         254 :         matchctx->npatterns = cpattern->re_nsub + 1;
     467         254 :         pmatch_len = cpattern->re_nsub + 1;
     468             :     }
     469             :     else
     470             :     {
     471         926 :         use_subpatterns = false;
     472         926 :         matchctx->npatterns = 1;
     473         926 :         pmatch_len = 1;
     474             :     }
     475             : 
     476             :     /* temporary output space for RE package */
     477        1180 :     pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
     478             : 
     479             :     /*
     480             :      * the real output space (grown dynamically if needed)
     481             :      *
     482             :      * use values 2^n-1, not 2^n, so that we hit the limit at 2^28-1 rather
     483             :      * than at 2^27
     484             :      */
     485        1180 :     array_len = re_flags->glob ? 255 : 31;
     486        1180 :     matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
     487        1180 :     array_idx = 0;
     488             : 
     489             :     /* search for the pattern, perhaps repeatedly */
     490        1180 :     prev_match_end = 0;
     491        1180 :     start_search = 0;
     492        1180 :     while (test_re_execute(cpattern, wide_str, wide_len,
     493             :                            start_search,
     494             :                            &matchctx->details,
     495             :                            pmatch_len, pmatch,
     496             :                            re_flags->eflags))
     497             :     {
     498             :         /* enlarge output space if needed */
     499         926 :         while (array_idx + matchctx->npatterns * 2 + 1 > array_len)
     500             :         {
     501           0 :             array_len += array_len + 1; /* 2^n-1 => 2^(n+1)-1 */
     502           0 :             if (array_len > MaxAllocSize / sizeof(int))
     503           0 :                 ereport(ERROR,
     504             :                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     505             :                          errmsg("too many regular expression matches")));
     506           0 :             matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
     507             :                                                     sizeof(int) * array_len);
     508             :         }
     509             : 
     510             :         /* save this match's locations */
     511        2188 :         for (int i = 0; i < matchctx->npatterns; i++)
     512             :         {
     513        1262 :             int         so = pmatch[i].rm_so;
     514        1262 :             int         eo = pmatch[i].rm_eo;
     515             : 
     516        1262 :             matchctx->match_locs[array_idx++] = so;
     517        1262 :             matchctx->match_locs[array_idx++] = eo;
     518        1262 :             if (so >= 0 && eo >= 0 && (eo - so) > maxlen)
     519         876 :                 maxlen = (eo - so);
     520             :         }
     521         926 :         matchctx->nmatches++;
     522         926 :         prev_match_end = pmatch[0].rm_eo;
     523             : 
     524             :         /* if not glob, stop after one match */
     525         926 :         if (!re_flags->glob)
     526         926 :             break;
     527             : 
     528             :         /*
     529             :          * Advance search position.  Normally we start the next search at the
     530             :          * end of the previous match; but if the match was of zero length, we
     531             :          * have to advance by one character, or we'd just find the same match
     532             :          * again.
     533             :          */
     534           0 :         start_search = prev_match_end;
     535           0 :         if (pmatch[0].rm_so == pmatch[0].rm_eo)
     536           0 :             start_search++;
     537           0 :         if (start_search > wide_len)
     538           0 :             break;
     539             :     }
     540             : 
     541             :     /*
     542             :      * If we had no match, but "partial" and "indices" are set, emit the
     543             :      * details.
     544             :      */
     545        1180 :     if (matchctx->nmatches == 0 && re_flags->partial && re_flags->indices)
     546             :     {
     547             :         /* enlarge output space if needed */
     548          36 :         while (array_idx + matchctx->npatterns * 2 + 1 > array_len)
     549             :         {
     550           0 :             array_len += array_len + 1; /* 2^n-1 => 2^(n+1)-1 */
     551           0 :             if (array_len > MaxAllocSize / sizeof(int))
     552           0 :                 ereport(ERROR,
     553             :                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     554             :                          errmsg("too many regular expression matches")));
     555           0 :             matchctx->match_locs = (int *) repalloc(matchctx->match_locs,
     556             :                                                     sizeof(int) * array_len);
     557             :         }
     558             : 
     559          36 :         matchctx->match_locs[array_idx++] = matchctx->details.rm_extend.rm_so;
     560          36 :         matchctx->match_locs[array_idx++] = matchctx->details.rm_extend.rm_eo;
     561             :         /* we don't have pmatch data, so emit -1 */
     562          40 :         for (int i = 1; i < matchctx->npatterns; i++)
     563             :         {
     564           4 :             matchctx->match_locs[array_idx++] = -1;
     565           4 :             matchctx->match_locs[array_idx++] = -1;
     566             :         }
     567          36 :         matchctx->nmatches++;
     568             :     }
     569             : 
     570             :     Assert(array_idx <= array_len);
     571             : 
     572        1180 :     if (eml > 1)
     573             :     {
     574        1180 :         int64       maxsiz = eml * (int64) maxlen;
     575             :         int         conv_bufsiz;
     576             : 
     577             :         /*
     578             :          * Make the conversion buffer large enough for any substring of
     579             :          * interest.
     580             :          *
     581             :          * Worst case: assume we need the maximum size (maxlen*eml), but take
     582             :          * advantage of the fact that the original string length in bytes is
     583             :          * an upper bound on the byte length of any fetched substring (and we
     584             :          * know that len+1 is safe to allocate because the varlena header is
     585             :          * longer than 1 byte).
     586             :          */
     587        1180 :         if (maxsiz > orig_len)
     588         830 :             conv_bufsiz = orig_len + 1;
     589             :         else
     590         350 :             conv_bufsiz = maxsiz + 1;   /* safe since maxsiz < 2^30 */
     591             : 
     592        1180 :         matchctx->conv_buf = palloc(conv_bufsiz);
     593        1180 :         matchctx->conv_bufsiz = conv_bufsiz;
     594        1180 :         matchctx->wide_str = wide_str;
     595             :     }
     596             :     else
     597             :     {
     598             :         /* No need to keep the wide string if we're in a single-byte charset. */
     599           0 :         pfree(wide_str);
     600           0 :         matchctx->wide_str = NULL;
     601           0 :         matchctx->conv_buf = NULL;
     602           0 :         matchctx->conv_bufsiz = 0;
     603             :     }
     604             : 
     605             :     /* Clean up temp storage */
     606        1180 :     pfree(pmatch);
     607             : 
     608        1180 :     return matchctx;
     609             : }
     610             : 
     611             : /*
     612             :  * build_test_info_result - build output array describing compiled regexp
     613             :  *
     614             :  * This borrows some code from Tcl's TclRegAbout().
     615             :  */
     616             : static ArrayType *
     617        1180 : build_test_info_result(regex_t *cpattern, test_re_flags *flags)
     618             : {
     619             :     /* Translation data for flag bits in regex_t.re_info */
     620             :     struct infoname
     621             :     {
     622             :         int         bit;
     623             :         const char *text;
     624             :     };
     625             :     static const struct infoname infonames[] = {
     626             :         {REG_UBACKREF, "REG_UBACKREF"},
     627             :         {REG_ULOOKAROUND, "REG_ULOOKAROUND"},
     628             :         {REG_UBOUNDS, "REG_UBOUNDS"},
     629             :         {REG_UBRACES, "REG_UBRACES"},
     630             :         {REG_UBSALNUM, "REG_UBSALNUM"},
     631             :         {REG_UPBOTCH, "REG_UPBOTCH"},
     632             :         {REG_UBBS, "REG_UBBS"},
     633             :         {REG_UNONPOSIX, "REG_UNONPOSIX"},
     634             :         {REG_UUNSPEC, "REG_UUNSPEC"},
     635             :         {REG_UUNPORT, "REG_UUNPORT"},
     636             :         {REG_ULOCALE, "REG_ULOCALE"},
     637             :         {REG_UEMPTYMATCH, "REG_UEMPTYMATCH"},
     638             :         {REG_UIMPOSSIBLE, "REG_UIMPOSSIBLE"},
     639             :         {REG_USHORTEST, "REG_USHORTEST"},
     640             :         {0, NULL}
     641             :     };
     642             :     const struct infoname *inf;
     643             :     Datum       elems[lengthof(infonames) + 1];
     644        1180 :     int         nresults = 0;
     645             :     char        buf[80];
     646             :     int         dims[1];
     647             :     int         lbs[1];
     648             : 
     649             :     /* Set up results: first, the number of subexpressions */
     650        1180 :     snprintf(buf, sizeof(buf), "%d", (int) cpattern->re_nsub);
     651        1180 :     elems[nresults++] = PointerGetDatum(cstring_to_text(buf));
     652             : 
     653             :     /* Report individual info bit states */
     654       17700 :     for (inf = infonames; inf->bit != 0; inf++)
     655             :     {
     656       16520 :         if (cpattern->re_info & inf->bit)
     657             :         {
     658        1516 :             if (flags->info & inf->bit)
     659        1516 :                 elems[nresults++] = PointerGetDatum(cstring_to_text(inf->text));
     660             :             else
     661             :             {
     662           0 :                 snprintf(buf, sizeof(buf), "unexpected %s!", inf->text);
     663           0 :                 elems[nresults++] = PointerGetDatum(cstring_to_text(buf));
     664             :             }
     665             :         }
     666             :         else
     667             :         {
     668       15004 :             if (flags->info & inf->bit)
     669             :             {
     670           0 :                 snprintf(buf, sizeof(buf), "missing %s!", inf->text);
     671           0 :                 elems[nresults++] = PointerGetDatum(cstring_to_text(buf));
     672             :             }
     673             :         }
     674             :     }
     675             : 
     676             :     /* And form an array */
     677        1180 :     dims[0] = nresults;
     678        1180 :     lbs[0] = 1;
     679             :     /* XXX: this hardcodes assumptions about the text type */
     680        1180 :     return construct_md_array(elems, NULL, 1, dims, lbs,
     681             :                               TEXTOID, -1, false, TYPALIGN_INT);
     682             : }
     683             : 
     684             : /*
     685             :  * build_test_match_result - build output array for current match
     686             :  *
     687             :  * Note that if the indices flag is set, we don't need any strings,
     688             :  * just the location data.
     689             :  */
     690             : static ArrayType *
     691         962 : build_test_match_result(test_regex_ctx *matchctx)
     692             : {
     693         962 :     char       *buf = matchctx->conv_buf;
     694         962 :     Datum      *elems = matchctx->elems;
     695         962 :     bool       *nulls = matchctx->nulls;
     696         962 :     bool        indices = matchctx->re_flags.indices;
     697             :     char        bufstr[80];
     698             :     int         dims[1];
     699             :     int         lbs[1];
     700             :     int         loc;
     701             :     int         i;
     702             : 
     703             :     /* Extract matching substrings from the original string */
     704         962 :     loc = matchctx->next_match * matchctx->npatterns * 2;
     705        2264 :     for (i = 0; i < matchctx->npatterns; i++)
     706             :     {
     707        1302 :         int         so = matchctx->match_locs[loc++];
     708        1302 :         int         eo = matchctx->match_locs[loc++];
     709             : 
     710        1302 :         if (indices)
     711             :         {
     712             :             /* Report eo this way for consistency with Tcl */
     713         168 :             snprintf(bufstr, sizeof(bufstr), "%d %d",
     714             :                      so, so < 0 ? eo : eo - 1);
     715         168 :             elems[i] = PointerGetDatum(cstring_to_text(bufstr));
     716         168 :             nulls[i] = false;
     717             :         }
     718        1134 :         else if (so < 0 || eo < 0)
     719             :         {
     720          24 :             elems[i] = (Datum) 0;
     721          24 :             nulls[i] = true;
     722             :         }
     723        1110 :         else if (buf)
     724             :         {
     725        1110 :             int         len = pg_wchar2mb_with_len(matchctx->wide_str + so,
     726             :                                                    buf,
     727             :                                                    eo - so);
     728             : 
     729             :             Assert(len < matchctx->conv_bufsiz);
     730        1110 :             elems[i] = PointerGetDatum(cstring_to_text_with_len(buf, len));
     731        1110 :             nulls[i] = false;
     732             :         }
     733             :         else
     734             :         {
     735           0 :             elems[i] = DirectFunctionCall3(text_substr,
     736             :                                            PointerGetDatum(matchctx->orig_str),
     737             :                                            Int32GetDatum(so + 1),
     738             :                                            Int32GetDatum(eo - so));
     739           0 :             nulls[i] = false;
     740             :         }
     741             :     }
     742             : 
     743             :     /* In EXPECT indices mode, also report the "details" */
     744         962 :     if (indices && (matchctx->re_flags.cflags & REG_EXPECT))
     745             :     {
     746          56 :         int         so = matchctx->details.rm_extend.rm_so;
     747          56 :         int         eo = matchctx->details.rm_extend.rm_eo;
     748             : 
     749          56 :         snprintf(bufstr, sizeof(bufstr), "%d %d",
     750             :                  so, so < 0 ? eo : eo - 1);
     751          56 :         elems[i] = PointerGetDatum(cstring_to_text(bufstr));
     752          56 :         nulls[i] = false;
     753          56 :         i++;
     754             :     }
     755             : 
     756             :     /* And form an array */
     757         962 :     dims[0] = i;
     758         962 :     lbs[0] = 1;
     759             :     /* XXX: this hardcodes assumptions about the text type */
     760         962 :     return construct_md_array(elems, nulls, 1, dims, lbs,
     761             :                               TEXTOID, -1, false, TYPALIGN_INT);
     762             : }

Generated by: LCOV version 1.14