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

Generated by: LCOV version 1.14