LCOV - code coverage report
Current view: top level - src/backend/regex - rege_dfa.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 83.9 % 552 463
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 13 13
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 67.4 % 430 290

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * DFA routines
       3                 :             :  * This file is #included by regexec.c.
       4                 :             :  *
       5                 :             :  * Copyright (c) 1998, 1999 Henry Spencer.  All rights reserved.
       6                 :             :  *
       7                 :             :  * Development of this software was funded, in part, by Cray Research Inc.,
       8                 :             :  * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
       9                 :             :  * Corporation, none of whom are responsible for the results.  The author
      10                 :             :  * thanks all of them.
      11                 :             :  *
      12                 :             :  * Redistribution and use in source and binary forms -- with or without
      13                 :             :  * modification -- are permitted for any purpose, provided that
      14                 :             :  * redistributions in source form retain this entire copyright notice and
      15                 :             :  * indicate the origin and nature of any modifications.
      16                 :             :  *
      17                 :             :  * I'd appreciate being given credit for this package in the documentation
      18                 :             :  * of software which uses it, but that is not a requirement.
      19                 :             :  *
      20                 :             :  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
      21                 :             :  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
      22                 :             :  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
      23                 :             :  * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      24                 :             :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      25                 :             :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
      26                 :             :  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
      27                 :             :  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
      28                 :             :  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
      29                 :             :  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30                 :             :  *
      31                 :             :  * src/backend/regex/rege_dfa.c
      32                 :             :  *
      33                 :             :  */
      34                 :             : 
      35                 :             : /*
      36                 :             :  * longest - longest-preferred matching engine
      37                 :             :  *
      38                 :             :  * On success, returns match endpoint address.  Returns NULL on no match.
      39                 :             :  * Internal errors also return NULL, with v->err set.
      40                 :             :  */
      41                 :             : static chr *
      42                 :       21995 : longest(struct vars *v,
      43                 :             :                 struct dfa *d,
      44                 :             :                 chr *start,                             /* where the match should start */
      45                 :             :                 chr *stop,                              /* match must end at or before here */
      46                 :             :                 int *hitstopp)                  /* record whether hit v->stop, if non-NULL */
      47                 :             : {
      48                 :       21995 :         chr                *cp;
      49         [ +  + ]:       21995 :         chr                *realstop = (stop == v->stop) ? stop : stop + 1;
      50                 :       21995 :         color           co;
      51                 :       21995 :         struct sset *css;
      52                 :       21995 :         struct sset *ss;
      53                 :       21995 :         chr                *post;
      54                 :       21995 :         int                     i;
      55                 :       21995 :         struct colormap *cm = d->cm;
      56                 :             : 
      57                 :             :         /* prevent "uninitialized variable" warnings */
      58         [ +  + ]:       21995 :         if (hitstopp != NULL)
      59                 :       17336 :                 *hitstopp = 0;
      60                 :             : 
      61                 :             :         /* if this is a backref to a known string, just match against that */
      62         [ +  + ]:       21995 :         if (d->backno >= 0)
      63                 :             :         {
      64         [ +  - ]:         203 :                 assert((size_t) d->backno < v->nmatch);
      65         [ +  + ]:         203 :                 if (v->pmatch[d->backno].rm_so >= 0)
      66                 :             :                 {
      67                 :         162 :                         cp = dfa_backref(v, d, start, start, stop, false);
      68   [ +  +  +  -  :         162 :                         if (cp == v->stop && stop == v->stop && hitstopp != NULL)
                   -  + ]
      69                 :           0 :                                 *hitstopp = 1;
      70                 :         162 :                         return cp;
      71                 :             :                 }
      72                 :          41 :         }
      73                 :             : 
      74                 :             :         /* fast path for matchall NFAs */
      75         [ +  + ]:       21833 :         if (d->cnfa->flags & MATCHALL)
      76                 :             :         {
      77                 :         438 :                 size_t          nchr = stop - start;
      78                 :         438 :                 size_t          maxmatchall = d->cnfa->maxmatchall;
      79                 :             : 
      80         [ +  + ]:         438 :                 if (nchr < d->cnfa->minmatchall)
      81                 :          41 :                         return NULL;
      82         [ +  + ]:         397 :                 if (maxmatchall == DUPINF)
      83                 :             :                 {
      84   [ +  +  +  + ]:         175 :                         if (stop == v->stop && hitstopp != NULL)
      85                 :           1 :                                 *hitstopp = 1;
      86                 :         175 :                 }
      87                 :             :                 else
      88                 :             :                 {
      89   [ +  +  +  +  :         222 :                         if (stop == v->stop && nchr <= maxmatchall + 1 && hitstopp != NULL)
                   +  + ]
      90                 :          23 :                                 *hitstopp = 1;
      91         [ +  + ]:         222 :                         if (nchr > maxmatchall)
      92                 :         161 :                                 return start + maxmatchall;
      93                 :             :                 }
      94                 :         236 :                 return stop;
      95                 :         438 :         }
      96                 :             : 
      97                 :             :         /* initialize */
      98                 :       21395 :         css = initialize(v, d, start);
      99         [ -  + ]:       21395 :         if (css == NULL)
     100                 :           0 :                 return NULL;
     101                 :       21395 :         cp = start;
     102                 :             : 
     103                 :             :         /* startup */
     104                 :             :         FDEBUG(("+++ startup +++\n"));
     105         [ +  + ]:       21395 :         if (cp == v->start)
     106                 :             :         {
     107                 :         335 :                 co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
     108                 :             :                 FDEBUG(("color %ld\n", (long) co));
     109                 :         335 :         }
     110                 :             :         else
     111                 :             :         {
     112         [ +  - ]:       21060 :                 co = GETCOLOR(cm, *(cp - 1));
     113                 :             :                 FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co));
     114                 :             :         }
     115                 :       21395 :         css = miss(v, d, css, co, cp, start);
     116         [ +  + ]:       21395 :         if (css == NULL)
     117                 :          54 :                 return NULL;
     118                 :       21341 :         css->lastseen = cp;
     119                 :             : 
     120                 :             :         /*
     121                 :             :          * This is the main text-scanning loop.  It seems worth having two copies
     122                 :             :          * to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
     123                 :             :          * builds, when you're not actively tracing.
     124                 :             :          */
     125                 :             : #ifdef REG_DEBUG
     126                 :             :         if (v->eflags & REG_FTRACE)
     127                 :             :         {
     128                 :             :                 while (cp < realstop)
     129                 :             :                 {
     130                 :             :                         FDEBUG(("+++ at c%d +++\n", (int) (css - d->ssets)));
     131                 :             :                         co = GETCOLOR(cm, *cp);
     132                 :             :                         FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
     133                 :             :                         ss = css->outs[co];
     134                 :             :                         if (ss == NULL)
     135                 :             :                         {
     136                 :             :                                 ss = miss(v, d, css, co, cp + 1, start);
     137                 :             :                                 if (ss == NULL)
     138                 :             :                                         break;          /* NOTE BREAK OUT */
     139                 :             :                         }
     140                 :             :                         cp++;
     141                 :             :                         ss->lastseen = cp;
     142                 :             :                         css = ss;
     143                 :             :                 }
     144                 :             :         }
     145                 :             :         else
     146                 :             : #endif
     147                 :             :         {
     148         [ +  + ]:      157140 :                 while (cp < realstop)
     149                 :             :                 {
     150         [ +  - ]:      153353 :                         co = GETCOLOR(cm, *cp);
     151                 :      153353 :                         ss = css->outs[co];
     152         [ +  + ]:      153353 :                         if (ss == NULL)
     153                 :             :                         {
     154                 :       93918 :                                 ss = miss(v, d, css, co, cp + 1, start);
     155         [ +  + ]:       93918 :                                 if (ss == NULL)
     156                 :       17554 :                                         break;          /* NOTE BREAK OUT */
     157                 :       76364 :                         }
     158                 :      135799 :                         cp++;
     159                 :      135799 :                         ss->lastseen = cp;
     160                 :      135799 :                         css = ss;
     161                 :             :                 }
     162                 :             :         }
     163                 :             : 
     164         [ -  + ]:       21341 :         if (ISERR())
     165                 :           0 :                 return NULL;
     166                 :             : 
     167                 :             :         /* shutdown */
     168                 :             :         FDEBUG(("+++ shutdown at c%d +++\n", (int) (css - d->ssets)));
     169   [ +  +  +  + ]:       21341 :         if (cp == v->stop && stop == v->stop)
     170                 :             :         {
     171         [ +  + ]:        1884 :                 if (hitstopp != NULL)
     172                 :        1051 :                         *hitstopp = 1;
     173                 :        1884 :                 co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
     174                 :             :                 FDEBUG(("color %ld\n", (long) co));
     175                 :        1884 :                 ss = miss(v, d, css, co, cp, start);
     176         [ -  + ]:        1884 :                 if (ISERR())
     177                 :           0 :                         return NULL;
     178                 :             :                 /* special case:  match ended at eol? */
     179   [ +  +  -  + ]:        1884 :                 if (ss != NULL && (ss->flags & POSTSTATE))
     180                 :        1051 :                         return cp;
     181         [ +  - ]:         833 :                 else if (ss != NULL)
     182                 :           0 :                         ss->lastseen = cp;   /* to be tidy */
     183                 :         833 :         }
     184                 :             : 
     185                 :             :         /* find last match, if any */
     186                 :       20290 :         post = d->lastpost;
     187         [ +  + ]:      120202 :         for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
     188   [ +  +  +  +  :      102173 :                 if ((ss->flags & POSTSTATE) && post != ss->lastseen &&
                   +  + ]
     189         [ +  + ]:      122066 :                         (post == NULL || post < ss->lastseen))
     190                 :       22154 :                         post = ss->lastseen;
     191         [ +  + ]:       20290 :         if (post != NULL)                       /* found one */
     192                 :       19974 :                 return post - 1;
     193                 :             : 
     194                 :         316 :         return NULL;
     195                 :       21995 : }
     196                 :             : 
     197                 :             : /*
     198                 :             :  * shortest - shortest-preferred matching engine
     199                 :             :  *
     200                 :             :  * On success, returns match endpoint address.  Returns NULL on no match.
     201                 :             :  * Internal errors also return NULL, with v->err set.
     202                 :             :  */
     203                 :             : static chr *
     204                 :     1158686 : shortest(struct vars *v,
     205                 :             :                  struct dfa *d,
     206                 :             :                  chr *start,                    /* where the match should start */
     207                 :             :                  chr *min,                              /* match must end at or after here */
     208                 :             :                  chr *max,                              /* match must end at or before here */
     209                 :             :                  chr **coldp,                   /* store coldstart pointer here, if non-NULL */
     210                 :             :                  int *hitstopp)                 /* record whether hit v->stop, if non-NULL */
     211                 :             : {
     212                 :     1158686 :         chr                *cp;
     213         [ +  + ]:     1158686 :         chr                *realmin = (min == v->stop) ? min : min + 1;
     214         [ +  + ]:     1158686 :         chr                *realmax = (max == v->stop) ? max : max + 1;
     215                 :     1158686 :         color           co;
     216                 :     1158686 :         struct sset *css;
     217                 :     1158686 :         struct sset *ss;
     218                 :     1158686 :         struct colormap *cm = d->cm;
     219                 :             : 
     220                 :             :         /* prevent "uninitialized variable" warnings */
     221         [ +  + ]:     1158686 :         if (coldp != NULL)
     222                 :     1158447 :                 *coldp = NULL;
     223         [ +  + ]:     1158686 :         if (hitstopp != NULL)
     224                 :          46 :                 *hitstopp = 0;
     225                 :             : 
     226                 :             :         /* if this is a backref to a known string, just match against that */
     227         [ +  - ]:     1158686 :         if (d->backno >= 0)
     228                 :             :         {
     229         [ #  # ]:           0 :                 assert((size_t) d->backno < v->nmatch);
     230         [ #  # ]:           0 :                 if (v->pmatch[d->backno].rm_so >= 0)
     231                 :             :                 {
     232                 :           0 :                         cp = dfa_backref(v, d, start, min, max, true);
     233   [ #  #  #  # ]:           0 :                         if (cp != NULL && coldp != NULL)
     234                 :           0 :                                 *coldp = start;
     235                 :             :                         /* there is no case where we should set *hitstopp */
     236                 :           0 :                         return cp;
     237                 :             :                 }
     238                 :           0 :         }
     239                 :             : 
     240                 :             :         /* fast path for matchall NFAs */
     241         [ +  + ]:     1158686 :         if (d->cnfa->flags & MATCHALL)
     242                 :             :         {
     243                 :         303 :                 size_t          nchr = min - start;
     244                 :             : 
     245   [ +  +  +  - ]:         303 :                 if (d->cnfa->maxmatchall != DUPINF &&
     246                 :           4 :                         nchr > d->cnfa->maxmatchall)
     247                 :           0 :                         return NULL;
     248         [ +  + ]:         303 :                 if ((max - start) < d->cnfa->minmatchall)
     249                 :           3 :                         return NULL;
     250         [ +  + ]:         300 :                 if (nchr < d->cnfa->minmatchall)
     251                 :          17 :                         min = start + d->cnfa->minmatchall;
     252         [ +  + ]:         300 :                 if (coldp != NULL)
     253                 :         143 :                         *coldp = start;
     254                 :             :                 /* there is no case where we should set *hitstopp */
     255                 :         300 :                 return min;
     256                 :         303 :         }
     257                 :             : 
     258                 :             :         /* initialize */
     259                 :     1158383 :         css = initialize(v, d, start);
     260         [ +  - ]:     1158383 :         if (css == NULL)
     261                 :           0 :                 return NULL;
     262                 :     1158383 :         cp = start;
     263                 :             : 
     264                 :             :         /* startup */
     265                 :             :         FDEBUG(("--- startup ---\n"));
     266         [ +  + ]:     1158383 :         if (cp == v->start)
     267                 :             :         {
     268                 :     1142307 :                 co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
     269                 :             :                 FDEBUG(("color %ld\n", (long) co));
     270                 :     1142307 :         }
     271                 :             :         else
     272                 :             :         {
     273         [ +  - ]:       16076 :                 co = GETCOLOR(cm, *(cp - 1));
     274                 :             :                 FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co));
     275                 :             :         }
     276                 :     1158383 :         css = miss(v, d, css, co, cp, start);
     277         [ +  + ]:     1158383 :         if (css == NULL)
     278                 :           1 :                 return NULL;
     279                 :     1158382 :         css->lastseen = cp;
     280                 :     1158382 :         ss = css;
     281                 :             : 
     282                 :             :         /*
     283                 :             :          * This is the main text-scanning loop.  It seems worth having two copies
     284                 :             :          * to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
     285                 :             :          * builds, when you're not actively tracing.
     286                 :             :          */
     287                 :             : #ifdef REG_DEBUG
     288                 :             :         if (v->eflags & REG_FTRACE)
     289                 :             :         {
     290                 :             :                 while (cp < realmax)
     291                 :             :                 {
     292                 :             :                         FDEBUG(("--- at c%d ---\n", (int) (css - d->ssets)));
     293                 :             :                         co = GETCOLOR(cm, *cp);
     294                 :             :                         FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
     295                 :             :                         ss = css->outs[co];
     296                 :             :                         if (ss == NULL)
     297                 :             :                         {
     298                 :             :                                 ss = miss(v, d, css, co, cp + 1, start);
     299                 :             :                                 if (ss == NULL)
     300                 :             :                                         break;          /* NOTE BREAK OUT */
     301                 :             :                         }
     302                 :             :                         cp++;
     303                 :             :                         ss->lastseen = cp;
     304                 :             :                         css = ss;
     305                 :             :                         if ((ss->flags & POSTSTATE) && cp >= realmin)
     306                 :             :                                 break;                  /* NOTE BREAK OUT */
     307                 :             :                 }
     308                 :             :         }
     309                 :             :         else
     310                 :             : #endif
     311                 :             :         {
     312         [ +  + ]:     2468698 :                 while (cp < realmax)
     313                 :             :                 {
     314         [ +  + ]:     2425156 :                         co = GETCOLOR(cm, *cp);
     315                 :     2425156 :                         ss = css->outs[co];
     316         [ +  + ]:     2425156 :                         if (ss == NULL)
     317                 :             :                         {
     318                 :     1541684 :                                 ss = miss(v, d, css, co, cp + 1, start);
     319         [ +  + ]:     1541684 :                                 if (ss == NULL)
     320                 :     1095851 :                                         break;          /* NOTE BREAK OUT */
     321                 :      445833 :                         }
     322                 :     1329305 :                         cp++;
     323                 :     1329305 :                         ss->lastseen = cp;
     324                 :     1329305 :                         css = ss;
     325   [ +  +  +  + ]:     1329305 :                         if ((ss->flags & POSTSTATE) && cp >= realmin)
     326                 :       18989 :                                 break;                  /* NOTE BREAK OUT */
     327                 :             :                 }
     328                 :             :         }
     329                 :             : 
     330         [ +  + ]:     1158382 :         if (ss == NULL)
     331                 :     1095851 :                 return NULL;
     332                 :             : 
     333         [ +  + ]:       62531 :         if (coldp != NULL)                      /* report last no-progress state set, if any */
     334                 :       62455 :                 *coldp = lastcold(v, d);
     335                 :             : 
     336   [ +  +  +  + ]:       62531 :         if ((ss->flags & POSTSTATE) && cp > min)
     337                 :             :         {
     338         [ +  - ]:       18985 :                 assert(cp >= realmin);
     339                 :       18985 :                 cp--;
     340                 :       18985 :         }
     341   [ +  +  +  + ]:       43546 :         else if (cp == v->stop && max == v->stop)
     342                 :             :         {
     343                 :       43538 :                 co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
     344                 :             :                 FDEBUG(("color %ld\n", (long) co));
     345                 :       43538 :                 ss = miss(v, d, css, co, cp, start);
     346                 :             :                 /* match might have ended at eol */
     347   [ +  +  +  + ]:       43538 :                 if ((ss == NULL || !(ss->flags & POSTSTATE)) && hitstopp != NULL)
     348                 :           2 :                         *hitstopp = 1;
     349                 :       43542 :         }
     350                 :             : 
     351   [ +  +  -  + ]:       62527 :         if (ss == NULL || !(ss->flags & POSTSTATE))
     352                 :       41323 :                 return NULL;
     353                 :             : 
     354                 :       21204 :         return cp;
     355                 :     1158682 : }
     356                 :             : 
     357                 :             : /*
     358                 :             :  * matchuntil - incremental matching engine
     359                 :             :  *
     360                 :             :  * This is meant for use with a search-style NFA (that is, the pattern is
     361                 :             :  * known to act as though it had a leading .*).  We determine whether a
     362                 :             :  * match exists starting at v->start and ending at probe.  Multiple calls
     363                 :             :  * require only O(N) time not O(N^2) so long as the probe values are
     364                 :             :  * nondecreasing.  *lastcss and *lastcp must be initialized to NULL before
     365                 :             :  * starting a series of calls.
     366                 :             :  *
     367                 :             :  * Returns 1 if a match exists, 0 if not.
     368                 :             :  * Internal errors also return 0, with v->err set.
     369                 :             :  */
     370                 :             : static int
     371                 :          14 : matchuntil(struct vars *v,
     372                 :             :                    struct dfa *d,
     373                 :             :                    chr *probe,                  /* we want to know if a match ends here */
     374                 :             :                    struct sset **lastcss,       /* state storage across calls */
     375                 :             :                    chr **lastcp)                /* state storage across calls */
     376                 :             : {
     377                 :          14 :         chr                *cp = *lastcp;
     378                 :          14 :         color           co;
     379                 :          14 :         struct sset *css = *lastcss;
     380                 :          14 :         struct sset *ss;
     381                 :          14 :         struct colormap *cm = d->cm;
     382                 :             : 
     383                 :             :         /* fast path for matchall NFAs */
     384         [ +  - ]:          14 :         if (d->cnfa->flags & MATCHALL)
     385                 :             :         {
     386                 :           0 :                 size_t          nchr = probe - v->start;
     387                 :             : 
     388         [ #  # ]:           0 :                 if (nchr < d->cnfa->minmatchall)
     389                 :           0 :                         return 0;
     390                 :             :                 /* maxmatchall will always be infinity, cf. makesearch() */
     391         [ #  # ]:           0 :                 assert(d->cnfa->maxmatchall == DUPINF);
     392                 :           0 :                 return 1;
     393                 :           0 :         }
     394                 :             : 
     395                 :             :         /* initialize and startup, or restart, if necessary */
     396   [ +  +  +  + ]:          14 :         if (cp == NULL || cp > probe)
     397                 :             :         {
     398                 :           4 :                 cp = v->start;
     399                 :           4 :                 css = initialize(v, d, cp);
     400         [ +  - ]:           4 :                 if (css == NULL)
     401                 :           0 :                         return 0;
     402                 :             : 
     403                 :             :                 FDEBUG((">>> startup >>>\n"));
     404                 :           4 :                 co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
     405                 :             :                 FDEBUG(("color %ld\n", (long) co));
     406                 :             : 
     407                 :           4 :                 css = miss(v, d, css, co, cp, v->start);
     408         [ +  - ]:           4 :                 if (css == NULL)
     409                 :           0 :                         return 0;
     410                 :           4 :                 css->lastseen = cp;
     411                 :           4 :         }
     412         [ +  - ]:          10 :         else if (css == NULL)
     413                 :             :         {
     414                 :             :                 /* we previously found that no match is possible beyond *lastcp */
     415                 :           0 :                 return 0;
     416                 :             :         }
     417                 :          14 :         ss = css;
     418                 :             : 
     419                 :             :         /*
     420                 :             :          * This is the main text-scanning loop.  It seems worth having two copies
     421                 :             :          * to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
     422                 :             :          * builds, when you're not actively tracing.
     423                 :             :          */
     424                 :             : #ifdef REG_DEBUG
     425                 :             :         if (v->eflags & REG_FTRACE)
     426                 :             :         {
     427                 :             :                 while (cp < probe)
     428                 :             :                 {
     429                 :             :                         FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
     430                 :             :                         co = GETCOLOR(cm, *cp);
     431                 :             :                         FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
     432                 :             :                         ss = css->outs[co];
     433                 :             :                         if (ss == NULL)
     434                 :             :                         {
     435                 :             :                                 ss = miss(v, d, css, co, cp + 1, v->start);
     436                 :             :                                 if (ss == NULL)
     437                 :             :                                         break;          /* NOTE BREAK OUT */
     438                 :             :                         }
     439                 :             :                         cp++;
     440                 :             :                         ss->lastseen = cp;
     441                 :             :                         css = ss;
     442                 :             :                 }
     443                 :             :         }
     444                 :             :         else
     445                 :             : #endif
     446                 :             :         {
     447         [ +  + ]:          30 :                 while (cp < probe)
     448                 :             :                 {
     449         [ +  - ]:          16 :                         co = GETCOLOR(cm, *cp);
     450                 :          16 :                         ss = css->outs[co];
     451         [ +  + ]:          16 :                         if (ss == NULL)
     452                 :             :                         {
     453                 :           2 :                                 ss = miss(v, d, css, co, cp + 1, v->start);
     454         [ -  + ]:           2 :                                 if (ss == NULL)
     455                 :           0 :                                         break;          /* NOTE BREAK OUT */
     456                 :           2 :                         }
     457                 :          16 :                         cp++;
     458                 :          16 :                         ss->lastseen = cp;
     459                 :          16 :                         css = ss;
     460                 :             :                 }
     461                 :             :         }
     462                 :             : 
     463                 :          14 :         *lastcss = ss;
     464                 :          14 :         *lastcp = cp;
     465                 :             : 
     466         [ +  - ]:          14 :         if (ss == NULL)
     467                 :           0 :                 return 0;                               /* impossible match, or internal error */
     468                 :             : 
     469                 :             :         /* We need to process one more chr, or the EOS symbol, to check match */
     470         [ +  - ]:          14 :         if (cp < v->stop)
     471                 :             :         {
     472                 :             :                 FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
     473         [ +  - ]:          14 :                 co = GETCOLOR(cm, *cp);
     474                 :             :                 FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
     475                 :          14 :                 ss = css->outs[co];
     476         [ +  + ]:          14 :                 if (ss == NULL)
     477                 :           9 :                         ss = miss(v, d, css, co, cp + 1, v->start);
     478                 :          14 :         }
     479                 :             :         else
     480                 :             :         {
     481         [ #  # ]:           0 :                 assert(cp == v->stop);
     482                 :           0 :                 co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
     483                 :             :                 FDEBUG(("color %ld\n", (long) co));
     484                 :           0 :                 ss = miss(v, d, css, co, cp, v->start);
     485                 :             :         }
     486                 :             : 
     487   [ +  -  +  + ]:          14 :         if (ss == NULL || !(ss->flags & POSTSTATE))
     488                 :          10 :                 return 0;
     489                 :             : 
     490                 :           4 :         return 1;
     491                 :          14 : }
     492                 :             : 
     493                 :             : /*
     494                 :             :  * dfa_backref - find best match length for a known backref string
     495                 :             :  *
     496                 :             :  * When the backref's referent is already available, we can deliver an exact
     497                 :             :  * answer with considerably less work than running the backref node's NFA.
     498                 :             :  *
     499                 :             :  * Return match endpoint for longest or shortest valid repeated match,
     500                 :             :  * or NULL if there is no valid match.
     501                 :             :  *
     502                 :             :  * Should be in sync with cbrdissect(), although that has the different task
     503                 :             :  * of checking a match to a predetermined section of the string.
     504                 :             :  */
     505                 :             : static chr *
     506                 :         162 : dfa_backref(struct vars *v,
     507                 :             :                         struct dfa *d,
     508                 :             :                         chr *start,                     /* where the match should start */
     509                 :             :                         chr *min,                       /* match must end at or after here */
     510                 :             :                         chr *max,                       /* match must end at or before here */
     511                 :             :                         bool shortest)
     512                 :             : {
     513                 :         162 :         int                     n = d->backno;
     514                 :         162 :         int                     backmin = d->backmin;
     515                 :         162 :         int                     backmax = d->backmax;
     516                 :         162 :         size_t          numreps;
     517                 :         162 :         size_t          minreps;
     518                 :         162 :         size_t          maxreps;
     519                 :         162 :         size_t          brlen;
     520                 :         162 :         chr                *brstring;
     521                 :         162 :         chr                *p;
     522                 :             : 
     523                 :             :         /* get the backreferenced string (caller should have checked this) */
     524         [ +  - ]:         162 :         if (v->pmatch[n].rm_so == -1)
     525                 :           0 :                 return NULL;
     526                 :         162 :         brstring = v->start + v->pmatch[n].rm_so;
     527                 :         162 :         brlen = v->pmatch[n].rm_eo - v->pmatch[n].rm_so;
     528                 :             : 
     529                 :             :         /* special-case zero-length backreference to avoid divide by zero */
     530         [ +  - ]:         162 :         if (brlen == 0)
     531                 :             :         {
     532                 :             :                 /*
     533                 :             :                  * matches only a zero-length string, but any number of repetitions
     534                 :             :                  * can be considered to be present
     535                 :             :                  */
     536   [ #  #  #  # ]:           0 :                 if (min == start && backmin <= backmax)
     537                 :           0 :                         return start;
     538                 :           0 :                 return NULL;
     539                 :             :         }
     540                 :             : 
     541                 :             :         /*
     542                 :             :          * convert min and max into numbers of possible repetitions of the backref
     543                 :             :          * string, rounding appropriately
     544                 :             :          */
     545         [ +  - ]:         162 :         if (min <= start)
     546                 :         162 :                 minreps = 0;
     547                 :             :         else
     548                 :           0 :                 minreps = (min - start - 1) / brlen + 1;
     549                 :         162 :         maxreps = (max - start) / brlen;
     550                 :             : 
     551                 :             :         /* apply bounds, then see if there is any allowed match length */
     552         [ +  + ]:         162 :         if (minreps < backmin)
     553                 :         157 :                 minreps = backmin;
     554   [ +  +  +  + ]:         162 :         if (backmax != DUPINF && maxreps > backmax)
     555                 :          87 :                 maxreps = backmax;
     556         [ +  + ]:         162 :         if (maxreps < minreps)
     557                 :          30 :                 return NULL;
     558                 :             : 
     559                 :             :         /* quick exit if zero-repetitions match is valid and preferred */
     560   [ -  +  #  # ]:         132 :         if (shortest && minreps == 0)
     561                 :           0 :                 return start;
     562                 :             : 
     563                 :             :         /* okay, compare the actual string contents */
     564                 :         132 :         p = start;
     565                 :         132 :         numreps = 0;
     566         [ +  + ]:         150 :         while (numreps < maxreps)
     567                 :             :         {
     568         [ +  + ]:         135 :                 if ((*v->g->compare) (brstring, p, brlen) != 0)
     569                 :         117 :                         break;
     570                 :          18 :                 p += brlen;
     571                 :          18 :                 numreps++;
     572   [ -  +  #  # ]:          18 :                 if (shortest && numreps >= minreps)
     573                 :           0 :                         break;
     574                 :             :         }
     575                 :             : 
     576         [ +  + ]:         132 :         if (numreps >= minreps)
     577                 :          16 :                 return p;
     578                 :         116 :         return NULL;
     579                 :         162 : }
     580                 :             : 
     581                 :             : /*
     582                 :             :  * lastcold - determine last point at which no progress had been made
     583                 :             :  */
     584                 :             : static chr *                                    /* endpoint, or NULL */
     585                 :       62455 : lastcold(struct vars *v,
     586                 :             :                  struct dfa *d)
     587                 :             : {
     588                 :       62455 :         struct sset *ss;
     589                 :       62455 :         chr                *nopr;
     590                 :       62455 :         int                     i;
     591                 :             : 
     592                 :       62455 :         nopr = d->lastnopr;
     593         [ -  + ]:       62455 :         if (nopr == NULL)
     594                 :       62455 :                 nopr = v->start;
     595         [ +  + ]:      311425 :         for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
     596   [ +  +  +  + ]:      300324 :                 if ((ss->flags & NOPROGRESS) && nopr < ss->lastseen)
     597                 :       51354 :                         nopr = ss->lastseen;
     598                 :      124910 :         return nopr;
     599                 :       62455 : }
     600                 :             : 
     601                 :             : /*
     602                 :             :  * newdfa - set up a fresh DFA
     603                 :             :  *
     604                 :             :  * Returns NULL (and sets v->err) on failure.
     605                 :             :  */
     606                 :             : static struct dfa *
     607                 :     1179603 : newdfa(struct vars *v,
     608                 :             :            struct cnfa *cnfa,
     609                 :             :            struct colormap *cm,
     610                 :             :            struct smalldfa *sml)        /* preallocated space, may be NULL */
     611                 :             : {
     612                 :     1179603 :         struct dfa *d;
     613                 :     1179603 :         size_t          nss = cnfa->nstates * 2;
     614                 :     1179603 :         int                     wordsper = (cnfa->nstates + UBITS - 1) / UBITS;
     615                 :     1179603 :         bool            ismalloced = false;
     616                 :             : 
     617         [ +  - ]:     1179603 :         assert(cnfa != NULL && cnfa->nstates != 0);
     618                 :             : 
     619   [ +  +  +  + ]:     1179603 :         if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS)
     620                 :             :         {
     621         [ +  - ]:      413934 :                 assert(wordsper == 1);
     622         [ +  + ]:      413934 :                 if (sml == NULL)
     623                 :             :                 {
     624                 :        3676 :                         sml = (struct smalldfa *) MALLOC(sizeof(struct smalldfa));
     625         [ +  - ]:        3676 :                         if (sml == NULL)
     626                 :             :                         {
     627         [ #  # ]:           0 :                                 ERR(REG_ESPACE);
     628                 :           0 :                                 return NULL;
     629                 :             :                         }
     630                 :        3676 :                         ismalloced = true;
     631                 :        3676 :                 }
     632                 :      413934 :                 d = &sml->dfa;
     633                 :      413934 :                 d->ssets = sml->ssets;
     634                 :      413934 :                 d->statesarea = sml->statesarea;
     635                 :      413934 :                 d->work = &d->statesarea[nss];
     636                 :      413934 :                 d->outsarea = sml->outsarea;
     637                 :      413934 :                 d->incarea = sml->incarea;
     638                 :      413934 :                 d->ismalloced = ismalloced;
     639                 :      413934 :                 d->arraysmalloced = false;   /* not separately allocated, anyway */
     640                 :      413934 :         }
     641                 :             :         else
     642                 :             :         {
     643                 :      765669 :                 d = (struct dfa *) MALLOC(sizeof(struct dfa));
     644         [ +  - ]:      765669 :                 if (d == NULL)
     645                 :             :                 {
     646         [ #  # ]:           0 :                         ERR(REG_ESPACE);
     647                 :           0 :                         return NULL;
     648                 :             :                 }
     649                 :      765669 :                 d->ssets = (struct sset *) MALLOC(nss * sizeof(struct sset));
     650                 :      765669 :                 d->statesarea = (unsigned *) MALLOC((nss + WORK) * wordsper *
     651                 :             :                                                                                         sizeof(unsigned));
     652                 :      765669 :                 d->work = &d->statesarea[nss * wordsper];
     653                 :      765669 :                 d->outsarea = (struct sset **) MALLOC(nss * cnfa->ncolors *
     654                 :             :                                                                                           sizeof(struct sset *));
     655                 :      765669 :                 d->incarea = (struct arcp *) MALLOC(nss * cnfa->ncolors *
     656                 :             :                                                                                         sizeof(struct arcp));
     657                 :      765669 :                 d->ismalloced = true;
     658                 :      765669 :                 d->arraysmalloced = true;
     659                 :             :                 /* now freedfa() will behave sanely */
     660   [ +  -  +  - ]:      765669 :                 if (d->ssets == NULL || d->statesarea == NULL ||
     661   [ +  -  -  + ]:      765669 :                         d->outsarea == NULL || d->incarea == NULL)
     662                 :             :                 {
     663                 :           0 :                         freedfa(d);
     664         [ #  # ]:           0 :                         ERR(REG_ESPACE);
     665                 :           0 :                         return NULL;
     666                 :             :                 }
     667                 :             :         }
     668                 :             : 
     669         [ -  + ]:     1179603 :         d->nssets = (v->eflags & REG_SMALL) ? 7 : nss;
     670                 :     1179603 :         d->nssused = 0;
     671                 :     1179603 :         d->nstates = cnfa->nstates;
     672                 :     1179603 :         d->ncolors = cnfa->ncolors;
     673                 :     1179603 :         d->wordsper = wordsper;
     674                 :     1179603 :         d->cnfa = cnfa;
     675                 :     1179603 :         d->cm = cm;
     676                 :     1179603 :         d->lastpost = NULL;
     677                 :     1179603 :         d->lastnopr = NULL;
     678                 :     1179603 :         d->search = d->ssets;
     679                 :     1179603 :         d->backno = -1;                              /* may be set by caller */
     680                 :     1179603 :         d->backmin = d->backmax = 0;
     681                 :             : 
     682                 :             :         /* initialization of sset fields is done as needed */
     683                 :             : 
     684                 :     1179603 :         return d;
     685                 :     1179603 : }
     686                 :             : 
     687                 :             : /*
     688                 :             :  * freedfa - free a DFA
     689                 :             :  */
     690                 :             : static void
     691                 :     1179603 : freedfa(struct dfa *d)
     692                 :             : {
     693         [ +  + ]:     1179603 :         if (d->arraysmalloced)
     694                 :             :         {
     695         [ -  + ]:      765669 :                 if (d->ssets != NULL)
     696                 :      765669 :                         FREE(d->ssets);
     697         [ -  + ]:      765669 :                 if (d->statesarea != NULL)
     698                 :      765669 :                         FREE(d->statesarea);
     699         [ -  + ]:      765669 :                 if (d->outsarea != NULL)
     700                 :      765669 :                         FREE(d->outsarea);
     701         [ -  + ]:      765669 :                 if (d->incarea != NULL)
     702                 :      765669 :                         FREE(d->incarea);
     703                 :      765669 :         }
     704                 :             : 
     705         [ +  + ]:     1179603 :         if (d->ismalloced)
     706                 :      769345 :                 FREE(d);
     707                 :     1179603 : }
     708                 :             : 
     709                 :             : /*
     710                 :             :  * hash - construct a hash code for a bitvector
     711                 :             :  *
     712                 :             :  * There are probably better ways, but they're more expensive.
     713                 :             :  */
     714                 :             : static unsigned
     715                 :        2858 : hash(unsigned *uv,
     716                 :             :          int n)
     717                 :             : {
     718                 :        2858 :         int                     i;
     719                 :        2858 :         unsigned        h;
     720                 :             : 
     721                 :        2858 :         h = 0;
     722         [ +  + ]:       10013 :         for (i = 0; i < n; i++)
     723                 :        7155 :                 h ^= uv[i];
     724                 :        5716 :         return h;
     725                 :        2858 : }
     726                 :             : 
     727                 :             : /*
     728                 :             :  * initialize - hand-craft a cache entry for startup, otherwise get ready
     729                 :             :  */
     730                 :             : static struct sset *
     731                 :     1179778 : initialize(struct vars *v,
     732                 :             :                    struct dfa *d,
     733                 :             :                    chr *start)
     734                 :             : {
     735                 :     1179778 :         struct sset *ss;
     736                 :     1179778 :         int                     i;
     737                 :             : 
     738                 :             :         /* is previous one still there? */
     739   [ +  +  -  + ]:     1179778 :         if (d->nssused > 0 && (d->ssets[0].flags & STARTER))
     740                 :         537 :                 ss = &d->ssets[0];
     741                 :             :         else
     742                 :             :         {                                                       /* no, must (re)build it */
     743                 :     1179241 :                 ss = getvacant(v, d, start, start);
     744         [ +  - ]:     1179241 :                 if (ss == NULL)
     745                 :           0 :                         return NULL;
     746         [ +  + ]:     2358767 :                 for (i = 0; i < d->wordsper; i++)
     747                 :     1179526 :                         ss->states[i] = 0;
     748                 :     1179241 :                 BSET(ss->states, d->cnfa->pre);
     749         [ +  + ]:     1179241 :                 ss->hash = HASH(ss->states, d->wordsper);
     750         [ +  - ]:     1179241 :                 assert(d->cnfa->pre != d->cnfa->post);
     751                 :     1179241 :                 ss->flags = STARTER | LOCKED | NOPROGRESS;
     752                 :             :                 /* lastseen dealt with below */
     753                 :             :         }
     754                 :             : 
     755         [ +  + ]:     2361286 :         for (i = 0; i < d->nssused; i++)
     756                 :     1181508 :                 d->ssets[i].lastseen = NULL;
     757                 :     1179778 :         ss->lastseen = start;                /* maybe untrue, but harmless */
     758                 :     1179778 :         d->lastpost = NULL;
     759                 :     1179778 :         d->lastnopr = NULL;
     760                 :     1179778 :         return ss;
     761                 :     1179778 : }
     762                 :             : 
     763                 :             : /*
     764                 :             :  * miss - handle a stateset cache miss
     765                 :             :  *
     766                 :             :  * css is the current stateset, co is the color of the current input character,
     767                 :             :  * cp points to the character after that (which is where we may need to test
     768                 :             :  * LACONs).  start does not affect matching behavior but is needed for pickss'
     769                 :             :  * heuristics about which stateset cache entry to replace.
     770                 :             :  *
     771                 :             :  * Ordinarily, returns the address of the next stateset (the one that is
     772                 :             :  * valid after consuming the input character).  Returns NULL if no valid
     773                 :             :  * NFA states remain, ie we have a certain match failure.
     774                 :             :  * Internal errors also return NULL, with v->err set.
     775                 :             :  */
     776                 :             : static struct sset *
     777                 :     2860817 : miss(struct vars *v,
     778                 :             :          struct dfa *d,
     779                 :             :          struct sset *css,
     780                 :             :          color co,
     781                 :             :          chr *cp,                                       /* next chr */
     782                 :             :          chr *start)                            /* where the attempt got started */
     783                 :             : {
     784                 :     2860817 :         struct cnfa *cnfa = d->cnfa;
     785                 :     2860817 :         int                     i;
     786                 :     2860817 :         unsigned        h;
     787                 :     2860817 :         struct carc *ca;
     788                 :     2860817 :         struct sset *p;
     789                 :     2860817 :         int                     ispseudocolor;
     790                 :     2860817 :         int                     ispost;
     791                 :     2860817 :         int                     noprogress;
     792                 :     2860817 :         int                     gotstate;
     793                 :     2860817 :         int                     dolacons;
     794                 :     2860817 :         int                     sawlacons;
     795                 :             : 
     796                 :             :         /* for convenience, we can be called even if it might not be a miss */
     797         [ +  + ]:     2860817 :         if (css->outs[co] != NULL)
     798                 :             :         {
     799                 :             :                 FDEBUG(("hit\n"));
     800                 :         400 :                 return css->outs[co];
     801                 :             :         }
     802                 :             :         FDEBUG(("miss\n"));
     803                 :             : 
     804                 :             :         /*
     805                 :             :          * Checking for operation cancel in the inner text search loop seems
     806                 :             :          * unduly expensive.  As a compromise, check during cache misses.
     807                 :             :          */
     808         [ +  - ]:     2860417 :         INTERRUPT(v->re);
     809                 :             : 
     810                 :             :         /*
     811                 :             :          * What set of states would we end up in after consuming the co character?
     812                 :             :          * We first consider PLAIN arcs that consume the character, and then look
     813                 :             :          * to see what LACON arcs could be traversed after consuming it.
     814                 :             :          */
     815         [ +  + ]:     5725109 :         for (i = 0; i < d->wordsper; i++)
     816                 :     2864692 :                 d->work[i] = 0;                      /* build new stateset bitmap in d->work */
     817                 :     2860417 :         ispseudocolor = d->cm->cd[co].flags & PSEUDO;
     818                 :     2860417 :         ispost = 0;
     819                 :     2860417 :         noprogress = 1;
     820                 :     2860417 :         gotstate = 0;
     821         [ +  + ]:    41627695 :         for (i = 0; i < d->nstates; i++)
     822         [ +  + ]:    42005271 :                 if (ISBSET(css->states, i))
     823         [ +  + ]:     8084432 :                         for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
     824   [ +  +  +  + ]:     5631573 :                                 if (ca->co == co ||
     825         [ +  + ]:     5499929 :                                         (ca->co == RAINBOW && !ispseudocolor))
     826                 :             :                                 {
     827                 :     2130843 :                                         BSET(d->work, ca->to);
     828                 :     2130843 :                                         gotstate = 1;
     829         [ +  + ]:     2130843 :                                         if (ca->to == cnfa->post)
     830                 :       46684 :                                                 ispost = 1;
     831         [ +  + ]:     2130843 :                                         if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
     832                 :      544477 :                                                 noprogress = 0;
     833                 :             :                                         FDEBUG(("%d -> %d\n", i, ca->to));
     834                 :     5368836 :                                 }
     835         [ +  + ]:     2860417 :         if (!gotstate)
     836                 :     1155616 :                 return NULL;                    /* character cannot reach any new state */
     837                 :     1704801 :         dolacons = (cnfa->flags & HASLACONS);
     838                 :     1704801 :         sawlacons = 0;
     839                 :             :         /* outer loop handles transitive closure of reachable-by-LACON states */
     840         [ +  + ]:     1705043 :         while (dolacons)
     841                 :             :         {
     842                 :         242 :                 dolacons = 0;
     843         [ +  + ]:        2295 :                 for (i = 0; i < d->nstates; i++)
     844         [ +  + ]:        2336 :                         if (ISBSET(d->work, i))
     845         [ +  + ]:         653 :                                 for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
     846                 :             :                                 {
     847         [ +  + ]:         370 :                                         if (ca->co < cnfa->ncolors)
     848                 :         332 :                                                 continue;       /* not a LACON arc */
     849         [ +  + ]:          38 :                                         if (ISBSET(d->work, ca->to))
     850                 :          12 :                                                 continue;       /* arc would be a no-op anyway */
     851                 :          26 :                                         sawlacons = 1;  /* this LACON affects our result */
     852         [ +  + ]:          26 :                                         if (!lacon(v, cnfa, cp, ca->co))
     853                 :             :                                         {
     854         [ -  + ]:          16 :                                                 if (ISERR())
     855                 :           0 :                                                         return NULL;
     856                 :          16 :                                                 continue;       /* LACON arc cannot be traversed */
     857                 :             :                                         }
     858         [ -  + ]:          10 :                                         if (ISERR())
     859                 :           0 :                                                 return NULL;
     860                 :          10 :                                         BSET(d->work, ca->to);
     861                 :          10 :                                         dolacons = 1;
     862         [ +  - ]:          10 :                                         if (ca->to == cnfa->post)
     863                 :           0 :                                                 ispost = 1;
     864         [ -  + ]:          10 :                                         if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
     865                 :          10 :                                                 noprogress = 0;
     866                 :             :                                         FDEBUG(("%d :> %d\n", i, ca->to));
     867                 :         293 :                                 }
     868                 :             :         }
     869         [ +  + ]:     1704801 :         h = HASH(d->work, d->wordsper);
     870                 :             : 
     871                 :             :         /* Is this stateset already in the cache? */
     872         [ +  + ]:     4650202 :         for (p = d->ssets, i = d->nssused; i > 0; p++, i--)
     873   [ +  +  +  +  :     3124173 :                 if (HIT(h, d->work, p, d->wordsper))
                   +  + ]
     874                 :             :                 {
     875                 :             :                         FDEBUG(("cached c%d\n", (int) (p - d->ssets)));
     876                 :      178772 :                         break;                          /* NOTE BREAK OUT */
     877                 :             :                 }
     878         [ +  + ]:     1704801 :         if (i == 0)
     879                 :             :         {                                                       /* nope, need a new cache entry */
     880                 :     1526029 :                 p = getvacant(v, d, cp, start);
     881         [ +  - ]:     1526029 :                 if (p == NULL)
     882                 :           0 :                         return NULL;
     883         [ +  - ]:     1526029 :                 assert(p != css);
     884         [ +  + ]:     3055045 :                 for (i = 0; i < d->wordsper; i++)
     885                 :     1529016 :                         p->states[i] = d->work[i];
     886                 :     1526029 :                 p->hash = h;
     887                 :     1526029 :                 p->flags = (ispost) ? POSTSTATE : 0;
     888         [ +  + ]:     1526029 :                 if (noprogress)
     889                 :     1179948 :                         p->flags |= NOPROGRESS;
     890                 :             :                 /* lastseen to be dealt with by caller */
     891                 :     1526029 :         }
     892                 :             : 
     893                 :             :         /*
     894                 :             :          * Link new stateset to old, unless a LACON affected the result, in which
     895                 :             :          * case we don't create the link.  That forces future transitions across
     896                 :             :          * this same arc (same prior stateset and character color) to come through
     897                 :             :          * miss() again, so that we can recheck the LACON(s), which might or might
     898                 :             :          * not pass since context will be different.
     899                 :             :          */
     900         [ +  + ]:     1704801 :         if (!sawlacons)
     901                 :             :         {
     902                 :             :                 FDEBUG(("c%d[%d]->c%d\n",
     903                 :             :                                 (int) (css - d->ssets), co, (int) (p - d->ssets)));
     904                 :     1704782 :                 css->outs[co] = p;
     905                 :     1704782 :                 css->inchain[co] = p->ins;
     906                 :     1704782 :                 p->ins.ss = css;
     907                 :     1704782 :                 p->ins.co = co;
     908                 :     1704782 :         }
     909                 :     1704801 :         return p;
     910                 :     2860817 : }
     911                 :             : 
     912                 :             : /*
     913                 :             :  * lacon - lookaround-constraint checker for miss()
     914                 :             :  */
     915                 :             : static int                                              /* predicate:  constraint satisfied? */
     916                 :          26 : lacon(struct vars *v,
     917                 :             :           struct cnfa *pcnfa,           /* parent cnfa */
     918                 :             :           chr *cp,
     919                 :             :           color co)                                     /* "color" of the lookaround constraint */
     920                 :             : {
     921                 :          26 :         int                     n;
     922                 :          26 :         struct subre *sub;
     923                 :          26 :         struct dfa *d;
     924                 :          26 :         chr                *end;
     925                 :          26 :         int                     satisfied;
     926                 :             : 
     927                 :             :         /* Since this is recursive, it could be driven to stack overflow */
     928         [ -  + ]:          26 :         if (STACK_TOO_DEEP(v->re))
     929                 :             :         {
     930         [ #  # ]:           0 :                 ERR(REG_ETOOBIG);
     931                 :           0 :                 return 0;
     932                 :             :         }
     933                 :             : 
     934                 :          26 :         n = co - pcnfa->ncolors;
     935         [ +  - ]:          26 :         assert(n > 0 && n < v->g->nlacons && v->g->lacons != NULL);
     936                 :             :         FDEBUG(("=== testing lacon %d\n", n));
     937                 :          26 :         sub = &v->g->lacons[n];
     938                 :          26 :         d = getladfa(v, n);
     939         [ +  - ]:          26 :         if (d == NULL)
     940                 :           0 :                 return 0;
     941         [ +  + ]:          26 :         if (LATYPE_IS_AHEAD(sub->latype))
     942                 :             :         {
     943                 :             :                 /* used to use longest() here, but shortest() could be much cheaper */
     944                 :          12 :                 end = shortest(v, d, cp, cp, v->stop,
     945                 :             :                                            (chr **) NULL, (int *) NULL);
     946         [ -  + ]:          12 :                 satisfied = LATYPE_IS_POS(sub->latype) ? (end != NULL) : (end == NULL);
     947                 :          12 :         }
     948                 :             :         else
     949                 :             :         {
     950                 :             :                 /*
     951                 :             :                  * To avoid doing O(N^2) work when repeatedly testing a lookbehind
     952                 :             :                  * constraint in an N-character string, we use matchuntil() which can
     953                 :             :                  * cache the DFA state across calls.  We only need to restart if the
     954                 :             :                  * probe point decreases, which is not common.  The NFA we're using is
     955                 :             :                  * a search NFA, so it doesn't mind scanning over stuff before the
     956                 :             :                  * nominal match.
     957                 :             :                  */
     958                 :          14 :                 satisfied = matchuntil(v, d, cp, &v->lblastcss[n], &v->lblastcp[n]);
     959         [ -  + ]:          14 :                 if (!LATYPE_IS_POS(sub->latype))
     960                 :           0 :                         satisfied = !satisfied;
     961                 :             :         }
     962                 :             :         FDEBUG(("=== lacon %d satisfied %d\n", n, satisfied));
     963                 :          26 :         return satisfied;
     964                 :          26 : }
     965                 :             : 
     966                 :             : /*
     967                 :             :  * getvacant - get a vacant state set
     968                 :             :  *
     969                 :             :  * This routine clears out the inarcs and outarcs, but does not otherwise
     970                 :             :  * clear the innards of the state set -- that's up to the caller.
     971                 :             :  */
     972                 :             : static struct sset *
     973                 :     2705270 : getvacant(struct vars *v,
     974                 :             :                   struct dfa *d,
     975                 :             :                   chr *cp,
     976                 :             :                   chr *start)
     977                 :             : {
     978                 :     2705270 :         int                     i;
     979                 :     2705270 :         struct sset *ss;
     980                 :     2705270 :         struct sset *p;
     981                 :     2705270 :         struct arcp ap;
     982                 :     2705270 :         color           co;
     983                 :             : 
     984                 :     2705270 :         ss = pickss(v, d, cp, start);
     985         [ +  - ]:     2705270 :         if (ss == NULL)
     986                 :           0 :                 return NULL;
     987         [ +  - ]:     2705270 :         assert(!(ss->flags & LOCKED));
     988                 :             : 
     989                 :             :         /* clear out its inarcs, including self-referential ones */
     990                 :     2705270 :         ap = ss->ins;
     991         [ -  + ]:     2705270 :         while ((p = ap.ss) != NULL)
     992                 :             :         {
     993                 :           0 :                 co = ap.co;
     994                 :             :                 FDEBUG(("zapping c%d's %ld outarc\n", (int) (p - d->ssets), (long) co));
     995                 :           0 :                 p->outs[co] = NULL;
     996                 :           0 :                 ap = p->inchain[co];
     997                 :           0 :                 p->inchain[co].ss = NULL;    /* paranoia */
     998                 :             :         }
     999                 :     2705270 :         ss->ins.ss = NULL;
    1000                 :             : 
    1001                 :             :         /* take it off the inarc chains of the ssets reached by its outarcs */
    1002         [ +  + ]:    37234973 :         for (i = 0; i < d->ncolors; i++)
    1003                 :             :         {
    1004                 :    34529703 :                 p = ss->outs[i];
    1005         [ +  - ]:    34529703 :                 assert(p != ss);                /* not self-referential */
    1006         [ -  + ]:    34529703 :                 if (p == NULL)
    1007                 :    34529703 :                         continue;                       /* NOTE CONTINUE */
    1008                 :             :                 FDEBUG(("del outarc %d from c%d's in chn\n", i, (int) (p - d->ssets)));
    1009   [ #  #  #  # ]:           0 :                 if (p->ins.ss == ss && p->ins.co == i)
    1010                 :           0 :                         p->ins = ss->inchain[i];
    1011                 :             :                 else
    1012                 :             :                 {
    1013                 :           0 :                         struct arcp lastap = {NULL, 0};
    1014                 :             : 
    1015         [ #  # ]:           0 :                         assert(p->ins.ss != NULL);
    1016   [ #  #  #  # ]:           0 :                         for (ap = p->ins; ap.ss != NULL &&
    1017         [ #  # ]:           0 :                                  !(ap.ss == ss && ap.co == i);
    1018                 :           0 :                                  ap = ap.ss->inchain[ap.co])
    1019                 :           0 :                                 lastap = ap;
    1020         [ #  # ]:           0 :                         assert(ap.ss != NULL);
    1021                 :           0 :                         lastap.ss->inchain[lastap.co] = ss->inchain[i];
    1022                 :           0 :                 }
    1023                 :           0 :                 ss->outs[i] = NULL;
    1024                 :           0 :                 ss->inchain[i].ss = NULL;
    1025                 :           0 :         }
    1026                 :             : 
    1027                 :             :         /* if ss was a success state, may need to remember location */
    1028   [ -  +  #  #  :     2705270 :         if ((ss->flags & POSTSTATE) && ss->lastseen != d->lastpost &&
                   #  # ]
    1029         [ #  # ]:           0 :                 (d->lastpost == NULL || d->lastpost < ss->lastseen))
    1030                 :           0 :                 d->lastpost = ss->lastseen;
    1031                 :             : 
    1032                 :             :         /* likewise for a no-progress state */
    1033   [ -  +  #  #  :     2705270 :         if ((ss->flags & NOPROGRESS) && ss->lastseen != d->lastnopr &&
                   #  # ]
    1034         [ #  # ]:           0 :                 (d->lastnopr == NULL || d->lastnopr < ss->lastseen))
    1035                 :           0 :                 d->lastnopr = ss->lastseen;
    1036                 :             : 
    1037                 :     2705270 :         return ss;
    1038                 :     2705270 : }
    1039                 :             : 
    1040                 :             : /*
    1041                 :             :  * pickss - pick the next stateset to be used
    1042                 :             :  */
    1043                 :             : static struct sset *
    1044                 :     2705270 : pickss(struct vars *v,
    1045                 :             :            struct dfa *d,
    1046                 :             :            chr *cp,
    1047                 :             :            chr *start)
    1048                 :             : {
    1049                 :     2705270 :         int                     i;
    1050                 :     2705270 :         struct sset *ss;
    1051                 :     2705270 :         struct sset *end;
    1052                 :     2705270 :         chr                *ancient;
    1053                 :             : 
    1054                 :             :         /* shortcut for cases where cache isn't full */
    1055         [ +  - ]:     2705270 :         if (d->nssused < d->nssets)
    1056                 :             :         {
    1057                 :     2705270 :                 i = d->nssused;
    1058                 :     2705270 :                 d->nssused++;
    1059                 :     2705270 :                 ss = &d->ssets[i];
    1060                 :             :                 FDEBUG(("new c%d\n", i));
    1061                 :             :                 /* set up innards */
    1062                 :     2705270 :                 ss->states = &d->statesarea[i * d->wordsper];
    1063                 :     2705270 :                 ss->flags = 0;
    1064                 :     2705270 :                 ss->ins.ss = NULL;
    1065                 :     2705270 :                 ss->ins.co = WHITE;          /* give it some value */
    1066                 :     2705270 :                 ss->outs = &d->outsarea[i * d->ncolors];
    1067                 :     2705270 :                 ss->inchain = &d->incarea[i * d->ncolors];
    1068         [ +  + ]:    37234973 :                 for (i = 0; i < d->ncolors; i++)
    1069                 :             :                 {
    1070                 :    34529703 :                         ss->outs[i] = NULL;
    1071                 :    34529703 :                         ss->inchain[i].ss = NULL;
    1072                 :    34529703 :                 }
    1073                 :     2705270 :                 return ss;
    1074                 :             :         }
    1075                 :             : 
    1076                 :             :         /* look for oldest, or old enough anyway */
    1077         [ #  # ]:           0 :         if (cp - start > d->nssets * 2 / 3) /* oldest 33% are expendable */
    1078                 :           0 :                 ancient = cp - d->nssets * 2 / 3;
    1079                 :             :         else
    1080                 :           0 :                 ancient = start;
    1081         [ #  # ]:           0 :         for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++)
    1082   [ #  #  #  # ]:           0 :                 if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
    1083                 :           0 :                         !(ss->flags & LOCKED))
    1084                 :             :                 {
    1085                 :           0 :                         d->search = ss + 1;
    1086                 :             :                         FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
    1087                 :           0 :                         return ss;
    1088                 :             :                 }
    1089         [ #  # ]:           0 :         for (ss = d->ssets, end = d->search; ss < end; ss++)
    1090   [ #  #  #  # ]:           0 :                 if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
    1091                 :           0 :                         !(ss->flags & LOCKED))
    1092                 :             :                 {
    1093                 :           0 :                         d->search = ss + 1;
    1094                 :             :                         FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
    1095                 :           0 :                         return ss;
    1096                 :             :                 }
    1097                 :             : 
    1098                 :             :         /* nobody's old enough?!? -- something's really wrong */
    1099                 :             :         FDEBUG(("cannot find victim to replace!\n"));
    1100         [ #  # ]:           0 :         ERR(REG_ASSERT);
    1101                 :           0 :         return NULL;
    1102                 :     2705270 : }
        

Generated by: LCOV version 2.3.2-1