PostgreSQL Source Code  git master
regc_nfa.c File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NISERR()   VISERR(nfa->v)
 
#define NERR(e)   VERR(nfa->v, (e))
 
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)   ((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32))
 
#define CA(ct, at)   (((ct)<<CHAR_BIT) | (at))
 

Functions

static struct nfanewnfa (struct vars *v, struct colormap *cm, struct nfa *parent)
 
static void freenfa (struct nfa *nfa)
 
static struct statenewstate (struct nfa *nfa)
 
static struct statenewfstate (struct nfa *nfa, int flag)
 
static void dropstate (struct nfa *nfa, struct state *s)
 
static void freestate (struct nfa *nfa, struct state *s)
 
static void newarc (struct nfa *nfa, int t, color co, struct state *from, struct state *to)
 
static void createarc (struct nfa *nfa, int t, color co, struct state *from, struct state *to)
 
static struct arcallocarc (struct nfa *nfa)
 
static void freearc (struct nfa *nfa, struct arc *victim)
 
static void changearcsource (struct arc *a, struct state *newfrom)
 
static void changearctarget (struct arc *a, struct state *newto)
 
static int hasnonemptyout (struct state *s)
 
static struct arcfindarc (struct state *s, int type, color co)
 
static void cparc (struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
 
static void sortins (struct nfa *nfa, struct state *s)
 
static int sortins_cmp (const void *a, const void *b)
 
static void sortouts (struct nfa *nfa, struct state *s)
 
static int sortouts_cmp (const void *a, const void *b)
 
static void moveins (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void copyins (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void mergeins (struct nfa *nfa, struct state *s, struct arc **arcarray, int arccount)
 
static void moveouts (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void copyouts (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void cloneouts (struct nfa *nfa, struct state *old, struct state *from, struct state *to, int type)
 
static void delsub (struct nfa *nfa, struct state *lp, struct state *rp)
 
static void deltraverse (struct nfa *nfa, struct state *leftend, struct state *s)
 
static void dupnfa (struct nfa *nfa, struct state *start, struct state *stop, struct state *from, struct state *to)
 
static void duptraverse (struct nfa *nfa, struct state *s, struct state *stmp)
 
static void removeconstraints (struct nfa *nfa, struct state *start, struct state *stop)
 
static void removetraverse (struct nfa *nfa, struct state *s)
 
static void cleartraverse (struct nfa *nfa, struct state *s)
 
static struct statesingle_color_transition (struct state *s1, struct state *s2)
 
static void specialcolors (struct nfa *nfa)
 
static long optimize (struct nfa *nfa, FILE *f)
 
static void pullback (struct nfa *nfa, FILE *f)
 
static int pull (struct nfa *nfa, struct arc *con, struct state **intermediates)
 
static void pushfwd (struct nfa *nfa, FILE *f)
 
static int push (struct nfa *nfa, struct arc *con, struct state **intermediates)
 
static int combine (struct nfa *nfa, struct arc *con, struct arc *a)
 
static void fixempties (struct nfa *nfa, FILE *f)
 
static struct stateemptyreachable (struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
 
static int isconstraintarc (struct arc *a)
 
static int hasconstraintout (struct state *s)
 
static void fixconstraintloops (struct nfa *nfa, FILE *f)
 
static int findconstraintloop (struct nfa *nfa, struct state *s)
 
static void breakconstraintloop (struct nfa *nfa, struct state *sinitial)
 
static void clonesuccessorstates (struct nfa *nfa, struct state *ssource, struct state *sclone, struct state *spredecessor, struct arc *refarc, char *curdonemap, char *outerdonemap, int nstates)
 
static void cleanup (struct nfa *nfa)
 
static void markreachable (struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
 
static void markcanreach (struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
 
static long analyze (struct nfa *nfa)
 
static void checkmatchall (struct nfa *nfa)
 
static bool checkmatchall_recurse (struct nfa *nfa, struct state *s, bool **haspaths)
 
static bool check_out_colors_match (struct state *s, color co1, color co2)
 
static bool check_in_colors_match (struct state *s, color co1, color co2)
 
static void compact (struct nfa *nfa, struct cnfa *cnfa)
 
static void carcsort (struct carc *first, size_t n)
 
static int carc_cmp (const void *a, const void *b)
 
static void freecnfa (struct cnfa *cnfa)
 
static void dumpnfa (struct nfa *nfa, FILE *f)
 

Macro Definition Documentation

◆ BULK_ARC_OP_USE_SORT

#define BULK_ARC_OP_USE_SORT (   nsrcarcs,
  ndestarcs 
)    ((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32))

Definition at line 766 of file regc_nfa.c.

Referenced by copyins(), copyouts(), moveins(), and moveouts().

◆ CA

#define CA (   ct,
  at 
)    (((ct)<<CHAR_BIT) | (at))

Referenced by combine().

◆ NERR

◆ NISERR

Function Documentation

◆ allocarc()

static struct arc* allocarc ( struct nfa nfa)
static

Definition at line 376 of file regc_nfa.c.

References arcbatch::a, ARCBATCHSIZE, FIRSTABSIZE, nfa::freearcs, nfa::lastab, nfa::lastabused, MALLOC, MAXABSIZE, arcbatch::narcs, NERR, arcbatch::next, REG_ESPACE, REG_ETOOBIG, REG_MAX_COMPILE_SPACE, vars::spaceused, and nfa::v.

Referenced by createarc().

377 {
378  struct arc *a;
379 
380  /* first, recycle anything that's on the freelist */
381  if (nfa->freearcs != NULL)
382  {
383  a = nfa->freearcs;
384  nfa->freearcs = a->freechain;
385  }
386  /* otherwise, is there anything left in the last arcbatch? */
387  else if (nfa->lastab != NULL && nfa->lastabused < nfa->lastab->narcs)
388  {
389  a = &nfa->lastab->a[nfa->lastabused++];
390  }
391  /* otherwise, need to allocate a new arcbatch */
392  else
393  {
394  struct arcbatch *newAb;
395  size_t narcs;
396 
397  if (nfa->v->spaceused >= REG_MAX_COMPILE_SPACE)
398  {
399  NERR(REG_ETOOBIG);
400  return NULL;
401  }
402  narcs = (nfa->lastab != NULL) ? nfa->lastab->narcs * 2 : FIRSTABSIZE;
403  if (narcs > MAXABSIZE)
404  narcs = MAXABSIZE;
405  newAb = (struct arcbatch *) MALLOC(ARCBATCHSIZE(narcs));
406  if (newAb == NULL)
407  {
408  NERR(REG_ESPACE);
409  return NULL;
410  }
411  nfa->v->spaceused += ARCBATCHSIZE(narcs);
412  newAb->narcs = narcs;
413  newAb->next = nfa->lastab;
414  nfa->lastab = newAb;
415  nfa->lastabused = 1;
416  a = &newAb->a[0];
417  }
418 
419  return a;
420 }
size_t lastabused
Definition: regguts.h:357
#define NERR(e)
Definition: regc_nfa.c:40
#define REG_ESPACE
Definition: regex.h:151
Definition: regguts.h:290
#define ARCBATCHSIZE(n)
Definition: regguts.h:312
size_t narcs
Definition: regguts.h:309
#define MALLOC(n)
Definition: regcustom.h:60
#define REG_MAX_COMPILE_SPACE
Definition: regguts.h:444
struct vars * v
Definition: regguts.h:364
struct arcbatch * next
Definition: regguts.h:308
struct arcbatch * lastab
Definition: regguts.h:355
#define FIRSTABSIZE
Definition: regguts.h:314
struct arc a[FLEXIBLE_ARRAY_MEMBER]
Definition: regguts.h:310
#define REG_ETOOBIG
Definition: regex.h:157
struct arc * freearcs
Definition: regguts.h:353
#define MAXABSIZE
Definition: regguts.h:315
size_t spaceused
Definition: regcomp.c:265

◆ analyze()

static long analyze ( struct nfa nfa)
static

Definition at line 3044 of file regc_nfa.c.

References checkmatchall(), NISERR, arc::outchain, state::outs, nfa::post, nfa::pre, REG_UEMPTYMATCH, REG_UIMPOSSIBLE, and arc::to.

Referenced by ExecVacuum(), GetCommandLogLevel(), and optimize().

3045 {
3046  struct arc *a;
3047  struct arc *aa;
3048 
3049  if (NISERR())
3050  return 0;
3051 
3052  /* Detect whether NFA can't match anything */
3053  if (nfa->pre->outs == NULL)
3054  return REG_UIMPOSSIBLE;
3055 
3056  /* Detect whether NFA matches all strings (possibly with length bounds) */
3057  checkmatchall(nfa);
3058 
3059  /* Detect whether NFA can possibly match a zero-length string */
3060  for (a = nfa->pre->outs; a != NULL; a = a->outchain)
3061  for (aa = a->to->outs; aa != NULL; aa = aa->outchain)
3062  if (aa->to == nfa->post)
3063  return REG_UEMPTYMATCH;
3064  return 0;
3065 }
#define REG_UIMPOSSIBLE
Definition: regex.h:74
#define NISERR()
Definition: regc_nfa.c:39
Definition: regguts.h:290
struct state * post
Definition: regguts.h:348
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
struct state * to
Definition: regguts.h:295
#define REG_UEMPTYMATCH
Definition: regex.h:73
struct state * pre
Definition: regguts.h:345
static void checkmatchall(struct nfa *nfa)
Definition: regc_nfa.c:3090

◆ breakconstraintloop()

static void breakconstraintloop ( struct nfa nfa,
struct state sinitial 
)
static

Definition at line 2579 of file regc_nfa.c.

References assert, clonesuccessorstates(), cparc(), freearc(), freestate(), arc::from, isconstraintarc(), newstate(), state::next, NISERR, state::nouts, nfa::nstates, arc::outchain, state::outs, nfa::states, state::tmp, and arc::to.

Referenced by findconstraintloop().

2580 {
2581  struct state *s;
2582  struct state *shead;
2583  struct state *stail;
2584  struct state *sclone;
2585  struct state *nexts;
2586  struct arc *refarc;
2587  struct arc *a;
2588  struct arc *nexta;
2589 
2590  /*
2591  * Start by identifying which loop step we want to break at.
2592  * Preferentially this is one with only one constraint arc. (XXX are
2593  * there any other secondary heuristics we want to use here?) Set refarc
2594  * to point to the selected lone constraint arc, if there is one.
2595  */
2596  refarc = NULL;
2597  s = sinitial;
2598  do
2599  {
2600  nexts = s->tmp;
2601  assert(nexts != s); /* should not see any one-element loops */
2602  if (refarc == NULL)
2603  {
2604  int narcs = 0;
2605 
2606  for (a = s->outs; a != NULL; a = a->outchain)
2607  {
2608  if (a->to == nexts && isconstraintarc(a))
2609  {
2610  refarc = a;
2611  narcs++;
2612  }
2613  }
2614  assert(narcs > 0);
2615  if (narcs > 1)
2616  refarc = NULL; /* multiple constraint arcs here, no good */
2617  }
2618  s = nexts;
2619  } while (s != sinitial);
2620 
2621  if (refarc)
2622  {
2623  /* break at the refarc */
2624  shead = refarc->from;
2625  stail = refarc->to;
2626  assert(stail == shead->tmp);
2627  }
2628  else
2629  {
2630  /* for lack of a better idea, break after sinitial */
2631  shead = sinitial;
2632  stail = sinitial->tmp;
2633  }
2634 
2635  /*
2636  * Reset the tmp fields so that we can use them for local storage in
2637  * clonesuccessorstates. (findconstraintloop won't mind, since it's just
2638  * going to abandon its search anyway.)
2639  */
2640  for (s = nfa->states; s != NULL; s = s->next)
2641  s->tmp = NULL;
2642 
2643  /*
2644  * Recursively build clone state(s) as needed.
2645  */
2646  sclone = newstate(nfa);
2647  if (sclone == NULL)
2648  {
2649  assert(NISERR());
2650  return;
2651  }
2652 
2653  clonesuccessorstates(nfa, stail, sclone, shead, refarc,
2654  NULL, NULL, nfa->nstates);
2655 
2656  if (NISERR())
2657  return;
2658 
2659  /*
2660  * It's possible that sclone has no outarcs at all, in which case it's
2661  * useless. (We don't try extremely hard to get rid of useless states
2662  * here, but this is an easy and fairly common case.)
2663  */
2664  if (sclone->nouts == 0)
2665  {
2666  freestate(nfa, sclone);
2667  sclone = NULL;
2668  }
2669 
2670  /*
2671  * Move shead's constraint-loop arcs to point to sclone, or just drop them
2672  * if we discovered we don't need sclone.
2673  */
2674  for (a = shead->outs; a != NULL; a = nexta)
2675  {
2676  nexta = a->outchain;
2677  if (a->to == stail && isconstraintarc(a))
2678  {
2679  if (sclone)
2680  cparc(nfa, a, shead, sclone);
2681  freearc(nfa, a);
2682  if (NISERR())
2683  break;
2684  }
2685  }
2686 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
struct arc * outchain
Definition: regguts.h:296
static int isconstraintarc(struct arc *a)
Definition: regc_nfa.c:2352
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
int nstates
Definition: regguts.h:349
struct arc * outs
Definition: regguts.h:325
static void clonesuccessorstates(struct nfa *nfa, struct state *ssource, struct state *sclone, struct state *spredecessor, struct arc *refarc, char *curdonemap, char *outerdonemap, int nstates)
Definition: regc_nfa.c:2725
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
Definition: regguts.h:317
int nouts
Definition: regguts.h:323
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
static void freestate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:246

◆ carc_cmp()

static int carc_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 3609 of file regc_nfa.c.

References carc::co, and carc::to.

Referenced by carcsort().

3610 {
3611  const struct carc *aa = (const struct carc *) a;
3612  const struct carc *bb = (const struct carc *) b;
3613 
3614  if (aa->co < bb->co)
3615  return -1;
3616  if (aa->co > bb->co)
3617  return +1;
3618  if (aa->to < bb->to)
3619  return -1;
3620  if (aa->to > bb->to)
3621  return +1;
3622  /* This is unreached, since there should be no duplicate arcs now: */
3623  return 0;
3624 }
int to
Definition: regguts.h:398
color co
Definition: regguts.h:397
Definition: regguts.h:395

◆ carcsort()

static void carcsort ( struct carc first,
size_t  n 
)
static

Definition at line 3602 of file regc_nfa.c.

References carc_cmp(), and qsort.

Referenced by compact().

3603 {
3604  if (n > 1)
3605  qsort(first, n, sizeof(struct carc), carc_cmp);
3606 }
static int carc_cmp(const void *a, const void *b)
Definition: regc_nfa.c:3609
#define qsort(a, b, c, d)
Definition: port.h:504
Definition: regguts.h:395

◆ changearcsource()

static void changearcsource ( struct arc a,
struct state newfrom 
)
static

Definition at line 497 of file regc_nfa.c.

References assert, arc::from, state::nouts, arc::outchain, arc::outchainRev, and state::outs.

Referenced by moveouts().

498 {
499  struct state *oldfrom = a->from;
500  struct arc *predecessor;
501 
502  assert(oldfrom != newfrom);
503 
504  /* take it off old source's out-chain */
505  assert(oldfrom != NULL);
506  predecessor = a->outchainRev;
507  if (predecessor == NULL)
508  {
509  assert(oldfrom->outs == a);
510  oldfrom->outs = a->outchain;
511  }
512  else
513  {
514  assert(predecessor->outchain == a);
515  predecessor->outchain = a->outchain;
516  }
517  if (a->outchain != NULL)
518  {
519  assert(a->outchain->outchainRev == a);
520  a->outchain->outchainRev = predecessor;
521  }
522  oldfrom->nouts--;
523 
524  a->from = newfrom;
525 
526  /* prepend it to new source's out-chain */
527  a->outchain = newfrom->outs;
528  a->outchainRev = NULL;
529  if (newfrom->outs)
530  newfrom->outs->outchainRev = a;
531  newfrom->outs = a;
532  newfrom->nouts++;
533 }
struct state * from
Definition: regguts.h:294
Definition: regguts.h:290
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
Definition: regguts.h:317
int nouts
Definition: regguts.h:323
struct arc * outchainRev
Definition: regguts.h:297

◆ changearctarget()

static void changearctarget ( struct arc a,
struct state newto 
)
static

Definition at line 541 of file regc_nfa.c.

References assert, arc::inchain, arc::inchainRev, state::ins, state::nins, and arc::to.

Referenced by moveins().

542 {
543  struct state *oldto = a->to;
544  struct arc *predecessor;
545 
546  assert(oldto != newto);
547 
548  /* take it off old target's in-chain */
549  assert(oldto != NULL);
550  predecessor = a->inchainRev;
551  if (predecessor == NULL)
552  {
553  assert(oldto->ins == a);
554  oldto->ins = a->inchain;
555  }
556  else
557  {
558  assert(predecessor->inchain == a);
559  predecessor->inchain = a->inchain;
560  }
561  if (a->inchain != NULL)
562  {
563  assert(a->inchain->inchainRev == a);
564  a->inchain->inchainRev = predecessor;
565  }
566  oldto->nins--;
567 
568  a->to = newto;
569 
570  /* prepend it to new target's in-chain */
571  a->inchain = newto->ins;
572  a->inchainRev = NULL;
573  if (newto->ins)
574  newto->ins->inchainRev = a;
575  newto->ins = a;
576  newto->nins++;
577 }
Definition: regguts.h:290
int nins
Definition: regguts.h:322
struct arc * inchainRev
Definition: regguts.h:300
#define assert(TEST)
Definition: imath.c:73
struct state * to
Definition: regguts.h:295
Definition: regguts.h:317
struct arc * inchain
Definition: regguts.h:299
struct arc * ins
Definition: regguts.h:324

◆ check_in_colors_match()

static bool check_in_colors_match ( struct state s,
color  co1,
color  co2 
)
static

Definition at line 3466 of file regc_nfa.c.

References assert, arc::co, arc::from, arc::inchain, state::ins, and state::tmp.

Referenced by checkmatchall().

3467 {
3468  bool result = true;
3469  struct arc *a;
3470 
3471  /*
3472  * Identical algorithm to check_out_colors_match, except examine the
3473  * from-states of s' inarcs.
3474  */
3475  for (a = s->ins; a != NULL; a = a->inchain)
3476  {
3477  if (a->co == co1)
3478  {
3479  assert(a->from->tmp == NULL);
3480  a->from->tmp = a->from;
3481  }
3482  }
3483  for (a = s->ins; a != NULL; a = a->inchain)
3484  {
3485  if (a->co == co2)
3486  {
3487  if (a->from->tmp != NULL)
3488  a->from->tmp = NULL;
3489  else
3490  result = false; /* unmatched co2 arc */
3491  }
3492  }
3493  for (a = s->ins; a != NULL; a = a->inchain)
3494  {
3495  if (a->co == co1)
3496  {
3497  if (a->from->tmp != NULL)
3498  {
3499  result = false; /* unmatched co1 arc */
3500  a->from->tmp = NULL;
3501  }
3502  }
3503  }
3504  return result;
3505 }
struct state * from
Definition: regguts.h:294
Definition: regguts.h:290
#define assert(TEST)
Definition: imath.c:73
struct state * tmp
Definition: regguts.h:326
color co
Definition: regguts.h:293
struct arc * inchain
Definition: regguts.h:299
struct arc * ins
Definition: regguts.h:324

◆ check_out_colors_match()

static bool check_out_colors_match ( struct state s,
color  co1,
color  co2 
)
static

Definition at line 3412 of file regc_nfa.c.

References assert, arc::co, arc::outchain, state::outs, state::tmp, and arc::to.

Referenced by checkmatchall().

3413 {
3414  bool result = true;
3415  struct arc *a;
3416 
3417  /*
3418  * To do this in linear time, we assume that the NFA contains no duplicate
3419  * arcs. Run through the out-arcs, marking states reachable by arcs of
3420  * color co1. Run through again, un-marking states reachable by arcs of
3421  * color co2; if we see a not-marked state, we know this co2 arc is
3422  * unmatched. Then run through again, checking for still-marked states,
3423  * and in any case leaving all the tmp fields reset to NULL.
3424  */
3425  for (a = s->outs; a != NULL; a = a->outchain)
3426  {
3427  if (a->co == co1)
3428  {
3429  assert(a->to->tmp == NULL);
3430  a->to->tmp = a->to;
3431  }
3432  }
3433  for (a = s->outs; a != NULL; a = a->outchain)
3434  {
3435  if (a->co == co2)
3436  {
3437  if (a->to->tmp != NULL)
3438  a->to->tmp = NULL;
3439  else
3440  result = false; /* unmatched co2 arc */
3441  }
3442  }
3443  for (a = s->outs; a != NULL; a = a->outchain)
3444  {
3445  if (a->co == co1)
3446  {
3447  if (a->to->tmp != NULL)
3448  {
3449  result = false; /* unmatched co1 arc */
3450  a->to->tmp = NULL;
3451  }
3452  }
3453  }
3454  return result;
3455 }
Definition: regguts.h:290
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
color co
Definition: regguts.h:293

◆ checkmatchall()

static void checkmatchall ( struct nfa nfa)
static

Definition at line 3090 of file regc_nfa.c.

References assert, nfa::bos, colormap::cd, check_in_colors_match(), check_out_colors_match(), checkmatchall_recurse(), nfa::cm, arc::co, DUPINF, nfa::eos, colordesc::flags, nfa::flags, FREE, i, MALLOC, MATCHALL, nfa::maxmatchall, nfa::minmatchall, state::next, state::no, nfa::nstates, arc::outchain, state::outs, PLAIN, nfa::post, nfa::pre, PSEUDO, RAINBOW, nfa::states, state::tmp, arc::to, and arc::type.

Referenced by analyze().

3091 {
3092  bool **haspaths;
3093  struct state *s;
3094  int i;
3095 
3096  /*
3097  * If there are too many states, don't bother trying to detect matchall.
3098  * This limit serves to bound the time and memory we could consume below.
3099  * Note that even if the graph is all-RAINBOW, if there are significantly
3100  * more than DUPINF states then it's likely that there are paths of length
3101  * more than DUPINF, which would force us to fail anyhow. In practice,
3102  * plausible ways of writing a matchall regex with maximum finite path
3103  * length K tend not to have very many more than K states.
3104  */
3105  if (nfa->nstates > DUPINF * 2)
3106  return;
3107 
3108  /*
3109  * First, scan all the states to verify that only RAINBOW arcs appear,
3110  * plus pseudocolor arcs adjacent to the pre and post states. This lets
3111  * us quickly eliminate most cases that aren't matchall NFAs.
3112  */
3113  for (s = nfa->states; s != NULL; s = s->next)
3114  {
3115  struct arc *a;
3116 
3117  for (a = s->outs; a != NULL; a = a->outchain)
3118  {
3119  if (a->type != PLAIN)
3120  return; /* any LACONs make it non-matchall */
3121  if (a->co != RAINBOW)
3122  {
3123  if (nfa->cm->cd[a->co].flags & PSEUDO)
3124  {
3125  /*
3126  * Pseudocolor arc: verify it's in a valid place (this
3127  * seems quite unlikely to fail, but let's be sure).
3128  */
3129  if (s == nfa->pre &&
3130  (a->co == nfa->bos[0] || a->co == nfa->bos[1]))
3131  /* okay BOS/BOL arc */ ;
3132  else if (a->to == nfa->post &&
3133  (a->co == nfa->eos[0] || a->co == nfa->eos[1]))
3134  /* okay EOS/EOL arc */ ;
3135  else
3136  return; /* unexpected pseudocolor arc */
3137  /* We'll check these arcs some more below. */
3138  }
3139  else
3140  return; /* any other color makes it non-matchall */
3141  }
3142  }
3143  /* Also, assert that the tmp fields are available for use. */
3144  assert(s->tmp == NULL);
3145  }
3146 
3147  /*
3148  * The next cheapest check we can make is to verify that the BOS/BOL
3149  * outarcs of the pre state reach the same states as its RAINBOW outarcs.
3150  * If they don't, the NFA expresses some constraints on the character
3151  * before the matched string, making it non-matchall. Likewise, the
3152  * EOS/EOL inarcs of the post state must match its RAINBOW inarcs.
3153  */
3154  if (!check_out_colors_match(nfa->pre, RAINBOW, nfa->bos[0]) ||
3155  !check_out_colors_match(nfa->pre, RAINBOW, nfa->bos[1]) ||
3156  !check_in_colors_match(nfa->post, RAINBOW, nfa->eos[0]) ||
3157  !check_in_colors_match(nfa->post, RAINBOW, nfa->eos[1]))
3158  return;
3159 
3160  /*
3161  * Initialize an array of path-length arrays, in which
3162  * checkmatchall_recurse will return per-state results. This lets us
3163  * memo-ize the recursive search and avoid exponential time consumption.
3164  */
3165  haspaths = (bool **) MALLOC(nfa->nstates * sizeof(bool *));
3166  if (haspaths == NULL)
3167  return; /* fail quietly */
3168  memset(haspaths, 0, nfa->nstates * sizeof(bool *));
3169 
3170  /*
3171  * Recursively search the graph for all-RAINBOW paths to the "post" state,
3172  * starting at the "pre" state, and computing the lengths of the paths.
3173  * (Given the preceding checks, there should be at least one such path.
3174  * However we could get back a false result anyway, in case there are
3175  * multi-state loops, paths exceeding DUPINF+1 length, or non-algorithmic
3176  * failures such as ENOMEM.)
3177  */
3178  if (checkmatchall_recurse(nfa, nfa->pre, haspaths))
3179  {
3180  /* The useful result is the path length array for the pre state */
3181  bool *haspath = haspaths[nfa->pre->no];
3182  int minmatch,
3183  maxmatch,
3184  morematch;
3185 
3186  assert(haspath != NULL);
3187 
3188  /*
3189  * haspath[] now represents the set of possible path lengths; but we
3190  * want to reduce that to a min and max value, because it doesn't seem
3191  * worth complicating regexec.c to deal with nonconsecutive possible
3192  * match lengths. Find min and max of first run of lengths, then
3193  * verify there are no nonconsecutive lengths.
3194  */
3195  for (minmatch = 0; minmatch <= DUPINF + 1; minmatch++)
3196  {
3197  if (haspath[minmatch])
3198  break;
3199  }
3200  assert(minmatch <= DUPINF + 1); /* else checkmatchall_recurse lied */
3201  for (maxmatch = minmatch; maxmatch < DUPINF + 1; maxmatch++)
3202  {
3203  if (!haspath[maxmatch + 1])
3204  break;
3205  }
3206  for (morematch = maxmatch + 1; morematch <= DUPINF + 1; morematch++)
3207  {
3208  if (haspath[morematch])
3209  {
3210  haspath = NULL; /* fail, there are nonconsecutive lengths */
3211  break;
3212  }
3213  }
3214 
3215  if (haspath != NULL)
3216  {
3217  /*
3218  * Success, so record the info. Here we have a fine point: the
3219  * path length from the pre state includes the pre-to-initial
3220  * transition, so it's one more than the actually matched string
3221  * length. (We avoided counting the final-to-post transition
3222  * within checkmatchall_recurse, but not this one.) This is why
3223  * checkmatchall_recurse allows one more level of path length than
3224  * might seem necessary. This decrement also takes care of
3225  * converting checkmatchall_recurse's definition of "infinity" as
3226  * "DUPINF+1" to our normal representation as "DUPINF".
3227  */
3228  assert(minmatch > 0); /* else pre and post states were adjacent */
3229  nfa->minmatchall = minmatch - 1;
3230  nfa->maxmatchall = maxmatch - 1;
3231  nfa->flags |= MATCHALL;
3232  }
3233  }
3234 
3235  /* Clean up */
3236  for (i = 0; i < nfa->nstates; i++)
3237  {
3238  if (haspaths[i] != NULL)
3239  FREE(haspaths[i]);
3240  }
3241  FREE(haspaths);
3242 }
int no
Definition: regguts.h:319
int type
Definition: regguts.h:292
static bool check_in_colors_match(struct state *s, color co1, color co2)
Definition: regc_nfa.c:3466
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
#define RAINBOW
Definition: regguts.h:154
color eos[2]
Definition: regguts.h:360
int flags
Definition: regguts.h:361
struct state * post
Definition: regguts.h:348
#define FREE(ptr)
Definition: cryptohash.c:37
struct colordesc * cd
Definition: regguts.h:231
#define MALLOC(n)
Definition: regcustom.h:60
int maxmatchall
Definition: regguts.h:363
struct arc * outchain
Definition: regguts.h:296
color bos[2]
Definition: regguts.h:359
#define assert(TEST)
Definition: imath.c:73
struct colormap * cm
Definition: regguts.h:358
struct state * next
Definition: regguts.h:327
int nstates
Definition: regguts.h:349
struct arc * outs
Definition: regguts.h:325
#define MATCHALL
Definition: regguts.h:407
struct state * tmp
Definition: regguts.h:326
int minmatchall
Definition: regguts.h:362
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
static bool checkmatchall_recurse(struct nfa *nfa, struct state *s, bool **haspaths)
Definition: regc_nfa.c:3270
color co
Definition: regguts.h:293
#define DUPINF
Definition: regguts.h:94
Definition: regguts.h:317
int flags
Definition: regguts.h:179
static bool check_out_colors_match(struct state *s, color co1, color co2)
Definition: regc_nfa.c:3412
#define PSEUDO
Definition: regguts.h:181
int i
struct state * pre
Definition: regguts.h:345

◆ checkmatchall_recurse()

static bool checkmatchall_recurse ( struct nfa nfa,
struct state s,
bool **  haspaths 
)
static

Definition at line 3270 of file regc_nfa.c.

References assert, CANCEL_REQUESTED, arc::co, DUPINF, i, MALLOC, NERR, state::no, nfa::nstates, arc::outchain, state::outs, nfa::post, RAINBOW, vars::re, REG_CANCEL, STACK_TOO_DEEP, state::tmp, arc::to, and nfa::v.

Referenced by checkmatchall().

3271 {
3272  bool result = false;
3273  bool foundloop = false;
3274  bool *haspath;
3275  struct arc *a;
3276 
3277  /*
3278  * Since this is recursive, it could be driven to stack overflow. But we
3279  * need not treat that as a hard failure; just deem the NFA non-matchall.
3280  */
3281  if (STACK_TOO_DEEP(nfa->v->re))
3282  return false;
3283 
3284  /* In case the search takes a long time, check for cancel */
3285  if (CANCEL_REQUESTED(nfa->v->re))
3286  {
3287  NERR(REG_CANCEL);
3288  return false;
3289  }
3290 
3291  /* Create a haspath array for this state */
3292  haspath = (bool *) MALLOC((DUPINF + 2) * sizeof(bool));
3293  if (haspath == NULL)
3294  return false; /* again, treat as non-matchall */
3295  memset(haspath, 0, (DUPINF + 2) * sizeof(bool));
3296 
3297  /* Mark this state as being visited */
3298  assert(s->tmp == NULL);
3299  s->tmp = s;
3300 
3301  for (a = s->outs; a != NULL; a = a->outchain)
3302  {
3303  if (a->co != RAINBOW)
3304  continue; /* ignore pseudocolor arcs */
3305  if (a->to == nfa->post)
3306  {
3307  /* We found an all-RAINBOW path to the post state */
3308  result = true;
3309 
3310  /*
3311  * Mark this state as being zero steps away from the string end
3312  * (the transition to the post state isn't counted).
3313  */
3314  haspath[0] = true;
3315  }
3316  else if (a->to == s)
3317  {
3318  /* We found a cycle of length 1, which we'll deal with below. */
3319  foundloop = true;
3320  }
3321  else if (a->to->tmp != NULL)
3322  {
3323  /* It's busy, so we found a cycle of length > 1, so fail. */
3324  result = false;
3325  break;
3326  }
3327  else
3328  {
3329  /* Consider paths forward through this to-state. */
3330  bool *nexthaspath;
3331  int i;
3332 
3333  /* If to-state was not already visited, recurse */
3334  if (haspaths[a->to->no] == NULL)
3335  {
3336  result = checkmatchall_recurse(nfa, a->to, haspaths);
3337  /* Fail if any recursive path fails */
3338  if (!result)
3339  break;
3340  }
3341  else
3342  {
3343  /* The previous visit must have found path(s) to the end */
3344  result = true;
3345  }
3346  assert(a->to->tmp == NULL);
3347  nexthaspath = haspaths[a->to->no];
3348  assert(nexthaspath != NULL);
3349 
3350  /*
3351  * Now, for every path of length i from a->to to the string end,
3352  * there is a path of length i + 1 from s to the string end.
3353  */
3354  if (nexthaspath[DUPINF] != nexthaspath[DUPINF + 1])
3355  {
3356  /*
3357  * a->to has a path of length exactly DUPINF, but not longer;
3358  * or it has paths of all lengths > DUPINF but not one of
3359  * exactly that length. In either case, we cannot represent
3360  * the possible path lengths from s correctly, so fail.
3361  */
3362  result = false;
3363  break;
3364  }
3365  /* Merge knowledge of these path lengths into what we have */
3366  for (i = 0; i < DUPINF; i++)
3367  haspath[i + 1] |= nexthaspath[i];
3368  /* Infinity + 1 is still infinity */
3369  haspath[DUPINF + 1] |= nexthaspath[DUPINF + 1];
3370  }
3371  }
3372 
3373  if (result && foundloop)
3374  {
3375  /*
3376  * If there is a length-1 loop at this state, then find the shortest
3377  * known path length to the end. The loop means that every larger
3378  * path length is possible, too. (It doesn't matter whether any of
3379  * the longer lengths were already known possible.)
3380  */
3381  int i;
3382 
3383  for (i = 0; i <= DUPINF; i++)
3384  {
3385  if (haspath[i])
3386  break;
3387  }
3388  for (i++; i <= DUPINF + 1; i++)
3389  haspath[i] = true;
3390  }
3391 
3392  /* Report out the completed path length map */
3393  assert(s->no < nfa->nstates);
3394  assert(haspaths[s->no] == NULL);
3395  haspaths[s->no] = haspath;
3396 
3397  /* Mark state no longer busy */
3398  s->tmp = NULL;
3399 
3400  return result;
3401 }
int no
Definition: regguts.h:319
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
#define RAINBOW
Definition: regguts.h:154
struct state * post
Definition: regguts.h:348
#define MALLOC(n)
Definition: regcustom.h:60
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
int nstates
Definition: regguts.h:349
struct arc * outs
Definition: regguts.h:325
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
static bool checkmatchall_recurse(struct nfa *nfa, struct state *s, bool **haspaths)
Definition: regc_nfa.c:3270
color co
Definition: regguts.h:293
#define DUPINF
Definition: regguts.h:94
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
int i
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520
unsigned char bool
Definition: c.h:391
#define REG_CANCEL
Definition: regex.h:159

◆ cleanup()

static void cleanup ( struct nfa nfa)
static

Definition at line 2957 of file regc_nfa.c.

References assert, cleartraverse(), dropstate(), state::flag, markcanreach(), markreachable(), state::next, state::nins, NISERR, state::no, nfa::nstates, nfa::post, nfa::pre, nfa::states, and state::tmp.

Referenced by optimize().

2958 {
2959  struct state *s;
2960  struct state *nexts;
2961  int n;
2962 
2963  if (NISERR())
2964  return;
2965 
2966  /* clear out unreachable or dead-end states */
2967  /* use pre to mark reachable, then post to mark can-reach-post */
2968  markreachable(nfa, nfa->pre, (struct state *) NULL, nfa->pre);
2969  markcanreach(nfa, nfa->post, nfa->pre, nfa->post);
2970  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2971  {
2972  nexts = s->next;
2973  if (s->tmp != nfa->post && !s->flag)
2974  dropstate(nfa, s);
2975  }
2976  assert(NISERR() || nfa->post->nins == 0 || nfa->post->tmp == nfa->post);
2977  cleartraverse(nfa, nfa->pre);
2978  assert(NISERR() || nfa->post->nins == 0 || nfa->post->tmp == NULL);
2979  /* the nins==0 (final unreachable) case will be caught later */
2980 
2981  /* renumber surviving states */
2982  n = 0;
2983  for (s = nfa->states; s != NULL; s = s->next)
2984  s->no = n++;
2985  nfa->nstates = n;
2986 }
#define NISERR()
Definition: regc_nfa.c:39
int no
Definition: regguts.h:319
static void markreachable(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
Definition: regc_nfa.c:2992
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
struct state * post
Definition: regguts.h:348
static void markcanreach(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
Definition: regc_nfa.c:3018
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
int nstates
Definition: regguts.h:349
char flag
Definition: regguts.h:321
static void cleartraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1515
struct state * tmp
Definition: regguts.h:326
Definition: regguts.h:317
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:230
struct state * pre
Definition: regguts.h:345

◆ cleartraverse()

static void cleartraverse ( struct nfa nfa,
struct state s 
)
static

Definition at line 1515 of file regc_nfa.c.

References NERR, arc::outchain, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, and nfa::v.

Referenced by cleanup(), dupnfa(), and removeconstraints().

1517 {
1518  struct arc *a;
1519 
1520  /* Since this is recursive, it could be driven to stack overflow */
1521  if (STACK_TOO_DEEP(nfa->v->re))
1522  {
1523  NERR(REG_ETOOBIG);
1524  return;
1525  }
1526 
1527  if (s->tmp == NULL)
1528  return;
1529  s->tmp = NULL;
1530 
1531  for (a = s->outs; a != NULL; a = a->outchain)
1532  cleartraverse(nfa, a->to);
1533 }
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
static void cleartraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1515
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
#define REG_ETOOBIG
Definition: regex.h:157
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ cloneouts()

static void cloneouts ( struct nfa nfa,
struct state old,
struct state from,
struct state to,
int  type 
)
static

Definition at line 1284 of file regc_nfa.c.

References AHEAD, assert, BEHIND, arc::co, newarc(), arc::outchain, state::outs, PLAIN, and arc::type.

1289 {
1290  struct arc *a;
1291 
1292  assert(old != from);
1293  assert(type == AHEAD || type == BEHIND);
1294 
1295  for (a = old->outs; a != NULL; a = a->outchain)
1296  {
1297  assert(a->type == PLAIN);
1298  newarc(nfa, type, a->co, from, to);
1299  }
1300 }
int type
Definition: regguts.h:292
Definition: regguts.h:290
#define BEHIND
Definition: regcomp.c:299
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
#define AHEAD
Definition: regcomp.c:298

◆ clonesuccessorstates()

static void clonesuccessorstates ( struct nfa nfa,
struct state ssource,
struct state sclone,
struct state spredecessor,
struct arc refarc,
char *  curdonemap,
char *  outerdonemap,
int  nstates 
)
static

Definition at line 2725 of file regc_nfa.c.

References a2, assert, arc::co, cparc(), dropstate(), FREE, arc::from, hasconstraintout(), state::ins, isconstraintarc(), MALLOC, NERR, newstate(), state::nins, NISERR, state::no, arc::outchain, state::outs, vars::re, REG_ESPACE, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, arc::type, and nfa::v.

Referenced by breakconstraintloop().

2733 {
2734  char *donemap;
2735  struct arc *a;
2736 
2737  /* Since this is recursive, it could be driven to stack overflow */
2738  if (STACK_TOO_DEEP(nfa->v->re))
2739  {
2740  NERR(REG_ETOOBIG);
2741  return;
2742  }
2743 
2744  /* If this state hasn't already got a donemap, create one */
2745  donemap = curdonemap;
2746  if (donemap == NULL)
2747  {
2748  donemap = (char *) MALLOC(nstates * sizeof(char));
2749  if (donemap == NULL)
2750  {
2751  NERR(REG_ESPACE);
2752  return;
2753  }
2754 
2755  if (outerdonemap != NULL)
2756  {
2757  /*
2758  * Not at outermost recursion level, so copy the outer level's
2759  * donemap; this ensures that we see states in process of being
2760  * visited at outer levels, or already merged into predecessor
2761  * states, as ones we shouldn't traverse back to.
2762  */
2763  memcpy(donemap, outerdonemap, nstates * sizeof(char));
2764  }
2765  else
2766  {
2767  /* At outermost level, only spredecessor is off-limits */
2768  memset(donemap, 0, nstates * sizeof(char));
2769  assert(spredecessor->no < nstates);
2770  donemap[spredecessor->no] = 1;
2771  }
2772  }
2773 
2774  /* Mark ssource as visited in the donemap */
2775  assert(ssource->no < nstates);
2776  assert(donemap[ssource->no] == 0);
2777  donemap[ssource->no] = 1;
2778 
2779  /*
2780  * We proceed by first cloning all of ssource's outarcs, creating new
2781  * clone states as needed but not doing more with them than that. Then in
2782  * a second pass, recurse to process the child clone states. This allows
2783  * us to have only one child clone state per reachable source state, even
2784  * when there are multiple outarcs leading to the same state. Also, when
2785  * we do visit a child state, its set of inarcs is known exactly, which
2786  * makes it safe to apply the constraint-is-already-checked optimization.
2787  * Also, this ensures that we've merged all the states we can into the
2788  * current clone before we recurse to any children, thus possibly saving
2789  * them from making extra images of those states.
2790  *
2791  * While this function runs, child clone states of the current state are
2792  * marked by setting their tmp fields to point to the original state they
2793  * were cloned from. This makes it possible to detect multiple outarcs
2794  * leading to the same state, and also makes it easy to distinguish clone
2795  * states from original states (which will have tmp == NULL).
2796  */
2797  for (a = ssource->outs; a != NULL && !NISERR(); a = a->outchain)
2798  {
2799  struct state *sto = a->to;
2800 
2801  /*
2802  * We do not consider cloning successor states that have no constraint
2803  * outarcs; just link to them as-is. They cannot be part of a
2804  * constraint loop so there is no need to make copies. In particular,
2805  * this rule keeps us from trying to clone the post state, which would
2806  * be a bad idea.
2807  */
2808  if (isconstraintarc(a) && hasconstraintout(sto))
2809  {
2810  struct state *prevclone;
2811  int canmerge;
2812  struct arc *a2;
2813 
2814  /*
2815  * Back-link constraint arcs must not be followed. Nor is there a
2816  * need to revisit states previously merged into this clone.
2817  */
2818  assert(sto->no < nstates);
2819  if (donemap[sto->no] != 0)
2820  continue;
2821 
2822  /*
2823  * Check whether we already have a child clone state for this
2824  * source state.
2825  */
2826  prevclone = NULL;
2827  for (a2 = sclone->outs; a2 != NULL; a2 = a2->outchain)
2828  {
2829  if (a2->to->tmp == sto)
2830  {
2831  prevclone = a2->to;
2832  break;
2833  }
2834  }
2835 
2836  /*
2837  * If this arc is labeled the same as refarc, or the same as any
2838  * arc we must have traversed to get to sclone, then no additional
2839  * constraints need to be met to get to sto, so we should just
2840  * merge its outarcs into sclone.
2841  */
2842  if (refarc && a->type == refarc->type && a->co == refarc->co)
2843  canmerge = 1;
2844  else
2845  {
2846  struct state *s;
2847 
2848  canmerge = 0;
2849  for (s = sclone; s->ins; s = s->ins->from)
2850  {
2851  if (s->nins == 1 &&
2852  a->type == s->ins->type && a->co == s->ins->co)
2853  {
2854  canmerge = 1;
2855  break;
2856  }
2857  }
2858  }
2859 
2860  if (canmerge)
2861  {
2862  /*
2863  * We can merge into sclone. If we previously made a child
2864  * clone state, drop it; there's no need to visit it. (This
2865  * can happen if ssource has multiple pathways to sto, and we
2866  * only just now found one that is provably a no-op.)
2867  */
2868  if (prevclone)
2869  dropstate(nfa, prevclone); /* kills our outarc, too */
2870 
2871  /* Recurse to merge sto's outarcs into sclone */
2873  sto,
2874  sclone,
2875  spredecessor,
2876  refarc,
2877  donemap,
2878  outerdonemap,
2879  nstates);
2880  /* sto should now be marked as previously visited */
2881  assert(NISERR() || donemap[sto->no] == 1);
2882  }
2883  else if (prevclone)
2884  {
2885  /*
2886  * We already have a clone state for this successor, so just
2887  * make another arc to it.
2888  */
2889  cparc(nfa, a, sclone, prevclone);
2890  }
2891  else
2892  {
2893  /*
2894  * We need to create a new successor clone state.
2895  */
2896  struct state *stoclone;
2897 
2898  stoclone = newstate(nfa);
2899  if (stoclone == NULL)
2900  {
2901  assert(NISERR());
2902  break;
2903  }
2904  /* Mark it as to what it's a clone of */
2905  stoclone->tmp = sto;
2906  /* ... and add the outarc leading to it */
2907  cparc(nfa, a, sclone, stoclone);
2908  }
2909  }
2910  else
2911  {
2912  /*
2913  * Non-constraint outarcs just get copied to sclone, as do outarcs
2914  * leading to states with no constraint outarc.
2915  */
2916  cparc(nfa, a, sclone, sto);
2917  }
2918  }
2919 
2920  /*
2921  * If we are at outer level for this clone state, recurse to all its child
2922  * clone states, clearing their tmp fields as we go. (If we're not
2923  * outermost for sclone, leave this to be done by the outer call level.)
2924  * Note that if we have multiple outarcs leading to the same clone state,
2925  * it will only be recursed-to once.
2926  */
2927  if (curdonemap == NULL)
2928  {
2929  for (a = sclone->outs; a != NULL && !NISERR(); a = a->outchain)
2930  {
2931  struct state *stoclone = a->to;
2932  struct state *sto = stoclone->tmp;
2933 
2934  if (sto != NULL)
2935  {
2936  stoclone->tmp = NULL;
2938  sto,
2939  stoclone,
2940  spredecessor,
2941  refarc,
2942  NULL,
2943  donemap,
2944  nstates);
2945  }
2946  }
2947 
2948  /* Don't forget to free sclone's donemap when done with it */
2949  FREE(donemap);
2950  }
2951 }
#define NISERR()
Definition: regc_nfa.c:39
int no
Definition: regguts.h:319
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
#define REG_ESPACE
Definition: regex.h:151
Definition: regguts.h:290
int nins
Definition: regguts.h:322
#define FREE(ptr)
Definition: cryptohash.c:37
#define MALLOC(n)
Definition: regcustom.h:60
struct vars * v
Definition: regguts.h:364
static const FormData_pg_attribute a2
Definition: heap.c:167
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
static int isconstraintarc(struct arc *a)
Definition: regc_nfa.c:2352
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static int hasconstraintout(struct state *s)
Definition: regc_nfa.c:2370
static void clonesuccessorstates(struct nfa *nfa, struct state *ssource, struct state *sclone, struct state *spredecessor, struct arc *refarc, char *curdonemap, char *outerdonemap, int nstates)
Definition: regc_nfa.c:2725
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
color co
Definition: regguts.h:293
Definition: regguts.h:317
#define REG_ETOOBIG
Definition: regex.h:157
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:230
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520
struct arc * ins
Definition: regguts.h:324

◆ combine()

static int combine ( struct nfa nfa,
struct arc con,
struct arc a 
)
static

Definition at line 2008 of file regc_nfa.c.

References AHEAD, assert, BEHIND, CA, colormap::cd, nfa::cm, arc::co, COMPATIBLE, colordesc::flags, INCOMPATIBLE, LACON, NOTREACHED, PLAIN, PSEUDO, RAINBOW, REPLACEARC, SATISFIED, and arc::type.

Referenced by pull(), and push().

2011 {
2012 #define CA(ct,at) (((ct)<<CHAR_BIT) | (at))
2013 
2014  switch (CA(con->type, a->type))
2015  {
2016  case CA('^', PLAIN): /* newlines are handled separately */
2017  case CA('$', PLAIN):
2018  return INCOMPATIBLE;
2019  break;
2020  case CA(AHEAD, PLAIN): /* color constraints meet colors */
2021  case CA(BEHIND, PLAIN):
2022  if (con->co == a->co)
2023  return SATISFIED;
2024  if (con->co == RAINBOW)
2025  {
2026  /* con is satisfied unless arc's color is a pseudocolor */
2027  if (!(nfa->cm->cd[a->co].flags & PSEUDO))
2028  return SATISFIED;
2029  }
2030  else if (a->co == RAINBOW)
2031  {
2032  /* con is incompatible if it's for a pseudocolor */
2033  /* (this is hypothetical; we make no such constraints today) */
2034  if (nfa->cm->cd[con->co].flags & PSEUDO)
2035  return INCOMPATIBLE;
2036  /* otherwise, constraint constrains arc to be only its color */
2037  return REPLACEARC;
2038  }
2039  return INCOMPATIBLE;
2040  break;
2041  case CA('^', '^'): /* collision, similar constraints */
2042  case CA('$', '$'):
2043  if (con->co == a->co) /* true duplication */
2044  return SATISFIED;
2045  return INCOMPATIBLE;
2046  break;
2047  case CA(AHEAD, AHEAD): /* collision, similar constraints */
2048  case CA(BEHIND, BEHIND):
2049  if (con->co == a->co) /* true duplication */
2050  return SATISFIED;
2051  if (con->co == RAINBOW)
2052  {
2053  /* con is satisfied unless arc's color is a pseudocolor */
2054  if (!(nfa->cm->cd[a->co].flags & PSEUDO))
2055  return SATISFIED;
2056  }
2057  else if (a->co == RAINBOW)
2058  {
2059  /* con is incompatible if it's for a pseudocolor */
2060  /* (this is hypothetical; we make no such constraints today) */
2061  if (nfa->cm->cd[con->co].flags & PSEUDO)
2062  return INCOMPATIBLE;
2063  /* otherwise, constraint constrains arc to be only its color */
2064  return REPLACEARC;
2065  }
2066  return INCOMPATIBLE;
2067  break;
2068  case CA('^', BEHIND): /* collision, dissimilar constraints */
2069  case CA(BEHIND, '^'):
2070  case CA('$', AHEAD):
2071  case CA(AHEAD, '$'):
2072  return INCOMPATIBLE;
2073  break;
2074  case CA('^', '$'): /* constraints passing each other */
2075  case CA('^', AHEAD):
2076  case CA(BEHIND, '$'):
2077  case CA(BEHIND, AHEAD):
2078  case CA('$', '^'):
2079  case CA('$', BEHIND):
2080  case CA(AHEAD, '^'):
2081  case CA(AHEAD, BEHIND):
2082  case CA('^', LACON):
2083  case CA(BEHIND, LACON):
2084  case CA('$', LACON):
2085  case CA(AHEAD, LACON):
2086  return COMPATIBLE;
2087  break;
2088  }
2089  assert(NOTREACHED);
2090  return INCOMPATIBLE; /* for benefit of blind compilers */
2091 }
int type
Definition: regguts.h:292
#define RAINBOW
Definition: regguts.h:154
#define CA(ct, at)
#define BEHIND
Definition: regcomp.c:299
struct colordesc * cd
Definition: regguts.h:231
#define NOTREACHED
Definition: regguts.h:91
#define SATISFIED
Definition: regcomp.c:165
#define LACON
Definition: regcomp.c:297
#define REPLACEARC
Definition: regcomp.c:167
#define assert(TEST)
Definition: imath.c:73
struct colormap * cm
Definition: regguts.h:358
#define COMPATIBLE
Definition: regcomp.c:166
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
int flags
Definition: regguts.h:179
#define PSEUDO
Definition: regguts.h:181
#define INCOMPATIBLE
Definition: regcomp.c:164
#define AHEAD
Definition: regcomp.c:298

◆ compact()

static void compact ( struct nfa nfa,
struct cnfa cnfa 
)
static

Definition at line 3511 of file regc_nfa.c.

References cnfa::arcs, assert, nfa::bos, cnfa::bos, carcsort(), nfa::cm, CNFA_NOPROGRESS, arc::co, carc::co, COLORLESS, nfa::eos, cnfa::eos, nfa::flags, cnfa::flags, FREE, HASLACONS, LACON, MALLOC, maxcolor(), nfa::maxmatchall, cnfa::maxmatchall, nfa::minmatchall, cnfa::minmatchall, cnfa::ncolors, NERR, state::next, NISERR, state::no, state::nouts, cnfa::nstates, arc::outchain, state::outs, PLAIN, nfa::post, cnfa::post, nfa::pre, cnfa::pre, REG_ASSERT, REG_ESPACE, nfa::states, cnfa::states, cnfa::stflags, arc::to, carc::to, and arc::type.

3513 {
3514  struct state *s;
3515  struct arc *a;
3516  size_t nstates;
3517  size_t narcs;
3518  struct carc *ca;
3519  struct carc *first;
3520 
3521  assert(!NISERR());
3522 
3523  nstates = 0;
3524  narcs = 0;
3525  for (s = nfa->states; s != NULL; s = s->next)
3526  {
3527  nstates++;
3528  narcs += s->nouts + 1; /* need one extra for endmarker */
3529  }
3530 
3531  cnfa->stflags = (char *) MALLOC(nstates * sizeof(char));
3532  cnfa->states = (struct carc **) MALLOC(nstates * sizeof(struct carc *));
3533  cnfa->arcs = (struct carc *) MALLOC(narcs * sizeof(struct carc));
3534  if (cnfa->stflags == NULL || cnfa->states == NULL || cnfa->arcs == NULL)
3535  {
3536  if (cnfa->stflags != NULL)
3537  FREE(cnfa->stflags);
3538  if (cnfa->states != NULL)
3539  FREE(cnfa->states);
3540  if (cnfa->arcs != NULL)
3541  FREE(cnfa->arcs);
3542  NERR(REG_ESPACE);
3543  return;
3544  }
3545  cnfa->nstates = nstates;
3546  cnfa->pre = nfa->pre->no;
3547  cnfa->post = nfa->post->no;
3548  cnfa->bos[0] = nfa->bos[0];
3549  cnfa->bos[1] = nfa->bos[1];
3550  cnfa->eos[0] = nfa->eos[0];
3551  cnfa->eos[1] = nfa->eos[1];
3552  cnfa->ncolors = maxcolor(nfa->cm) + 1;
3553  cnfa->flags = nfa->flags;
3554  cnfa->minmatchall = nfa->minmatchall;
3555  cnfa->maxmatchall = nfa->maxmatchall;
3556 
3557  ca = cnfa->arcs;
3558  for (s = nfa->states; s != NULL; s = s->next)
3559  {
3560  assert((size_t) s->no < nstates);
3561  cnfa->stflags[s->no] = 0;
3562  cnfa->states[s->no] = ca;
3563  first = ca;
3564  for (a = s->outs; a != NULL; a = a->outchain)
3565  switch (a->type)
3566  {
3567  case PLAIN:
3568  ca->co = a->co;
3569  ca->to = a->to->no;
3570  ca++;
3571  break;
3572  case LACON:
3573  assert(s->no != cnfa->pre);
3574  assert(a->co >= 0);
3575  ca->co = (color) (cnfa->ncolors + a->co);
3576  ca->to = a->to->no;
3577  ca++;
3578  cnfa->flags |= HASLACONS;
3579  break;
3580  default:
3581  NERR(REG_ASSERT);
3582  return;
3583  }
3584  carcsort(first, ca - first);
3585  ca->co = COLORLESS;
3586  ca->to = 0;
3587  ca++;
3588  }
3589  assert(ca == &cnfa->arcs[narcs]);
3590  assert(cnfa->nstates != 0);
3591 
3592  /* mark no-progress states */
3593  for (a = nfa->pre->outs; a != NULL; a = a->outchain)
3594  cnfa->stflags[a->to->no] = CNFA_NOPROGRESS;
3595  cnfa->stflags[nfa->pre->no] = CNFA_NOPROGRESS;
3596 }
#define NISERR()
Definition: regc_nfa.c:39
int no
Definition: regguts.h:319
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
int maxmatchall
Definition: regguts.h:419
#define CNFA_NOPROGRESS
Definition: regguts.h:413
#define REG_ESPACE
Definition: regex.h:151
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
short color
Definition: regguts.h:150
int pre
Definition: regguts.h:408
color eos[2]
Definition: regguts.h:360
int flags
Definition: regguts.h:361
struct state * post
Definition: regguts.h:348
#define FREE(ptr)
Definition: cryptohash.c:37
struct carc ** states
Definition: regguts.h:414
#define MALLOC(n)
Definition: regcustom.h:60
int maxmatchall
Definition: regguts.h:363
int nstates
Definition: regguts.h:403
#define LACON
Definition: regcomp.c:297
struct arc * outchain
Definition: regguts.h:296
color bos[2]
Definition: regguts.h:359
#define assert(TEST)
Definition: imath.c:73
struct colormap * cm
Definition: regguts.h:358
struct state * next
Definition: regguts.h:327
struct arc * outs
Definition: regguts.h:325
char * stflags
Definition: regguts.h:412
int minmatchall
Definition: regguts.h:362
color bos[2]
Definition: regguts.h:410
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
int to
Definition: regguts.h:398
color co
Definition: regguts.h:293
#define REG_ASSERT
Definition: regex.h:153
Definition: regguts.h:317
int flags
Definition: regguts.h:405
struct carc * arcs
Definition: regguts.h:416
int minmatchall
Definition: regguts.h:418
int post
Definition: regguts.h:409
int nouts
Definition: regguts.h:323
color co
Definition: regguts.h:397
static color maxcolor(struct colormap *cm)
Definition: regc_color.c:172
static void carcsort(struct carc *first, size_t n)
Definition: regc_nfa.c:3602
#define COLORLESS
Definition: regguts.h:153
#define HASLACONS
Definition: regguts.h:406
struct state * pre
Definition: regguts.h:345
color eos[2]
Definition: regguts.h:411
int ncolors
Definition: regguts.h:404
Definition: regguts.h:395

◆ copyins()

static void copyins ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 894 of file regc_nfa.c.

References assert, BULK_ARC_OP_USE_SORT, CANCEL_REQUESTED, arc::co, cparc(), createarc(), arc::from, arc::inchain, state::ins, NERR, state::nins, NISERR, NOTREACHED, vars::re, REG_CANCEL, sortins(), sortins_cmp(), arc::type, and nfa::v.

Referenced by pull().

897 {
898  assert(oldState != newState);
899  assert(newState->nins == 0); /* see comment above */
900 
901  if (newState->nins == 0)
902  {
903  /* No need for de-duplication */
904  struct arc *a;
905 
906  for (a = oldState->ins; a != NULL; a = a->inchain)
907  createarc(nfa, a->type, a->co, a->from, newState);
908  }
909 #ifdef NOT_USED /* see comment above */
910  else if (!BULK_ARC_OP_USE_SORT(oldState->nins, newState->nins))
911  {
912  /* With not too many arcs, just do them one at a time */
913  struct arc *a;
914 
915  for (a = oldState->ins; a != NULL; a = a->inchain)
916  cparc(nfa, a, a->from, newState);
917  }
918  else
919  {
920  /*
921  * With many arcs, use a sort-merge approach. Note that createarc()
922  * will put new arcs onto the front of newState's chain, so it does
923  * not break our walk through the sorted part of the chain.
924  */
925  struct arc *oa;
926  struct arc *na;
927 
928  /*
929  * Because we bypass newarc() in this code path, we'd better include a
930  * cancel check.
931  */
932  if (CANCEL_REQUESTED(nfa->v->re))
933  {
934  NERR(REG_CANCEL);
935  return;
936  }
937 
938  sortins(nfa, oldState);
939  sortins(nfa, newState);
940  if (NISERR())
941  return; /* might have failed to sort */
942  oa = oldState->ins;
943  na = newState->ins;
944  while (oa != NULL && na != NULL)
945  {
946  struct arc *a = oa;
947 
948  switch (sortins_cmp(&oa, &na))
949  {
950  case -1:
951  /* newState does not have anything matching oa */
952  oa = oa->inchain;
953  createarc(nfa, a->type, a->co, a->from, newState);
954  break;
955  case 0:
956  /* match, advance in both lists */
957  oa = oa->inchain;
958  na = na->inchain;
959  break;
960  case +1:
961  /* advance only na; oa might have a match later */
962  na = na->inchain;
963  break;
964  default:
966  }
967  }
968  while (oa != NULL)
969  {
970  /* newState does not have anything matching oa */
971  struct arc *a = oa;
972 
973  oa = oa->inchain;
974  createarc(nfa, a->type, a->co, a->from, newState);
975  }
976  }
977 #endif /* NOT_USED */
978 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
int nins
Definition: regguts.h:322
#define NOTREACHED
Definition: regguts.h:91
struct vars * v
Definition: regguts.h:364
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)
Definition: regc_nfa.c:766
regex_t * re
Definition: regcomp.c:239
#define assert(TEST)
Definition: imath.c:73
static int sortins_cmp(const void *a, const void *b)
Definition: regc_nfa.c:670
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:331
color co
Definition: regguts.h:293
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
struct arc * inchain
Definition: regguts.h:299
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
struct arc * ins
Definition: regguts.h:324
#define REG_CANCEL
Definition: regex.h:159
static void sortins(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:628

◆ copyouts()

static void copyouts ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 1191 of file regc_nfa.c.

References assert, BULK_ARC_OP_USE_SORT, CANCEL_REQUESTED, arc::co, cparc(), createarc(), NERR, NISERR, NOTREACHED, state::nouts, arc::outchain, state::outs, vars::re, REG_CANCEL, sortouts(), sortouts_cmp(), arc::to, arc::type, and nfa::v.

Referenced by push().

1194 {
1195  assert(oldState != newState);
1196  assert(newState->nouts == 0); /* see comment above */
1197 
1198  if (newState->nouts == 0)
1199  {
1200  /* No need for de-duplication */
1201  struct arc *a;
1202 
1203  for (a = oldState->outs; a != NULL; a = a->outchain)
1204  createarc(nfa, a->type, a->co, newState, a->to);
1205  }
1206 #ifdef NOT_USED /* see comment above */
1207  else if (!BULK_ARC_OP_USE_SORT(oldState->nouts, newState->nouts))
1208  {
1209  /* With not too many arcs, just do them one at a time */
1210  struct arc *a;
1211 
1212  for (a = oldState->outs; a != NULL; a = a->outchain)
1213  cparc(nfa, a, newState, a->to);
1214  }
1215  else
1216  {
1217  /*
1218  * With many arcs, use a sort-merge approach. Note that createarc()
1219  * will put new arcs onto the front of newState's chain, so it does
1220  * not break our walk through the sorted part of the chain.
1221  */
1222  struct arc *oa;
1223  struct arc *na;
1224 
1225  /*
1226  * Because we bypass newarc() in this code path, we'd better include a
1227  * cancel check.
1228  */
1229  if (CANCEL_REQUESTED(nfa->v->re))
1230  {
1231  NERR(REG_CANCEL);
1232  return;
1233  }
1234 
1235  sortouts(nfa, oldState);
1236  sortouts(nfa, newState);
1237  if (NISERR())
1238  return; /* might have failed to sort */
1239  oa = oldState->outs;
1240  na = newState->outs;
1241  while (oa != NULL && na != NULL)
1242  {
1243  struct arc *a = oa;
1244 
1245  switch (sortouts_cmp(&oa, &na))
1246  {
1247  case -1:
1248  /* newState does not have anything matching oa */
1249  oa = oa->outchain;
1250  createarc(nfa, a->type, a->co, newState, a->to);
1251  break;
1252  case 0:
1253  /* match, advance in both lists */
1254  oa = oa->outchain;
1255  na = na->outchain;
1256  break;
1257  case +1:
1258  /* advance only na; oa might have a match later */
1259  na = na->outchain;
1260  break;
1261  default:
1262  assert(NOTREACHED);
1263  }
1264  }
1265  while (oa != NULL)
1266  {
1267  /* newState does not have anything matching oa */
1268  struct arc *a = oa;
1269 
1270  oa = oa->outchain;
1271  createarc(nfa, a->type, a->co, newState, a->to);
1272  }
1273  }
1274 #endif /* NOT_USED */
1275 }
#define NISERR()
Definition: regc_nfa.c:39
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
#define NOTREACHED
Definition: regguts.h:91
struct vars * v
Definition: regguts.h:364
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)
Definition: regc_nfa.c:766
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
struct state * to
Definition: regguts.h:295
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:331
color co
Definition: regguts.h:293
static void sortouts(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:695
static int sortouts_cmp(const void *a, const void *b)
Definition: regc_nfa.c:737
int nouts
Definition: regguts.h:323
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
#define REG_CANCEL
Definition: regex.h:159

◆ cparc()

static void cparc ( struct nfa nfa,
struct arc oa,
struct state from,
struct state to 
)
static

Definition at line 616 of file regc_nfa.c.

References arc::co, newarc(), and arc::type.

Referenced by breakconstraintloop(), clonesuccessorstates(), copyins(), copyouts(), duptraverse(), moveins(), moveouts(), pull(), and push().

620 {
621  newarc(nfa, oa->type, oa->co, from, to);
622 }
int type
Definition: regguts.h:292
color co
Definition: regguts.h:293
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285

◆ createarc()

static void createarc ( struct nfa nfa,
int  t,
color  co,
struct state from,
struct state to 
)
static

Definition at line 331 of file regc_nfa.c.

References allocarc(), assert, nfa::cm, arc::co, colorchain(), COLORED, arc::from, arc::inchain, arc::inchainRev, state::ins, state::nins, NISERR, state::nouts, arc::outchain, arc::outchainRev, state::outs, nfa::parent, arc::to, and arc::type.

Referenced by copyins(), copyouts(), mergeins(), moveins(), moveouts(), and newarc().

336 {
337  struct arc *a;
338 
339  a = allocarc(nfa);
340  if (NISERR())
341  return;
342  assert(a != NULL);
343 
344  a->type = t;
345  a->co = co;
346  a->to = to;
347  a->from = from;
348 
349  /*
350  * Put the new arc on the beginning, not the end, of the chains; it's
351  * simpler here, and freearc() is the same cost either way. See also the
352  * logic in moveins() and its cohorts, as well as fixempties().
353  */
354  a->inchain = to->ins;
355  a->inchainRev = NULL;
356  if (to->ins)
357  to->ins->inchainRev = a;
358  to->ins = a;
359  a->outchain = from->outs;
360  a->outchainRev = NULL;
361  if (from->outs)
362  from->outs->outchainRev = a;
363  from->outs = a;
364 
365  from->nouts++;
366  to->nins++;
367 
368  if (COLORED(a) && nfa->parent == NULL)
369  colorchain(nfa->cm, a);
370 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
int nins
Definition: regguts.h:322
#define COLORED(a)
Definition: regcomp.c:307
static struct arc * allocarc(struct nfa *nfa)
Definition: regc_nfa.c:376
struct arc * inchainRev
Definition: regguts.h:300
static void colorchain(struct colormap *cm, struct arc *a)
Definition: regc_color.c:984
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct colormap * cm
Definition: regguts.h:358
struct arc * outs
Definition: regguts.h:325
struct state * to
Definition: regguts.h:295
color co
Definition: regguts.h:293
int nouts
Definition: regguts.h:323
struct nfa * parent
Definition: regguts.h:365
struct arc * inchain
Definition: regguts.h:299
struct arc * outchainRev
Definition: regguts.h:297
struct arc * ins
Definition: regguts.h:324

◆ delsub()

static void delsub ( struct nfa nfa,
struct state lp,
struct state rp 
)
static

Definition at line 1309 of file regc_nfa.c.

References assert, deltraverse(), FREESTATE, state::nins, NISERR, state::no, state::nouts, and state::tmp.

1312 {
1313  assert(lp != rp);
1314 
1315  rp->tmp = rp; /* mark end */
1316 
1317  deltraverse(nfa, lp, lp);
1318  if (NISERR())
1319  return; /* asserts might not hold after failure */
1320  assert(lp->nouts == 0 && rp->nins == 0); /* did the job */
1321  assert(lp->no != FREESTATE && rp->no != FREESTATE); /* no more */
1322 
1323  rp->tmp = NULL; /* unmark end */
1324  lp->tmp = NULL; /* and begin, marked by deltraverse */
1325 }
#define NISERR()
Definition: regc_nfa.c:39
int no
Definition: regguts.h:319
#define FREESTATE
Definition: regguts.h:320
int nins
Definition: regguts.h:322
static void deltraverse(struct nfa *nfa, struct state *leftend, struct state *s)
Definition: regc_nfa.c:1332
#define assert(TEST)
Definition: imath.c:73
struct state * tmp
Definition: regguts.h:326
int nouts
Definition: regguts.h:323

◆ deltraverse()

static void deltraverse ( struct nfa nfa,
struct state leftend,
struct state s 
)
static

Definition at line 1332 of file regc_nfa.c.

References assert, freearc(), freestate(), FREESTATE, NERR, state::nins, NISERR, state::no, state::nouts, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, and nfa::v.

Referenced by delsub().

1335 {
1336  struct arc *a;
1337  struct state *to;
1338 
1339  /* Since this is recursive, it could be driven to stack overflow */
1340  if (STACK_TOO_DEEP(nfa->v->re))
1341  {
1342  NERR(REG_ETOOBIG);
1343  return;
1344  }
1345 
1346  if (s->nouts == 0)
1347  return; /* nothing to do */
1348  if (s->tmp != NULL)
1349  return; /* already in progress */
1350 
1351  s->tmp = s; /* mark as in progress */
1352 
1353  while ((a = s->outs) != NULL)
1354  {
1355  to = a->to;
1356  deltraverse(nfa, leftend, to);
1357  if (NISERR())
1358  return; /* asserts might not hold after failure */
1359  assert(to->nouts == 0 || to->tmp != NULL);
1360  freearc(nfa, a);
1361  if (to->nins == 0 && to->tmp == NULL)
1362  {
1363  assert(to->nouts == 0);
1364  freestate(nfa, to);
1365  }
1366  }
1367 
1368  assert(s->no != FREESTATE); /* we're still here */
1369  assert(s == leftend || s->nins != 0); /* and still reachable */
1370  assert(s->nouts == 0); /* but have no outarcs */
1371 
1372  s->tmp = NULL; /* we're done here */
1373 }
#define NISERR()
Definition: regc_nfa.c:39
int no
Definition: regguts.h:319
#define NERR(e)
Definition: regc_nfa.c:40
#define FREESTATE
Definition: regguts.h:320
Definition: regguts.h:290
int nins
Definition: regguts.h:322
static void deltraverse(struct nfa *nfa, struct state *leftend, struct state *s)
Definition: regc_nfa.c:1332
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
Definition: regguts.h:317
#define REG_ETOOBIG
Definition: regex.h:157
int nouts
Definition: regguts.h:323
static void freestate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:246
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ dropstate()

static void dropstate ( struct nfa nfa,
struct state s 
)
static

Definition at line 230 of file regc_nfa.c.

References freearc(), freestate(), state::ins, and state::outs.

Referenced by cleanup(), clonesuccessorstates(), fixconstraintloops(), fixempties(), pullback(), and pushfwd().

232 {
233  struct arc *a;
234 
235  while ((a = s->ins) != NULL)
236  freearc(nfa, a);
237  while ((a = s->outs) != NULL)
238  freearc(nfa, a);
239  freestate(nfa, s);
240 }
Definition: regguts.h:290
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
static void freestate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:246
struct arc * ins
Definition: regguts.h:324

◆ dumpnfa()

static void dumpnfa ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 3643 of file regc_nfa.c.

References AHEAD, assert, BEHIND, nfa::bos, cnfa::bos, nfa::cm, CNFA_NOPROGRESS, arc::co, carc::co, COLORLESS, DUPINF, EMPTY, nfa::eos, cnfa::eos, state::flag, nfa::flags, cnfa::flags, fprintf, arc::from, HASLACONS, arc::inchain, state::ins, LACON, MATCHALL, nfa::maxmatchall, cnfa::maxmatchall, nfa::minmatchall, cnfa::minmatchall, cnfa::ncolors, state::next, state::no, state::nouts, cnfa::nstates, arc::outchain, arc::outchainRev, state::outs, nfa::parent, PLAIN, nfa::post, cnfa::post, nfa::pre, cnfa::pre, state::prev, RAINBOW, nfa::states, cnfa::states, cnfa::stflags, state::tmp, arc::to, carc::to, and arc::type.

Referenced by fixconstraintloops(), fixempties(), optimize(), pullback(), and pushfwd().

3645 {
3646 #ifdef REG_DEBUG
3647  struct state *s;
3648  int nstates = 0;
3649  int narcs = 0;
3650 
3651  fprintf(f, "pre %d, post %d", nfa->pre->no, nfa->post->no);
3652  if (nfa->bos[0] != COLORLESS)
3653  fprintf(f, ", bos [%ld]", (long) nfa->bos[0]);
3654  if (nfa->bos[1] != COLORLESS)
3655  fprintf(f, ", bol [%ld]", (long) nfa->bos[1]);
3656  if (nfa->eos[0] != COLORLESS)
3657  fprintf(f, ", eos [%ld]", (long) nfa->eos[0]);
3658  if (nfa->eos[1] != COLORLESS)
3659  fprintf(f, ", eol [%ld]", (long) nfa->eos[1]);
3660  if (nfa->flags & HASLACONS)
3661  fprintf(f, ", haslacons");
3662  if (nfa->flags & MATCHALL)
3663  {
3664  fprintf(f, ", minmatchall %d", nfa->minmatchall);
3665  if (nfa->maxmatchall == DUPINF)
3666  fprintf(f, ", maxmatchall inf");
3667  else
3668  fprintf(f, ", maxmatchall %d", nfa->maxmatchall);
3669  }
3670  fprintf(f, "\n");
3671  for (s = nfa->states; s != NULL; s = s->next)
3672  {
3673  dumpstate(s, f);
3674  nstates++;
3675  narcs += s->nouts;
3676  }
3677  fprintf(f, "total of %d states, %d arcs\n", nstates, narcs);
3678  if (nfa->parent == NULL)
3679  dumpcolors(nfa->cm, f);
3680  fflush(f);
3681 #endif
3682 }
int no
Definition: regguts.h:319
struct state * states
Definition: regguts.h:350
color eos[2]
Definition: regguts.h:360
int flags
Definition: regguts.h:361
struct state * post
Definition: regguts.h:348
#define fprintf
Definition: port.h:220
int maxmatchall
Definition: regguts.h:363
color bos[2]
Definition: regguts.h:359
struct colormap * cm
Definition: regguts.h:358
struct state * next
Definition: regguts.h:327
#define MATCHALL
Definition: regguts.h:407
int minmatchall
Definition: regguts.h:362
#define DUPINF
Definition: regguts.h:94
Definition: regguts.h:317
int nouts
Definition: regguts.h:323
struct nfa * parent
Definition: regguts.h:365
#define COLORLESS
Definition: regguts.h:153
#define HASLACONS
Definition: regguts.h:406
struct state * pre
Definition: regguts.h:345

◆ dupnfa()

static void dupnfa ( struct nfa nfa,
struct state start,
struct state stop,
struct state from,
struct state to 
)
static

Definition at line 1383 of file regc_nfa.c.

References cleartraverse(), duptraverse(), EMPTY, newarc(), and state::tmp.

1388 {
1389  if (start == stop)
1390  {
1391  newarc(nfa, EMPTY, 0, from, to);
1392  return;
1393  }
1394 
1395  stop->tmp = to;
1396  duptraverse(nfa, start, from);
1397  /* done, except for clearing out the tmp pointers */
1398 
1399  stop->tmp = NULL;
1400  cleartraverse(nfa, start);
1401 }
static void cleartraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1515
struct state * tmp
Definition: regguts.h:326
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
#define EMPTY
Definition: regcomp.c:285
static void duptraverse(struct nfa *nfa, struct state *s, struct state *stmp)
Definition: regc_nfa.c:1407

◆ duptraverse()

static void duptraverse ( struct nfa nfa,
struct state s,
struct state stmp 
)
static

Definition at line 1407 of file regc_nfa.c.

References assert, cparc(), NERR, newstate(), NISERR, arc::outchain, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, and nfa::v.

Referenced by dupnfa().

1410 {
1411  struct arc *a;
1412 
1413  /* Since this is recursive, it could be driven to stack overflow */
1414  if (STACK_TOO_DEEP(nfa->v->re))
1415  {
1416  NERR(REG_ETOOBIG);
1417  return;
1418  }
1419 
1420  if (s->tmp != NULL)
1421  return; /* already done */
1422 
1423  s->tmp = (stmp == NULL) ? newstate(nfa) : stmp;
1424  if (s->tmp == NULL)
1425  {
1426  assert(NISERR());
1427  return;
1428  }
1429 
1430  for (a = s->outs; a != NULL && !NISERR(); a = a->outchain)
1431  {
1432  duptraverse(nfa, a->to, (struct state *) NULL);
1433  if (NISERR())
1434  break;
1435  assert(a->to->tmp != NULL);
1436  cparc(nfa, a, s->tmp, a->to->tmp);
1437  }
1438 }
#define NISERR()
Definition: regc_nfa.c:39
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
Definition: regguts.h:317
#define REG_ETOOBIG
Definition: regex.h:157
static void duptraverse(struct nfa *nfa, struct state *s, struct state *stmp)
Definition: regc_nfa.c:1407
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ emptyreachable()

static struct state* emptyreachable ( struct nfa nfa,
struct state s,
struct state lastfound,
struct arc **  inarcsorig 
)
static

Definition at line 2324 of file regc_nfa.c.

References EMPTY, arc::from, arc::inchain, NERR, state::no, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::type, and nfa::v.

Referenced by fixempties().

2328 {
2329  struct arc *a;
2330 
2331  /* Since this is recursive, it could be driven to stack overflow */
2332  if (STACK_TOO_DEEP(nfa->v->re))
2333  {
2334  NERR(REG_ETOOBIG);
2335  return lastfound;
2336  }
2337 
2338  s->tmp = lastfound;
2339  lastfound = s;
2340  for (a = inarcsorig[s->no]; a != NULL; a = a->inchain)
2341  {
2342  if (a->type == EMPTY && a->from->tmp == NULL)
2343  lastfound = emptyreachable(nfa, a->from, lastfound, inarcsorig);
2344  }
2345  return lastfound;
2346 }
int no
Definition: regguts.h:319
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
static struct state * emptyreachable(struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
Definition: regc_nfa.c:2324
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct state * tmp
Definition: regguts.h:326
#define REG_ETOOBIG
Definition: regex.h:157
#define EMPTY
Definition: regcomp.c:285
struct arc * inchain
Definition: regguts.h:299
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ findarc()

static struct arc* findarc ( struct state s,
int  type,
color  co 
)
static

Definition at line 600 of file regc_nfa.c.

References arc::co, arc::outchain, state::outs, and arc::type.

Referenced by colorcomplement().

603 {
604  struct arc *a;
605 
606  for (a = s->outs; a != NULL; a = a->outchain)
607  if (a->type == type && a->co == co)
608  return a;
609  return NULL;
610 }
int type
Definition: regguts.h:292
Definition: regguts.h:290
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
color co
Definition: regguts.h:293

◆ findconstraintloop()

static int findconstraintloop ( struct nfa nfa,
struct state s 
)
static

Definition at line 2490 of file regc_nfa.c.

References assert, breakconstraintloop(), isconstraintarc(), NERR, arc::outchain, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, and nfa::v.

Referenced by fixconstraintloops().

2491 {
2492  struct arc *a;
2493 
2494  /* Since this is recursive, it could be driven to stack overflow */
2495  if (STACK_TOO_DEEP(nfa->v->re))
2496  {
2497  NERR(REG_ETOOBIG);
2498  return 1; /* to exit as quickly as possible */
2499  }
2500 
2501  if (s->tmp != NULL)
2502  {
2503  /* Already proven uninteresting? */
2504  if (s->tmp == s)
2505  return 0;
2506  /* Found a loop involving s */
2507  breakconstraintloop(nfa, s);
2508  /* The tmp fields have been cleaned up by breakconstraintloop */
2509  return 1;
2510  }
2511  for (a = s->outs; a != NULL; a = a->outchain)
2512  {
2513  if (isconstraintarc(a))
2514  {
2515  struct state *sto = a->to;
2516 
2517  assert(sto != s);
2518  s->tmp = sto;
2519  if (findconstraintloop(nfa, sto))
2520  return 1;
2521  }
2522  }
2523 
2524  /*
2525  * If we get here, no constraint loop exists leading out from s. Mark it
2526  * with s->tmp == s so we need not rediscover that fact again later.
2527  */
2528  s->tmp = s;
2529  return 0;
2530 }
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
static void breakconstraintloop(struct nfa *nfa, struct state *sinitial)
Definition: regc_nfa.c:2579
struct arc * outchain
Definition: regguts.h:296
static int isconstraintarc(struct arc *a)
Definition: regc_nfa.c:2352
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
struct state * tmp
Definition: regguts.h:326
static int findconstraintloop(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:2490
struct state * to
Definition: regguts.h:295
Definition: regguts.h:317
#define REG_ETOOBIG
Definition: regex.h:157
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ fixconstraintloops()

static void fixconstraintloops ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 2391 of file regc_nfa.c.

References assert, dropstate(), dumpnfa(), findconstraintloop(), state::flag, freearc(), isconstraintarc(), state::next, state::nins, NISERR, state::nouts, arc::outchain, state::outs, nfa::states, state::tmp, and arc::to.

Referenced by optimize().

2393 {
2394  struct state *s;
2395  struct state *nexts;
2396  struct arc *a;
2397  struct arc *nexta;
2398  int hasconstraints;
2399 
2400  /*
2401  * In the trivial case of a state that loops to itself, we can just drop
2402  * the constraint arc altogether. This is worth special-casing because
2403  * such loops are far more common than loops containing multiple states.
2404  * While we're at it, note whether any constraint arcs survive.
2405  */
2406  hasconstraints = 0;
2407  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2408  {
2409  nexts = s->next;
2410  /* while we're at it, ensure tmp fields are clear for next step */
2411  assert(s->tmp == NULL);
2412  for (a = s->outs; a != NULL && !NISERR(); a = nexta)
2413  {
2414  nexta = a->outchain;
2415  if (isconstraintarc(a))
2416  {
2417  if (a->to == s)
2418  freearc(nfa, a);
2419  else
2420  hasconstraints = 1;
2421  }
2422  }
2423  /* If we removed all the outarcs, the state is useless. */
2424  if (s->nouts == 0 && !s->flag)
2425  dropstate(nfa, s);
2426  }
2427 
2428  /* Nothing to do if no remaining constraint arcs */
2429  if (NISERR() || !hasconstraints)
2430  return;
2431 
2432  /*
2433  * Starting from each remaining NFA state, search outwards for a
2434  * constraint loop. If we find a loop, break the loop, then start the
2435  * search over. (We could possibly retain some state from the first scan,
2436  * but it would complicate things greatly, and multi-state constraint
2437  * loops are rare enough that it's not worth optimizing the case.)
2438  */
2439 restart:
2440  for (s = nfa->states; s != NULL && !NISERR(); s = s->next)
2441  {
2442  if (findconstraintloop(nfa, s))
2443  goto restart;
2444  }
2445 
2446  if (NISERR())
2447  return;
2448 
2449  /*
2450  * Now remove any states that have become useless. (This cleanup is not
2451  * very thorough, and would be even less so if we tried to combine it with
2452  * the previous step; but cleanup() will take care of anything we miss.)
2453  *
2454  * Because findconstraintloop intentionally doesn't reset all tmp fields,
2455  * we have to clear them after it's done. This is a convenient place to
2456  * do that, too.
2457  */
2458  for (s = nfa->states; s != NULL; s = nexts)
2459  {
2460  nexts = s->next;
2461  s->tmp = NULL;
2462  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
2463  dropstate(nfa, s);
2464  }
2465 
2466  if (f != NULL)
2467  dumpnfa(nfa, f);
2468 }
#define NISERR()
Definition: regc_nfa.c:39
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
static void dumpnfa(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:3643
struct arc * outchain
Definition: regguts.h:296
static int isconstraintarc(struct arc *a)
Definition: regc_nfa.c:2352
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
static int findconstraintloop(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:2490
struct state * to
Definition: regguts.h:295
Definition: regguts.h:317
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:230
int nouts
Definition: regguts.h:323

◆ fixempties()

static void fixempties ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 2097 of file regc_nfa.c.

References assert, dropstate(), dumpnfa(), EMPTY, emptyreachable(), state::flag, FREE, freearc(), arc::from, hasnonemptyout(), arc::inchain, state::ins, MALLOC, mergeins(), moveins(), moveouts(), NERR, state::next, state::nins, NISERR, state::no, state::nouts, nfa::nstates, arc::outchain, state::outs, REG_ESPACE, s2, nfa::states, state::tmp, arc::to, and arc::type.

Referenced by optimize().

2099 {
2100  struct state *s;
2101  struct state *s2;
2102  struct state *nexts;
2103  struct arc *a;
2104  struct arc *nexta;
2105  int totalinarcs;
2106  struct arc **inarcsorig;
2107  struct arc **arcarray;
2108  int arccount;
2109  int prevnins;
2110  int nskip;
2111 
2112  /*
2113  * First, get rid of any states whose sole out-arc is an EMPTY, since
2114  * they're basically just aliases for their successor. The parsing
2115  * algorithm creates enough of these that it's worth special-casing this.
2116  */
2117  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2118  {
2119  nexts = s->next;
2120  if (s->flag || s->nouts != 1)
2121  continue;
2122  a = s->outs;
2123  assert(a != NULL && a->outchain == NULL);
2124  if (a->type != EMPTY)
2125  continue;
2126  if (s != a->to)
2127  moveins(nfa, s, a->to);
2128  dropstate(nfa, s);
2129  }
2130 
2131  /*
2132  * Similarly, get rid of any state with a single EMPTY in-arc, by folding
2133  * it into its predecessor.
2134  */
2135  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2136  {
2137  nexts = s->next;
2138  /* while we're at it, ensure tmp fields are clear for next step */
2139  assert(s->tmp == NULL);
2140  if (s->flag || s->nins != 1)
2141  continue;
2142  a = s->ins;
2143  assert(a != NULL && a->inchain == NULL);
2144  if (a->type != EMPTY)
2145  continue;
2146  if (s != a->from)
2147  moveouts(nfa, s, a->from);
2148  dropstate(nfa, s);
2149  }
2150 
2151  if (NISERR())
2152  return;
2153 
2154  /*
2155  * For each remaining NFA state, find all other states from which it is
2156  * reachable by a chain of one or more EMPTY arcs. Then generate new arcs
2157  * that eliminate the need for each such chain.
2158  *
2159  * We could replace a chain of EMPTY arcs that leads from a "from" state
2160  * to a "to" state either by pushing non-EMPTY arcs forward (linking
2161  * directly from "from"'s predecessors to "to") or by pulling them back
2162  * (linking directly from "from" to "to"'s successors). We choose to
2163  * always do the former; this choice is somewhat arbitrary, but the
2164  * approach below requires that we uniformly do one or the other.
2165  *
2166  * Suppose we have a chain of N successive EMPTY arcs (where N can easily
2167  * approach the size of the NFA). All of the intermediate states must
2168  * have additional inarcs and outarcs, else they'd have been removed by
2169  * the steps above. Assuming their inarcs are mostly not empties, we will
2170  * add O(N^2) arcs to the NFA, since a non-EMPTY inarc leading to any one
2171  * state in the chain must be duplicated to lead to all its successor
2172  * states as well. So there is no hope of doing less than O(N^2) work;
2173  * however, we should endeavor to keep the big-O cost from being even
2174  * worse than that, which it can easily become without care. In
2175  * particular, suppose we were to copy all S1's inarcs forward to S2, and
2176  * then also to S3, and then later we consider pushing S2's inarcs forward
2177  * to S3. If we include the arcs already copied from S1 in that, we'd be
2178  * doing O(N^3) work. (The duplicate-arc elimination built into newarc()
2179  * and its cohorts would get rid of the extra arcs, but not without cost.)
2180  *
2181  * We can avoid this cost by treating only arcs that existed at the start
2182  * of this phase as candidates to be pushed forward. To identify those,
2183  * we remember the first inarc each state had to start with. We rely on
2184  * the fact that newarc() and friends put new arcs on the front of their
2185  * to-states' inchains, and that this phase never deletes arcs, so that
2186  * the original arcs must be the last arcs in their to-states' inchains.
2187  *
2188  * So the process here is that, for each state in the NFA, we gather up
2189  * all non-EMPTY inarcs of states that can reach the target state via
2190  * EMPTY arcs. We then sort, de-duplicate, and merge these arcs into the
2191  * target state's inchain. (We can safely use sort-merge for this as long
2192  * as we update each state's original-arcs pointer after we add arcs to
2193  * it; the sort step of mergeins probably changed the order of the old
2194  * arcs.)
2195  *
2196  * Another refinement worth making is that, because we only add non-EMPTY
2197  * arcs during this phase, and all added arcs have the same from-state as
2198  * the non-EMPTY arc they were cloned from, we know ahead of time that any
2199  * states having only EMPTY outarcs will be useless for lack of outarcs
2200  * after we drop the EMPTY arcs. (They cannot gain non-EMPTY outarcs if
2201  * they had none to start with.) So we need not bother to update the
2202  * inchains of such states at all.
2203  */
2204 
2205  /* Remember the states' first original inarcs */
2206  /* ... and while at it, count how many old inarcs there are altogether */
2207  inarcsorig = (struct arc **) MALLOC(nfa->nstates * sizeof(struct arc *));
2208  if (inarcsorig == NULL)
2209  {
2210  NERR(REG_ESPACE);
2211  return;
2212  }
2213  totalinarcs = 0;
2214  for (s = nfa->states; s != NULL; s = s->next)
2215  {
2216  inarcsorig[s->no] = s->ins;
2217  totalinarcs += s->nins;
2218  }
2219 
2220  /*
2221  * Create a workspace for accumulating the inarcs to be added to the
2222  * current target state. totalinarcs is probably a considerable
2223  * overestimate of the space needed, but the NFA is unlikely to be large
2224  * enough at this point to make it worth being smarter.
2225  */
2226  arcarray = (struct arc **) MALLOC(totalinarcs * sizeof(struct arc *));
2227  if (arcarray == NULL)
2228  {
2229  NERR(REG_ESPACE);
2230  FREE(inarcsorig);
2231  return;
2232  }
2233 
2234  /* And iterate over the target states */
2235  for (s = nfa->states; s != NULL && !NISERR(); s = s->next)
2236  {
2237  /* Ignore target states without non-EMPTY outarcs, per note above */
2238  if (!s->flag && !hasnonemptyout(s))
2239  continue;
2240 
2241  /* Find predecessor states and accumulate their original inarcs */
2242  arccount = 0;
2243  for (s2 = emptyreachable(nfa, s, s, inarcsorig); s2 != s; s2 = nexts)
2244  {
2245  /* Add s2's original inarcs to arcarray[], but ignore empties */
2246  for (a = inarcsorig[s2->no]; a != NULL; a = a->inchain)
2247  {
2248  if (a->type != EMPTY)
2249  arcarray[arccount++] = a;
2250  }
2251 
2252  /* Reset the tmp fields as we walk back */
2253  nexts = s2->tmp;
2254  s2->tmp = NULL;
2255  }
2256  s->tmp = NULL;
2257  assert(arccount <= totalinarcs);
2258 
2259  /* Remember how many original inarcs this state has */
2260  prevnins = s->nins;
2261 
2262  /* Add non-duplicate inarcs to target state */
2263  mergeins(nfa, s, arcarray, arccount);
2264 
2265  /* Now we must update the state's inarcsorig pointer */
2266  nskip = s->nins - prevnins;
2267  a = s->ins;
2268  while (nskip-- > 0)
2269  a = a->inchain;
2270  inarcsorig[s->no] = a;
2271  }
2272 
2273  FREE(arcarray);
2274  FREE(inarcsorig);
2275 
2276  if (NISERR())
2277  return;
2278 
2279  /*
2280  * Now remove all the EMPTY arcs, since we don't need them anymore.
2281  */
2282  for (s = nfa->states; s != NULL; s = s->next)
2283  {
2284  for (a = s->outs; a != NULL; a = nexta)
2285  {
2286  nexta = a->outchain;
2287  if (a->type == EMPTY)
2288  freearc(nfa, a);
2289  }
2290  }
2291 
2292  /*
2293  * And remove any states that have become useless. (This cleanup is not
2294  * very thorough, and would be even less so if we tried to combine it with
2295  * the previous step; but cleanup() will take care of anything we miss.)
2296  */
2297  for (s = nfa->states; s != NULL; s = nexts)
2298  {
2299  nexts = s->next;
2300  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
2301  dropstate(nfa, s);
2302  }
2303 
2304  if (f != NULL)
2305  dumpnfa(nfa, f);
2306 }
#define NISERR()
Definition: regc_nfa.c:39
int no
Definition: regguts.h:319
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
static void moveouts(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:1086
#define NERR(e)
Definition: regc_nfa.c:40
#define REG_ESPACE
Definition: regex.h:151
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
#define FREE(ptr)
Definition: cryptohash.c:37
static struct state * emptyreachable(struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
Definition: regc_nfa.c:2324
static void dumpnfa(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:3643
static void mergeins(struct nfa *nfa, struct state *s, struct arc **arcarray, int arccount)
Definition: regc_nfa.c:987
#define MALLOC(n)
Definition: regcustom.h:60
static int hasnonemptyout(struct state *s)
Definition: regc_nfa.c:583
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
int nstates
Definition: regguts.h:349
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
static void moveins(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:786
struct state * to
Definition: regguts.h:295
char * s2
Definition: regguts.h:317
#define EMPTY
Definition: regcomp.c:285
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:230
int nouts
Definition: regguts.h:323
struct arc * inchain
Definition: regguts.h:299
struct arc * ins
Definition: regguts.h:324

◆ freearc()

static void freearc ( struct nfa nfa,
struct arc victim 
)
static

Definition at line 426 of file regc_nfa.c.

References assert, nfa::cm, COLORED, nfa::freearcs, arc::from, arc::inchain, arc::inchainRev, state::ins, state::nins, state::nouts, arc::outchain, arc::outchainRev, state::outs, nfa::parent, arc::to, arc::type, and uncolorchain().

Referenced by breakconstraintloop(), deltraverse(), dropstate(), fixconstraintloops(), fixempties(), moveins(), moveouts(), pull(), pullback(), push(), pushfwd(), and removetraverse().

428 {
429  struct state *from = victim->from;
430  struct state *to = victim->to;
431  struct arc *predecessor;
432 
433  assert(victim->type != 0);
434 
435  /* take it off color chain if necessary */
436  if (COLORED(victim) && nfa->parent == NULL)
437  uncolorchain(nfa->cm, victim);
438 
439  /* take it off source's out-chain */
440  assert(from != NULL);
441  predecessor = victim->outchainRev;
442  if (predecessor == NULL)
443  {
444  assert(from->outs == victim);
445  from->outs = victim->outchain;
446  }
447  else
448  {
449  assert(predecessor->outchain == victim);
450  predecessor->outchain = victim->outchain;
451  }
452  if (victim->outchain != NULL)
453  {
454  assert(victim->outchain->outchainRev == victim);
455  victim->outchain->outchainRev = predecessor;
456  }
457  from->nouts--;
458 
459  /* take it off target's in-chain */
460  assert(to != NULL);
461  predecessor = victim->inchainRev;
462  if (predecessor == NULL)
463  {
464  assert(to->ins == victim);
465  to->ins = victim->inchain;
466  }
467  else
468  {
469  assert(predecessor->inchain == victim);
470  predecessor->inchain = victim->inchain;
471  }
472  if (victim->inchain != NULL)
473  {
474  assert(victim->inchain->inchainRev == victim);
475  victim->inchain->inchainRev = predecessor;
476  }
477  to->nins--;
478 
479  /* clean up and place on NFA's free list */
480  victim->type = 0;
481  victim->from = NULL; /* precautions... */
482  victim->to = NULL;
483  victim->inchain = NULL;
484  victim->inchainRev = NULL;
485  victim->outchain = NULL;
486  victim->outchainRev = NULL;
487  victim->freechain = nfa->freearcs;
488  nfa->freearcs = victim;
489 }
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
int nins
Definition: regguts.h:322
#define COLORED(a)
Definition: regcomp.c:307
struct arc * inchainRev
Definition: regguts.h:300
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct colormap * cm
Definition: regguts.h:358
struct arc * outs
Definition: regguts.h:325
struct state * to
Definition: regguts.h:295
Definition: regguts.h:317
struct arc * freearcs
Definition: regguts.h:353
int nouts
Definition: regguts.h:323
static void uncolorchain(struct colormap *cm, struct arc *a)
Definition: regc_color.c:1001
struct nfa * parent
Definition: regguts.h:365
struct arc * inchain
Definition: regguts.h:299
struct arc * outchainRev
Definition: regguts.h:297
struct arc * ins
Definition: regguts.h:324

◆ freecnfa()

static void freecnfa ( struct cnfa cnfa)
static

Definition at line 3630 of file regc_nfa.c.

References cnfa::arcs, assert, FREE, NULLCNFA, cnfa::states, cnfa::stflags, and ZAPCNFA.

3631 {
3632  assert(!NULLCNFA(*cnfa)); /* not empty already */
3633  FREE(cnfa->stflags);
3634  FREE(cnfa->states);
3635  FREE(cnfa->arcs);
3636  ZAPCNFA(*cnfa);
3637 }
#define NULLCNFA(cnfa)
Definition: regguts.h:431
#define FREE(ptr)
Definition: cryptohash.c:37
struct carc ** states
Definition: regguts.h:414
#define ZAPCNFA(cnfa)
Definition: regguts.h:429
#define assert(TEST)
Definition: imath.c:73
char * stflags
Definition: regguts.h:412
struct carc * arcs
Definition: regguts.h:416

◆ freenfa()

static void freenfa ( struct nfa nfa)
static

Definition at line 107 of file regc_nfa.c.

References ARCBATCHSIZE, FREE, nfa::lastab, nfa::lastsb, arcbatch::narcs, arcbatch::next, statebatch::next, statebatch::nstates, nfa::nstates, vars::spaceused, STATEBATCHSIZE, and nfa::v.

Referenced by newnfa().

108 {
109  struct statebatch *sb;
110  struct statebatch *sbnext;
111  struct arcbatch *ab;
112  struct arcbatch *abnext;
113 
114  for (sb = nfa->lastsb; sb != NULL; sb = sbnext)
115  {
116  sbnext = sb->next;
117  nfa->v->spaceused -= STATEBATCHSIZE(sb->nstates);
118  FREE(sb);
119  }
120  nfa->lastsb = NULL;
121  for (ab = nfa->lastab; ab != NULL; ab = abnext)
122  {
123  abnext = ab->next;
124  nfa->v->spaceused -= ARCBATCHSIZE(ab->narcs);
125  FREE(ab);
126  }
127  nfa->lastab = NULL;
128 
129  nfa->nstates = -1;
130  FREE(nfa);
131 }
#define FREE(ptr)
Definition: cryptohash.c:37
#define ARCBATCHSIZE(n)
Definition: regguts.h:312
size_t narcs
Definition: regguts.h:309
struct vars * v
Definition: regguts.h:364
struct arcbatch * next
Definition: regguts.h:308
struct statebatch * next
Definition: regguts.h:334
int nstates
Definition: regguts.h:349
struct arcbatch * lastab
Definition: regguts.h:355
size_t nstates
Definition: regguts.h:335
#define STATEBATCHSIZE(n)
Definition: regguts.h:338
size_t spaceused
Definition: regcomp.c:265
struct statebatch * lastsb
Definition: regguts.h:354

◆ freestate()

static void freestate ( struct nfa nfa,
struct state s 
)
static

Definition at line 246 of file regc_nfa.c.

References assert, state::flag, FREESTATE, nfa::freestates, state::next, state::nins, state::no, state::nouts, state::prev, nfa::slast, and nfa::states.

Referenced by breakconstraintloop(), deltraverse(), and dropstate().

248 {
249  assert(s != NULL);
250  assert(s->nins == 0 && s->nouts == 0);
251 
252  s->no = FREESTATE;
253  s->flag = 0;
254  if (s->next != NULL)
255  s->next->prev = s->prev;
256  else
257  {
258  assert(s == nfa->slast);
259  nfa->slast = s->prev;
260  }
261  if (s->prev != NULL)
262  s->prev->next = s->next;
263  else
264  {
265  assert(s == nfa->states);
266  nfa->states = s->next;
267  }
268  s->prev = NULL;
269  s->next = nfa->freestates; /* don't delete it, put it on the free list */
270  nfa->freestates = s;
271 }
int no
Definition: regguts.h:319
#define FREESTATE
Definition: regguts.h:320
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
char flag
Definition: regguts.h:321
struct state * freestates
Definition: regguts.h:352
struct state * prev
Definition: regguts.h:329
struct state * slast
Definition: regguts.h:351
int nouts
Definition: regguts.h:323

◆ hasconstraintout()

static int hasconstraintout ( struct state s)
static

Definition at line 2370 of file regc_nfa.c.

References isconstraintarc(), arc::outchain, and state::outs.

Referenced by clonesuccessorstates().

2371 {
2372  struct arc *a;
2373 
2374  for (a = s->outs; a != NULL; a = a->outchain)
2375  {
2376  if (isconstraintarc(a))
2377  return 1;
2378  }
2379  return 0;
2380 }
Definition: regguts.h:290
struct arc * outchain
Definition: regguts.h:296
static int isconstraintarc(struct arc *a)
Definition: regc_nfa.c:2352
struct arc * outs
Definition: regguts.h:325

◆ hasnonemptyout()

static int hasnonemptyout ( struct state s)
static

Definition at line 583 of file regc_nfa.c.

References EMPTY, arc::outchain, state::outs, and arc::type.

Referenced by fixempties().

584 {
585  struct arc *a;
586 
587  for (a = s->outs; a != NULL; a = a->outchain)
588  {
589  if (a->type != EMPTY)
590  return 1;
591  }
592  return 0;
593 }
int type
Definition: regguts.h:292
Definition: regguts.h:290
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
#define EMPTY
Definition: regcomp.c:285

◆ isconstraintarc()

static int isconstraintarc ( struct arc a)
inlinestatic

Definition at line 2352 of file regc_nfa.c.

References AHEAD, BEHIND, LACON, and arc::type.

Referenced by breakconstraintloop(), clonesuccessorstates(), findconstraintloop(), fixconstraintloops(), and hasconstraintout().

2353 {
2354  switch (a->type)
2355  {
2356  case '^':
2357  case '$':
2358  case BEHIND:
2359  case AHEAD:
2360  case LACON:
2361  return 1;
2362  }
2363  return 0;
2364 }
int type
Definition: regguts.h:292
#define BEHIND
Definition: regcomp.c:299
#define LACON
Definition: regcomp.c:297
#define AHEAD
Definition: regcomp.c:298

◆ markcanreach()

static void markcanreach ( struct nfa nfa,
struct state s,
struct state okay,
struct state mark 
)
static

Definition at line 3018 of file regc_nfa.c.

References arc::from, arc::inchain, state::ins, NERR, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by cleanup().

3022 {
3023  struct arc *a;
3024 
3025  /* Since this is recursive, it could be driven to stack overflow */
3026  if (STACK_TOO_DEEP(nfa->v->re))
3027  {
3028  NERR(REG_ETOOBIG);
3029  return;
3030  }
3031 
3032  if (s->tmp != okay)
3033  return;
3034  s->tmp = mark;
3035 
3036  for (a = s->ins; a != NULL; a = a->inchain)
3037  markcanreach(nfa, a->from, okay, mark);
3038 }
struct state * from
Definition: regguts.h:294
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
struct vars * v
Definition: regguts.h:364
static void markcanreach(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
Definition: regc_nfa.c:3018
regex_t * re
Definition: regcomp.c:239
struct state * tmp
Definition: regguts.h:326
#define REG_ETOOBIG
Definition: regex.h:157
struct arc * inchain
Definition: regguts.h:299
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520
struct arc * ins
Definition: regguts.h:324

◆ markreachable()

static void markreachable ( struct nfa nfa,
struct state s,
struct state okay,
struct state mark 
)
static

Definition at line 2992 of file regc_nfa.c.

References NERR, arc::outchain, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, and nfa::v.

Referenced by cleanup().

2996 {
2997  struct arc *a;
2998 
2999  /* Since this is recursive, it could be driven to stack overflow */
3000  if (STACK_TOO_DEEP(nfa->v->re))
3001  {
3002  NERR(REG_ETOOBIG);
3003  return;
3004  }
3005 
3006  if (s->tmp != okay)
3007  return;
3008  s->tmp = mark;
3009 
3010  for (a = s->outs; a != NULL; a = a->outchain)
3011  markreachable(nfa, a->to, okay, mark);
3012 }
static void markreachable(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
Definition: regc_nfa.c:2992
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
#define REG_ETOOBIG
Definition: regex.h:157
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ mergeins()

static void mergeins ( struct nfa nfa,
struct state s,
struct arc **  arcarray,
int  arccount 
)
static

Definition at line 987 of file regc_nfa.c.

References assert, CANCEL_REQUESTED, arc::co, createarc(), arc::from, i, arc::inchain, state::ins, NERR, NISERR, NOTREACHED, qsort, vars::re, REG_CANCEL, sortins(), sortins_cmp(), arc::type, and nfa::v.

Referenced by fixempties().

991 {
992  struct arc *na;
993  int i;
994  int j;
995 
996  if (arccount <= 0)
997  return;
998 
999  /*
1000  * Because we bypass newarc() in this code path, we'd better include a
1001  * cancel check.
1002  */
1003  if (CANCEL_REQUESTED(nfa->v->re))
1004  {
1005  NERR(REG_CANCEL);
1006  return;
1007  }
1008 
1009  /* Sort existing inarcs as well as proposed new ones */
1010  sortins(nfa, s);
1011  if (NISERR())
1012  return; /* might have failed to sort */
1013 
1014  qsort(arcarray, arccount, sizeof(struct arc *), sortins_cmp);
1015 
1016  /*
1017  * arcarray very likely includes dups, so we must eliminate them. (This
1018  * could be folded into the next loop, but it's not worth the trouble.)
1019  */
1020  j = 0;
1021  for (i = 1; i < arccount; i++)
1022  {
1023  switch (sortins_cmp(&arcarray[j], &arcarray[i]))
1024  {
1025  case -1:
1026  /* non-dup */
1027  arcarray[++j] = arcarray[i];
1028  break;
1029  case 0:
1030  /* dup */
1031  break;
1032  default:
1033  /* trouble */
1034  assert(NOTREACHED);
1035  }
1036  }
1037  arccount = j + 1;
1038 
1039  /*
1040  * Now merge into s' inchain. Note that createarc() will put new arcs
1041  * onto the front of s's chain, so it does not break our walk through the
1042  * sorted part of the chain.
1043  */
1044  i = 0;
1045  na = s->ins;
1046  while (i < arccount && na != NULL)
1047  {
1048  struct arc *a = arcarray[i];
1049 
1050  switch (sortins_cmp(&a, &na))
1051  {
1052  case -1:
1053  /* s does not have anything matching a */
1054  createarc(nfa, a->type, a->co, a->from, s);
1055  i++;
1056  break;
1057  case 0:
1058  /* match, advance in both lists */
1059  i++;
1060  na = na->inchain;
1061  break;
1062  case +1:
1063  /* advance only na; array might have a match later */
1064  na = na->inchain;
1065  break;
1066  default:
1067  assert(NOTREACHED);
1068  }
1069  }
1070  while (i < arccount)
1071  {
1072  /* s does not have anything matching a */
1073  struct arc *a = arcarray[i];
1074 
1075  createarc(nfa, a->type, a->co, a->from, s);
1076  i++;
1077  }
1078 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
#define NOTREACHED
Definition: regguts.h:91
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
#define assert(TEST)
Definition: imath.c:73
static int sortins_cmp(const void *a, const void *b)
Definition: regc_nfa.c:670
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:331
color co
Definition: regguts.h:293
struct arc * inchain
Definition: regguts.h:299
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
int i
#define qsort(a, b, c, d)
Definition: port.h:504
struct arc * ins
Definition: regguts.h:324
#define REG_CANCEL
Definition: regex.h:159
static void sortins(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:628

◆ moveins()

static void moveins ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 786 of file regc_nfa.c.

References assert, BULK_ARC_OP_USE_SORT, CANCEL_REQUESTED, changearctarget(), arc::co, cparc(), createarc(), freearc(), arc::from, arc::inchain, state::ins, NERR, state::nins, NISERR, NOTREACHED, vars::re, REG_CANCEL, sortins(), sortins_cmp(), arc::type, and nfa::v.

Referenced by fixempties(), and pull().

789 {
790  assert(oldState != newState);
791 
792  if (newState->nins == 0)
793  {
794  /* No need for de-duplication */
795  struct arc *a;
796 
797  while ((a = oldState->ins) != NULL)
798  {
799  createarc(nfa, a->type, a->co, a->from, newState);
800  freearc(nfa, a);
801  }
802  }
803  else if (!BULK_ARC_OP_USE_SORT(oldState->nins, newState->nins))
804  {
805  /* With not too many arcs, just do them one at a time */
806  struct arc *a;
807 
808  while ((a = oldState->ins) != NULL)
809  {
810  cparc(nfa, a, a->from, newState);
811  freearc(nfa, a);
812  }
813  }
814  else
815  {
816  /*
817  * With many arcs, use a sort-merge approach. Note changearctarget()
818  * will put the arc onto the front of newState's chain, so it does not
819  * break our walk through the sorted part of the chain.
820  */
821  struct arc *oa;
822  struct arc *na;
823 
824  /*
825  * Because we bypass newarc() in this code path, we'd better include a
826  * cancel check.
827  */
828  if (CANCEL_REQUESTED(nfa->v->re))
829  {
830  NERR(REG_CANCEL);
831  return;
832  }
833 
834  sortins(nfa, oldState);
835  sortins(nfa, newState);
836  if (NISERR())
837  return; /* might have failed to sort */
838  oa = oldState->ins;
839  na = newState->ins;
840  while (oa != NULL && na != NULL)
841  {
842  struct arc *a = oa;
843 
844  switch (sortins_cmp(&oa, &na))
845  {
846  case -1:
847  /* newState does not have anything matching oa */
848  oa = oa->inchain;
849 
850  /*
851  * Rather than doing createarc+freearc, we can just unlink
852  * and relink the existing arc struct.
853  */
854  changearctarget(a, newState);
855  break;
856  case 0:
857  /* match, advance in both lists */
858  oa = oa->inchain;
859  na = na->inchain;
860  /* ... and drop duplicate arc from oldState */
861  freearc(nfa, a);
862  break;
863  case +1:
864  /* advance only na; oa might have a match later */
865  na = na->inchain;
866  break;
867  default:
869  }
870  }
871  while (oa != NULL)
872  {
873  /* newState does not have anything matching oa */
874  struct arc *a = oa;
875 
876  oa = oa->inchain;
877  changearctarget(a, newState);
878  }
879  }
880 
881  assert(oldState->nins == 0);
882  assert(oldState->ins == NULL);
883 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
static void changearctarget(struct arc *a, struct state *newto)
Definition: regc_nfa.c:541
int nins
Definition: regguts.h:322
#define NOTREACHED
Definition: regguts.h:91
struct vars * v
Definition: regguts.h:364
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)
Definition: regc_nfa.c:766
regex_t * re
Definition: regcomp.c:239
#define assert(TEST)
Definition: imath.c:73
static int sortins_cmp(const void *a, const void *b)
Definition: regc_nfa.c:670
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:331
color co
Definition: regguts.h:293
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
struct arc * inchain
Definition: regguts.h:299
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
struct arc * ins
Definition: regguts.h:324
#define REG_CANCEL
Definition: regex.h:159
static void sortins(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:628

◆ moveouts()

static void moveouts ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 1086 of file regc_nfa.c.

References assert, BULK_ARC_OP_USE_SORT, CANCEL_REQUESTED, changearcsource(), arc::co, cparc(), createarc(), freearc(), NERR, NISERR, NOTREACHED, state::nouts, arc::outchain, state::outs, vars::re, REG_CANCEL, sortouts(), sortouts_cmp(), arc::to, arc::type, and nfa::v.

Referenced by fixempties(), and push().

1089 {
1090  assert(oldState != newState);
1091 
1092  if (newState->nouts == 0)
1093  {
1094  /* No need for de-duplication */
1095  struct arc *a;
1096 
1097  while ((a = oldState->outs) != NULL)
1098  {
1099  createarc(nfa, a->type, a->co, newState, a->to);
1100  freearc(nfa, a);
1101  }
1102  }
1103  else if (!BULK_ARC_OP_USE_SORT(oldState->nouts, newState->nouts))
1104  {
1105  /* With not too many arcs, just do them one at a time */
1106  struct arc *a;
1107 
1108  while ((a = oldState->outs) != NULL)
1109  {
1110  cparc(nfa, a, newState, a->to);
1111  freearc(nfa, a);
1112  }
1113  }
1114  else
1115  {
1116  /*
1117  * With many arcs, use a sort-merge approach. Note changearcsource()
1118  * will put the arc onto the front of newState's chain, so it does not
1119  * break our walk through the sorted part of the chain.
1120  */
1121  struct arc *oa;
1122  struct arc *na;
1123 
1124  /*
1125  * Because we bypass newarc() in this code path, we'd better include a
1126  * cancel check.
1127  */
1128  if (CANCEL_REQUESTED(nfa->v->re))
1129  {
1130  NERR(REG_CANCEL);
1131  return;
1132  }
1133 
1134  sortouts(nfa, oldState);
1135  sortouts(nfa, newState);
1136  if (NISERR())
1137  return; /* might have failed to sort */
1138  oa = oldState->outs;
1139  na = newState->outs;
1140  while (oa != NULL && na != NULL)
1141  {
1142  struct arc *a = oa;
1143 
1144  switch (sortouts_cmp(&oa, &na))
1145  {
1146  case -1:
1147  /* newState does not have anything matching oa */
1148  oa = oa->outchain;
1149 
1150  /*
1151  * Rather than doing createarc+freearc, we can just unlink
1152  * and relink the existing arc struct.
1153  */
1154  changearcsource(a, newState);
1155  break;
1156  case 0:
1157  /* match, advance in both lists */
1158  oa = oa->outchain;
1159  na = na->outchain;
1160  /* ... and drop duplicate arc from oldState */
1161  freearc(nfa, a);
1162  break;
1163  case +1:
1164  /* advance only na; oa might have a match later */
1165  na = na->outchain;
1166  break;
1167  default:
1168  assert(NOTREACHED);
1169  }
1170  }
1171  while (oa != NULL)
1172  {
1173  /* newState does not have anything matching oa */
1174  struct arc *a = oa;
1175 
1176  oa = oa->outchain;
1177  changearcsource(a, newState);
1178  }
1179  }
1180 
1181  assert(oldState->nouts == 0);
1182  assert(oldState->outs == NULL);
1183 }
#define NISERR()
Definition: regc_nfa.c:39
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
static void changearcsource(struct arc *a, struct state *newfrom)
Definition: regc_nfa.c:497
#define NOTREACHED
Definition: regguts.h:91
struct vars * v
Definition: regguts.h:364
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)
Definition: regc_nfa.c:766
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
struct state * to
Definition: regguts.h:295
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:331
color co
Definition: regguts.h:293
static void sortouts(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:695
static int sortouts_cmp(const void *a, const void *b)
Definition: regc_nfa.c:737
int nouts
Definition: regguts.h:323
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
#define REG_CANCEL
Definition: regex.h:159

◆ newarc()

static void newarc ( struct nfa nfa,
int  t,
color  co,
struct state from,
struct state to 
)
static

Definition at line 285 of file regc_nfa.c.

References assert, CANCEL_REQUESTED, arc::co, createarc(), arc::from, arc::inchain, state::ins, NERR, state::nins, state::nouts, arc::outchain, state::outs, vars::re, REG_CANCEL, arc::to, arc::type, and nfa::v.

Referenced by cloneouts(), colorcomplement(), cparc(), dupnfa(), newnfa(), okcolors(), pull(), pullback(), push(), pushfwd(), rainbow(), removetraverse(), subcolorcvec(), subcoloronechr(), and subcoloronerow().

290 {
291  struct arc *a;
292 
293  assert(from != NULL && to != NULL);
294 
295  /*
296  * This is a handy place to check for operation cancel during regex
297  * compilation, since no code path will go very long without making a new
298  * state or arc.
299  */
300  if (CANCEL_REQUESTED(nfa->v->re))
301  {
302  NERR(REG_CANCEL);
303  return;
304  }
305 
306  /* check for duplicate arc, using whichever chain is shorter */
307  if (from->nouts <= to->nins)
308  {
309  for (a = from->outs; a != NULL; a = a->outchain)
310  if (a->to == to && a->co == co && a->type == t)
311  return;
312  }
313  else
314  {
315  for (a = to->ins; a != NULL; a = a->inchain)
316  if (a->from == from && a->co == co && a->type == t)
317  return;
318  }
319 
320  /* no dup, so create the arc */
321  createarc(nfa, t, co, from, to);
322 }
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
int nins
Definition: regguts.h:322
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
struct state * to
Definition: regguts.h:295
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:331
color co
Definition: regguts.h:293
int nouts
Definition: regguts.h:323
struct arc * inchain
Definition: regguts.h:299
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
struct arc * ins
Definition: regguts.h:324
#define REG_CANCEL
Definition: regex.h:159

◆ newfstate()

static struct state* newfstate ( struct nfa nfa,
int  flag 
)
static

Definition at line 216 of file regc_nfa.c.

References state::flag, and newstate().

Referenced by newnfa().

217 {
218  struct state *s;
219 
220  s = newstate(nfa);
221  if (s != NULL)
222  s->flag = (char) flag;
223  return s;
224 }
char * flag(int b)
Definition: test-ctype.c:33
char flag
Definition: regguts.h:321
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
Definition: regguts.h:317

◆ newnfa()

static struct nfa* newnfa ( struct vars v,
struct colormap cm,
struct nfa parent 
)
static

Definition at line 47 of file regc_nfa.c.

References nfa::bos, nfa::cm, COLORLESS, nfa::eos, ERR, nfa::final, nfa::flags, nfa::freearcs, freenfa(), nfa::freestates, nfa::init, ISERR, nfa::lastab, nfa::lastabused, nfa::lastsb, nfa::lastsbused, MALLOC, nfa::maxmatchall, nfa::minmatchall, newarc(), newfstate(), newstate(), nfa::nstates, nfa::parent, PLAIN, nfa::post, nfa::pre, rainbow(), REG_ESPACE, nfa::slast, nfa::states, and nfa::v.

50 {
51  struct nfa *nfa;
52 
53  nfa = (struct nfa *) MALLOC(sizeof(struct nfa));
54  if (nfa == NULL)
55  {
56  ERR(REG_ESPACE);
57  return NULL;
58  }
59 
60  /* Make the NFA minimally valid, so freenfa() will behave sanely */
61  nfa->states = NULL;
62  nfa->slast = NULL;
63  nfa->freestates = NULL;
64  nfa->freearcs = NULL;
65  nfa->lastsb = NULL;
66  nfa->lastab = NULL;
67  nfa->lastsbused = 0;
68  nfa->lastabused = 0;
69  nfa->nstates = 0;
70  nfa->cm = cm;
71  nfa->v = v;
72  nfa->bos[0] = nfa->bos[1] = COLORLESS;
73  nfa->eos[0] = nfa->eos[1] = COLORLESS;
74  nfa->flags = 0;
75  nfa->minmatchall = nfa->maxmatchall = -1;
76  nfa->parent = parent; /* Precedes newfstate so parent is valid. */
77 
78  /* Create required infrastructure */
79  nfa->post = newfstate(nfa, '@'); /* number 0 */
80  nfa->pre = newfstate(nfa, '>'); /* number 1 */
81  nfa->init = newstate(nfa); /* may become invalid later */
82  nfa->final = newstate(nfa);
83  if (ISERR())
84  {
85  freenfa(nfa);
86  return NULL;
87  }
88  rainbow(nfa, nfa->cm, PLAIN, COLORLESS, nfa->pre, nfa->init);
89  newarc(nfa, '^', 1, nfa->pre, nfa->init);
90  newarc(nfa, '^', 0, nfa->pre, nfa->init);
91  rainbow(nfa, nfa->cm, PLAIN, COLORLESS, nfa->final, nfa->post);
92  newarc(nfa, '$', 1, nfa->final, nfa->post);
93  newarc(nfa, '$', 0, nfa->final, nfa->post);
94 
95  if (ISERR())
96  {
97  freenfa(nfa);
98  return NULL;
99  }
100  return nfa;
101 }
size_t lastabused
Definition: regguts.h:357
size_t lastsbused
Definition: regguts.h:356
#define ERR
Definition: _int.h:161
struct state * final
Definition: regguts.h:347
#define REG_ESPACE
Definition: regex.h:151
struct state * states
Definition: regguts.h:350
color eos[2]
Definition: regguts.h:360
int flags
Definition: regguts.h:361
struct state * post
Definition: regguts.h:348
Definition: regguts.h:343
#define ISERR()
Definition: regcomp.c:273
#define MALLOC(n)
Definition: regcustom.h:60
int maxmatchall
Definition: regguts.h:363
static struct state * newfstate(struct nfa *nfa, int flag)
Definition: regc_nfa.c:216
struct vars * v
Definition: regguts.h:364
color bos[2]
Definition: regguts.h:359
struct colormap * cm
Definition: regguts.h:358
int nstates
Definition: regguts.h:349
static void freenfa(struct nfa *nfa)
Definition: regc_nfa.c:107
static void rainbow(struct nfa *nfa, struct colormap *cm, int type, color but, struct state *from, struct state *to)
Definition: regc_color.c:1031
struct state * freestates
Definition: regguts.h:352
int minmatchall
Definition: regguts.h:362
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
#define PLAIN
Definition: regcomp.c:287
struct arcbatch * lastab
Definition: regguts.h:355
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
struct arc * freearcs
Definition: regguts.h:353
struct state * slast
Definition: regguts.h:351
struct nfa * parent
Definition: regguts.h:365
struct state * init
Definition: regguts.h:346
#define COLORLESS
Definition: regguts.h:153
struct state * pre
Definition: regguts.h:345
struct statebatch * lastsb
Definition: regguts.h:354

◆ newstate()

static struct state* newstate ( struct nfa nfa)
static

Definition at line 137 of file regc_nfa.c.

References assert, CANCEL_REQUESTED, FIRSTSBSIZE, state::flag, nfa::freestates, state::ins, nfa::lastsb, nfa::lastsbused, MALLOC, MAXSBSIZE, NERR, state::next, statebatch::next, state::nins, state::no, state::nouts, statebatch::nstates, nfa::nstates, state::outs, state::prev, vars::re, REG_CANCEL, REG_ESPACE, REG_ETOOBIG, REG_MAX_COMPILE_SPACE, statebatch::s, nfa::slast, vars::spaceused, STATEBATCHSIZE, nfa::states, state::tmp, and nfa::v.

Referenced by breakconstraintloop(), clonesuccessorstates(), duptraverse(), newfstate(), newnfa(), pull(), and push().

138 {
139  struct state *s;
140 
141  /*
142  * This is a handy place to check for operation cancel during regex
143  * compilation, since no code path will go very long without making a new
144  * state or arc.
145  */
146  if (CANCEL_REQUESTED(nfa->v->re))
147  {
148  NERR(REG_CANCEL);
149  return NULL;
150  }
151 
152  /* first, recycle anything that's on the freelist */
153  if (nfa->freestates != NULL)
154  {
155  s = nfa->freestates;
156  nfa->freestates = s->next;
157  }
158  /* otherwise, is there anything left in the last statebatch? */
159  else if (nfa->lastsb != NULL && nfa->lastsbused < nfa->lastsb->nstates)
160  {
161  s = &nfa->lastsb->s[nfa->lastsbused++];
162  }
163  /* otherwise, need to allocate a new statebatch */
164  else
165  {
166  struct statebatch *newSb;
167  size_t nstates;
168 
169  if (nfa->v->spaceused >= REG_MAX_COMPILE_SPACE)
170  {
171  NERR(REG_ETOOBIG);
172  return NULL;
173  }
174  nstates = (nfa->lastsb != NULL) ? nfa->lastsb->nstates * 2 : FIRSTSBSIZE;
175  if (nstates > MAXSBSIZE)
176  nstates = MAXSBSIZE;
177  newSb = (struct statebatch *) MALLOC(STATEBATCHSIZE(nstates));
178  if (newSb == NULL)
179  {
180  NERR(REG_ESPACE);
181  return NULL;
182  }
183  nfa->v->spaceused += STATEBATCHSIZE(nstates);
184  newSb->nstates = nstates;
185  newSb->next = nfa->lastsb;
186  nfa->lastsb = newSb;
187  nfa->lastsbused = 1;
188  s = &newSb->s[0];
189  }
190 
191  assert(nfa->nstates >= 0);
192  s->no = nfa->nstates++;
193  s->flag = 0;
194  if (nfa->states == NULL)
195  nfa->states = s;
196  s->nins = 0;
197  s->ins = NULL;
198  s->nouts = 0;
199  s->outs = NULL;
200  s->tmp = NULL;
201  s->next = NULL;
202  if (nfa->slast != NULL)
203  {
204  assert(nfa->slast->next == NULL);
205  nfa->slast->next = s;
206  }
207  s->prev = nfa->slast;
208  nfa->slast = s;
209  return s;
210 }
#define MAXSBSIZE
Definition: regguts.h:341
int no
Definition: regguts.h:319
size_t lastsbused
Definition: regguts.h:356
#define NERR(e)
Definition: regc_nfa.c:40
struct state s[FLEXIBLE_ARRAY_MEMBER]
Definition: regguts.h:336
#define REG_ESPACE
Definition: regex.h:151
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
#define MALLOC(n)
Definition: regcustom.h:60
#define REG_MAX_COMPILE_SPACE
Definition: regguts.h:444
struct vars * v
Definition: regguts.h:364
regex_t * re
Definition: regcomp.c:239
struct statebatch * next
Definition: regguts.h:334
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
int nstates
Definition: regguts.h:349
struct arc * outs
Definition: regguts.h:325
#define FIRSTSBSIZE
Definition: regguts.h:340
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
struct state * freestates
Definition: regguts.h:352
Definition: regguts.h:317
#define REG_ETOOBIG
Definition: regex.h:157
size_t nstates
Definition: regguts.h:335
struct state * prev
Definition: regguts.h:329
struct state * slast
Definition: regguts.h:351
int nouts
Definition: regguts.h:323
#define CANCEL_REQUESTED(re)
Definition: regguts.h:517
#define STATEBATCHSIZE(n)
Definition: regguts.h:338
size_t spaceused
Definition: regcomp.c:265
struct arc * ins
Definition: regguts.h:324
#define REG_CANCEL
Definition: regex.h:159
struct statebatch * lastsb
Definition: regguts.h:354

◆ optimize()

static long optimize ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 1621 of file regc_nfa.c.

References analyze(), cleanup(), dumpnfa(), fixconstraintloops(), fixempties(), fprintf, pullback(), pushfwd(), and verbose.

1623 {
1624 #ifdef REG_DEBUG
1625  int verbose = (f != NULL) ? 1 : 0;
1626 
1627  if (verbose)
1628  fprintf(f, "\ninitial cleanup:\n");
1629 #endif
1630  cleanup(nfa); /* may simplify situation */
1631 #ifdef REG_DEBUG
1632  if (verbose)
1633  dumpnfa(nfa, f);
1634  if (verbose)
1635  fprintf(f, "\nempties:\n");
1636 #endif
1637  fixempties(nfa, f); /* get rid of EMPTY arcs */
1638 #ifdef REG_DEBUG
1639  if (verbose)
1640  fprintf(f, "\nconstraints:\n");
1641 #endif
1642  fixconstraintloops(nfa, f); /* get rid of constraint loops */
1643  pullback(nfa, f); /* pull back constraints backward */
1644  pushfwd(nfa, f); /* push fwd constraints forward */
1645 #ifdef REG_DEBUG
1646  if (verbose)
1647  fprintf(f, "\nfinal cleanup:\n");
1648 #endif
1649  cleanup(nfa); /* final tidying */
1650 #ifdef REG_DEBUG
1651  if (verbose)
1652  dumpnfa(nfa, f);
1653 #endif
1654  return analyze(nfa); /* and analysis */
1655 }
static void fixconstraintloops(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:2391
static void fixempties(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:2097
static void pullback(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:1661
static void dumpnfa(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:3643
#define fprintf
Definition: port.h:220
static void pushfwd(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:1832
static int verbose
static void cleanup(struct nfa *nfa)
Definition: regc_nfa.c:2957
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:3044

◆ pull()

static int pull ( struct nfa nfa,
struct arc con,
struct state **  intermediates 
)
static

Definition at line 1741 of file regc_nfa.c.

References assert, arc::co, combine(), COMPATIBLE, copyins(), cparc(), state::flag, freearc(), arc::from, arc::inchain, INCOMPATIBLE, state::ins, moveins(), newarc(), newstate(), state::nins, NISERR, NOTREACHED, state::nouts, state::outs, REPLACEARC, SATISFIED, state::tmp, arc::to, and arc::type.

Referenced by pullback().

1744 {
1745  struct state *from = con->from;
1746  struct state *to = con->to;
1747  struct arc *a;
1748  struct arc *nexta;
1749  struct state *s;
1750 
1751  assert(from != to); /* should have gotten rid of this earlier */
1752  if (from->flag) /* can't pull back beyond start */
1753  return 0;
1754  if (from->nins == 0)
1755  { /* unreachable */
1756  freearc(nfa, con);
1757  return 1;
1758  }
1759 
1760  /*
1761  * First, clone from state if necessary to avoid other outarcs. This may
1762  * seem wasteful, but it simplifies the logic, and we'll get rid of the
1763  * clone state again at the bottom.
1764  */
1765  if (from->nouts > 1)
1766  {
1767  s = newstate(nfa);
1768  if (NISERR())
1769  return 0;
1770  copyins(nfa, from, s); /* duplicate inarcs */
1771  cparc(nfa, con, s, to); /* move constraint arc */
1772  freearc(nfa, con);
1773  if (NISERR())
1774  return 0;
1775  from = s;
1776  con = from->outs;
1777  }
1778  assert(from->nouts == 1);
1779 
1780  /* propagate the constraint into the from state's inarcs */
1781  for (a = from->ins; a != NULL && !NISERR(); a = nexta)
1782  {
1783  nexta = a->inchain;
1784  switch (combine(nfa, con, a))
1785  {
1786  case INCOMPATIBLE: /* destroy the arc */
1787  freearc(nfa, a);
1788  break;
1789  case SATISFIED: /* no action needed */
1790  break;
1791  case COMPATIBLE: /* swap the two arcs, more or less */
1792  /* need an intermediate state, but might have one already */
1793  for (s = *intermediates; s != NULL; s = s->tmp)
1794  {
1795  assert(s->nins > 0 && s->nouts > 0);
1796  if (s->ins->from == a->from && s->outs->to == to)
1797  break;
1798  }
1799  if (s == NULL)
1800  {
1801  s = newstate(nfa);
1802  if (NISERR())
1803  return 0;
1804  s->tmp = *intermediates;
1805  *intermediates = s;
1806  }
1807  cparc(nfa, con, a->from, s);
1808  cparc(nfa, a, s, to);
1809  freearc(nfa, a);
1810  break;
1811  case REPLACEARC: /* replace arc's color */
1812  newarc(nfa, a->type, con->co, a->from, to);
1813  freearc(nfa, a);
1814  break;
1815  default:
1816  assert(NOTREACHED);
1817  break;
1818  }
1819  }
1820 
1821  /* remaining inarcs, if any, incorporate the constraint */
1822  moveins(nfa, from, to);
1823  freearc(nfa, con);
1824  /* from state is now useless, but we leave it to pullback() to clean up */
1825  return 1;
1826 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
int nins
Definition: regguts.h:322
static void copyins(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:894
#define NOTREACHED
Definition: regguts.h:91
#define SATISFIED
Definition: regcomp.c:165
#define REPLACEARC
Definition: regcomp.c:167
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
static void moveins(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:786
struct state * to
Definition: regguts.h:295
#define COMPATIBLE
Definition: regcomp.c:166
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
color co
Definition: regguts.h:293
Definition: regguts.h:317
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
int nouts
Definition: regguts.h:323
static int combine(struct nfa *nfa, struct arc *con, struct arc *a)
Definition: regc_nfa.c:2008
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
struct arc * inchain
Definition: regguts.h:299
#define INCOMPATIBLE
Definition: regcomp.c:164
struct arc * ins
Definition: regguts.h:324

◆ pullback()

static void pullback ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 1661 of file regc_nfa.c.

References assert, BEHIND, nfa::bos, arc::co, dropstate(), dumpnfa(), state::flag, freearc(), arc::from, newarc(), state::next, state::nins, NISERR, state::nouts, arc::outchain, state::outs, PLAIN, nfa::pre, progress, pull(), nfa::states, state::tmp, arc::to, and arc::type.

Referenced by optimize().

1663 {
1664  struct state *s;
1665  struct state *nexts;
1666  struct arc *a;
1667  struct arc *nexta;
1668  struct state *intermediates;
1669  int progress;
1670 
1671  /* find and pull until there are no more */
1672  do
1673  {
1674  progress = 0;
1675  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
1676  {
1677  nexts = s->next;
1678  intermediates = NULL;
1679  for (a = s->outs; a != NULL && !NISERR(); a = nexta)
1680  {
1681  nexta = a->outchain;
1682  if (a->type == '^' || a->type == BEHIND)
1683  if (pull(nfa, a, &intermediates))
1684  progress = 1;
1685  }
1686  /* clear tmp fields of intermediate states created here */
1687  while (intermediates != NULL)
1688  {
1689  struct state *ns = intermediates->tmp;
1690 
1691  intermediates->tmp = NULL;
1692  intermediates = ns;
1693  }
1694  /* if s is now useless, get rid of it */
1695  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
1696  dropstate(nfa, s);
1697  }
1698  if (progress && f != NULL)
1699  dumpnfa(nfa, f);
1700  } while (progress && !NISERR());
1701  if (NISERR())
1702  return;
1703 
1704  /*
1705  * Any ^ constraints we were able to pull to the start state can now be
1706  * replaced by PLAIN arcs referencing the BOS or BOL colors. There should
1707  * be no other ^ or BEHIND arcs left in the NFA, though we do not check
1708  * that here (compact() will fail if so).
1709  */
1710  for (a = nfa->pre->outs; a != NULL; a = nexta)
1711  {
1712  nexta = a->outchain;
1713  if (a->type == '^')
1714  {
1715  assert(a->co == 0 || a->co == 1);
1716  newarc(nfa, PLAIN, nfa->bos[a->co], a->from, a->to);
1717  freearc(nfa, a);
1718  }
1719  }
1720 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
static void dumpnfa(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:3643
#define BEHIND
Definition: regcomp.c:299
struct arc * outchain
Definition: regguts.h:296
color bos[2]
Definition: regguts.h:359
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
int progress
Definition: pgbench.c:272
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
Definition: regguts.h:317
static int pull(struct nfa *nfa, struct arc *con, struct state **intermediates)
Definition: regc_nfa.c:1741
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:230
int nouts
Definition: regguts.h:323
struct state * pre
Definition: regguts.h:345

◆ push()

static int push ( struct nfa nfa,
struct arc con,
struct state **  intermediates 
)
static

Definition at line 1912 of file regc_nfa.c.

References assert, arc::co, combine(), COMPATIBLE, copyouts(), cparc(), state::flag, freearc(), arc::from, INCOMPATIBLE, state::ins, moveouts(), newarc(), newstate(), state::nins, NISERR, NOTREACHED, state::nouts, arc::outchain, state::outs, REPLACEARC, SATISFIED, state::tmp, arc::to, and arc::type.

Referenced by pushfwd().

1915 {
1916  struct state *from = con->from;
1917  struct state *to = con->to;
1918  struct arc *a;
1919  struct arc *nexta;
1920  struct state *s;
1921 
1922  assert(to != from); /* should have gotten rid of this earlier */
1923  if (to->flag) /* can't push forward beyond end */
1924  return 0;
1925  if (to->nouts == 0)
1926  { /* dead end */
1927  freearc(nfa, con);
1928  return 1;
1929  }
1930 
1931  /*
1932  * First, clone to state if necessary to avoid other inarcs. This may
1933  * seem wasteful, but it simplifies the logic, and we'll get rid of the
1934  * clone state again at the bottom.
1935  */
1936  if (to->nins > 1)
1937  {
1938  s = newstate(nfa);
1939  if (NISERR())
1940  return 0;
1941  copyouts(nfa, to, s); /* duplicate outarcs */
1942  cparc(nfa, con, from, s); /* move constraint arc */
1943  freearc(nfa, con);
1944  if (NISERR())
1945  return 0;
1946  to = s;
1947  con = to->ins;
1948  }
1949  assert(to->nins == 1);
1950 
1951  /* propagate the constraint into the to state's outarcs */
1952  for (a = to->outs; a != NULL && !NISERR(); a = nexta)
1953  {
1954  nexta = a->outchain;
1955  switch (combine(nfa, con, a))
1956  {
1957  case INCOMPATIBLE: /* destroy the arc */
1958  freearc(nfa, a);
1959  break;
1960  case SATISFIED: /* no action needed */
1961  break;
1962  case COMPATIBLE: /* swap the two arcs, more or less */
1963  /* need an intermediate state, but might have one already */
1964  for (s = *intermediates; s != NULL; s = s->tmp)
1965  {
1966  assert(s->nins > 0 && s->nouts > 0);
1967  if (s->ins->from == from && s->outs->to == a->to)
1968  break;
1969  }
1970  if (s == NULL)
1971  {
1972  s = newstate(nfa);
1973  if (NISERR())
1974  return 0;
1975  s->tmp = *intermediates;
1976  *intermediates = s;
1977  }
1978  cparc(nfa, con, s, a->to);
1979  cparc(nfa, a, from, s);
1980  freearc(nfa, a);
1981  break;
1982  case REPLACEARC: /* replace arc's color */
1983  newarc(nfa, a->type, con->co, from, a->to);
1984  freearc(nfa, a);
1985  break;
1986  default:
1987  assert(NOTREACHED);
1988  break;
1989  }
1990  }
1991 
1992  /* remaining outarcs, if any, incorporate the constraint */
1993  moveouts(nfa, to, from);
1994  freearc(nfa, con);
1995  /* to state is now useless, but we leave it to pushfwd() to clean up */
1996  return 1;
1997 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
static void moveouts(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:1086
Definition: regguts.h:290
int nins
Definition: regguts.h:322
#define NOTREACHED
Definition: regguts.h:91
#define SATISFIED
Definition: regcomp.c:165
static void copyouts(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:1191
#define REPLACEARC
Definition: regcomp.c:167
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
#define COMPATIBLE
Definition: regcomp.c:166
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
color co
Definition: regguts.h:293
Definition: regguts.h:317
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
int nouts
Definition: regguts.h:323
static int combine(struct nfa *nfa, struct arc *con, struct arc *a)
Definition: regc_nfa.c:2008
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:616
#define INCOMPATIBLE
Definition: regcomp.c:164
struct arc * ins
Definition: regguts.h:324

◆ pushfwd()

static void pushfwd ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 1832 of file regc_nfa.c.

References AHEAD, assert, arc::co, dropstate(), dumpnfa(), nfa::eos, state::flag, freearc(), arc::from, arc::inchain, state::ins, newarc(), state::next, state::nins, NISERR, state::nouts, PLAIN, nfa::post, progress, push(), nfa::states, state::tmp, arc::to, and arc::type.

Referenced by optimize().

1834 {
1835  struct state *s;
1836  struct state *nexts;
1837  struct arc *a;
1838  struct arc *nexta;
1839  struct state *intermediates;
1840  int progress;
1841 
1842  /* find and push until there are no more */
1843  do
1844  {
1845  progress = 0;
1846  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
1847  {
1848  nexts = s->next;
1849  intermediates = NULL;
1850  for (a = s->ins; a != NULL && !NISERR(); a = nexta)
1851  {
1852  nexta = a->inchain;
1853  if (a->type == '$' || a->type == AHEAD)
1854  if (push(nfa, a, &intermediates))
1855  progress = 1;
1856  }
1857  /* clear tmp fields of intermediate states created here */
1858  while (intermediates != NULL)
1859  {
1860  struct state *ns = intermediates->tmp;
1861 
1862  intermediates->tmp = NULL;
1863  intermediates = ns;
1864  }
1865  /* if s is now useless, get rid of it */
1866  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
1867  dropstate(nfa, s);
1868  }
1869  if (progress && f != NULL)
1870  dumpnfa(nfa, f);
1871  } while (progress && !NISERR());
1872  if (NISERR())
1873  return;
1874 
1875  /*
1876  * Any $ constraints we were able to push to the post state can now be
1877  * replaced by PLAIN arcs referencing the EOS or EOL colors. There should
1878  * be no other $ or AHEAD arcs left in the NFA, though we do not check
1879  * that here (compact() will fail if so).
1880  */
1881  for (a = nfa->post->ins; a != NULL; a = nexta)
1882  {
1883  nexta = a->inchain;
1884  if (a->type == '$')
1885  {
1886  assert(a->co == 0 || a->co == 1);
1887  newarc(nfa, PLAIN, nfa->eos[a->co], a->from, a->to);
1888  freearc(nfa, a);
1889  }
1890  }
1891 }
#define NISERR()
Definition: regc_nfa.c:39
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
static int push(struct nfa *nfa, struct arc *con, struct state **intermediates)
Definition: regc_nfa.c:1912
Definition: regguts.h:290
struct state * states
Definition: regguts.h:350
int nins
Definition: regguts.h:322
color eos[2]
Definition: regguts.h:360
struct state * post
Definition: regguts.h:348
static void dumpnfa(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:3643
#define assert(TEST)
Definition: imath.c:73
struct state * next
Definition: regguts.h:327
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
char flag
Definition: regguts.h:321
struct state * tmp
Definition: regguts.h:326
int progress
Definition: pgbench.c:272
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
Definition: regguts.h:317
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:230
int nouts
Definition: regguts.h:323
struct arc * inchain
Definition: regguts.h:299
#define AHEAD
Definition: regcomp.c:298
struct arc * ins
Definition: regguts.h:324

◆ removeconstraints()

static void removeconstraints ( struct nfa nfa,
struct state start,
struct state stop 
)
static

Definition at line 1447 of file regc_nfa.c.

References cleartraverse(), removetraverse(), and state::tmp.

1450 {
1451  if (start == stop)
1452  return;
1453 
1454  stop->tmp = stop;
1455  removetraverse(nfa, start);
1456  /* done, except for clearing out the tmp pointers */
1457 
1458  stop->tmp = NULL;
1459  cleartraverse(nfa, start);
1460 }
static void removetraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1466
static void cleartraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1515
struct state * tmp
Definition: regguts.h:326

◆ removetraverse()

static void removetraverse ( struct nfa nfa,
struct state s 
)
static

Definition at line 1466 of file regc_nfa.c.

References AHEAD, BEHIND, EMPTY, freearc(), LACON, NERR, newarc(), NISERR, arc::outchain, state::outs, PLAIN, vars::re, REG_ASSERT, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::to, arc::type, and nfa::v.

Referenced by removeconstraints().

1468 {
1469  struct arc *a;
1470  struct arc *oa;
1471 
1472  /* Since this is recursive, it could be driven to stack overflow */
1473  if (STACK_TOO_DEEP(nfa->v->re))
1474  {
1475  NERR(REG_ETOOBIG);
1476  return;
1477  }
1478 
1479  if (s->tmp != NULL)
1480  return; /* already done */
1481 
1482  s->tmp = s;
1483  for (a = s->outs; a != NULL && !NISERR(); a = oa)
1484  {
1485  removetraverse(nfa, a->to);
1486  if (NISERR())
1487  break;
1488  oa = a->outchain;
1489  switch (a->type)
1490  {
1491  case PLAIN:
1492  case EMPTY:
1493  /* nothing to do */
1494  break;
1495  case AHEAD:
1496  case BEHIND:
1497  case '^':
1498  case '$':
1499  case LACON:
1500  /* replace it */
1501  newarc(nfa, EMPTY, 0, s, a->to);
1502  freearc(nfa, a);
1503  break;
1504  default:
1505  NERR(REG_ASSERT);
1506  break;
1507  }
1508  }
1509 }
#define NISERR()
Definition: regc_nfa.c:39
int type
Definition: regguts.h:292
#define NERR(e)
Definition: regc_nfa.c:40
Definition: regguts.h:290
static void removetraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1466
#define BEHIND
Definition: regcomp.c:299
struct vars * v
Definition: regguts.h:364
#define LACON
Definition: regcomp.c:297
regex_t * re
Definition: regcomp.c:239
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:426
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
#define REG_ASSERT
Definition: regex.h:153
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:285
#define REG_ETOOBIG
Definition: regex.h:157
#define EMPTY
Definition: regcomp.c:285
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520
#define AHEAD
Definition: regcomp.c:298

◆ single_color_transition()

static struct state* single_color_transition ( struct state s1,
struct state s2 
)
static

Definition at line 1552 of file regc_nfa.c.

References EMPTY, arc::from, state::ins, state::nins, state::nouts, arc::outchain, state::outs, PLAIN, s1, arc::to, and arc::type.

1553 {
1554  struct arc *a;
1555 
1556  /* Ignore leading EMPTY arc, if any */
1557  if (s1->nouts == 1 && s1->outs->type == EMPTY)
1558  s1 = s1->outs->to;
1559  /* Likewise for any trailing EMPTY arc */
1560  if (s2->nins == 1 && s2->ins->type == EMPTY)
1561  s2 = s2->ins->from;
1562  /* Perhaps we could have a single-state loop in between, if so reject */
1563  if (s1 == s2)
1564  return NULL;
1565  /* s1 must have at least one outarc... */
1566  if (s1->outs == NULL)
1567  return NULL;
1568  /* ... and they must all be PLAIN arcs to s2 */
1569  for (a = s1->outs; a != NULL; a = a->outchain)
1570  {
1571  if (a->type != PLAIN || a->to != s2)
1572  return NULL;
1573  }
1574  /* OK, return s1 as the possessor of the relevant outarcs */
1575  return s1;
1576 }
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
int nins
Definition: regguts.h:322
char * s1
struct arc * outchain
Definition: regguts.h:296
struct arc * outs
Definition: regguts.h:325
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
#define EMPTY
Definition: regcomp.c:285
int nouts
Definition: regguts.h:323
struct arc * ins
Definition: regguts.h:324

◆ sortins()

static void sortins ( struct nfa nfa,
struct state s 
)
static

Definition at line 628 of file regc_nfa.c.

References assert, FREE, i, arc::inchain, arc::inchainRev, state::ins, MALLOC, NERR, state::nins, qsort, REG_ESPACE, and sortins_cmp().

Referenced by copyins(), mergeins(), and moveins().

630 {
631  struct arc **sortarray;
632  struct arc *a;
633  int n = s->nins;
634  int i;
635 
636  if (n <= 1)
637  return; /* nothing to do */
638  /* make an array of arc pointers ... */
639  sortarray = (struct arc **) MALLOC(n * sizeof(struct arc *));
640  if (sortarray == NULL)
641  {
642  NERR(REG_ESPACE);
643  return;
644  }
645  i = 0;
646  for (a = s->ins; a != NULL; a = a->inchain)
647  sortarray[i++] = a;
648  assert(i == n);
649  /* ... sort the array */
650  qsort(sortarray, n, sizeof(struct arc *), sortins_cmp);
651  /* ... and rebuild arc list in order */
652  /* it seems worth special-casing first and last items to simplify loop */
653  a = sortarray[0];
654  s->ins = a;
655  a->inchain = sortarray[1];
656  a->inchainRev = NULL;
657  for (i = 1; i < n - 1; i++)
658  {
659  a = sortarray[i];
660  a->inchain = sortarray[i + 1];
661  a->inchainRev = sortarray[i - 1];
662  }
663  a = sortarray[i];
664  a->inchain = NULL;
665  a->inchainRev = sortarray[i - 1];
666  FREE(sortarray);
667 }
#define NERR(e)
Definition: regc_nfa.c:40
#define REG_ESPACE
Definition: regex.h:151
Definition: regguts.h:290
int nins
Definition: regguts.h:322
#define FREE(ptr)
Definition: cryptohash.c:37
#define MALLOC(n)
Definition: regcustom.h:60
struct arc * inchainRev
Definition: regguts.h:300
#define assert(TEST)
Definition: imath.c:73
static int sortins_cmp(const void *a, const void *b)
Definition: regc_nfa.c:670
struct arc * inchain
Definition: regguts.h:299
int i
#define qsort(a, b, c, d)
Definition: port.h:504
struct arc * ins
Definition: regguts.h:324

◆ sortins_cmp()

static int sortins_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 670 of file regc_nfa.c.

References arc::co, arc::from, state::no, and arc::type.

Referenced by copyins(), mergeins(), moveins(), and sortins().

671 {
672  const struct arc *aa = *((const struct arc *const *) a);
673  const struct arc *bb = *((const struct arc *const *) b);
674 
675  /* we check the fields in the order they are most likely to be different */
676  if (aa->from->no < bb->from->no)
677  return -1;
678  if (aa->from->no > bb->from->no)
679  return 1;
680  if (aa->co < bb->co)
681  return -1;
682  if (aa->co > bb->co)
683  return 1;
684  if (aa->type < bb->type)
685  return -1;
686  if (aa->type > bb->type)
687  return 1;
688  return 0;
689 }
int no
Definition: regguts.h:319
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
color co
Definition: regguts.h:293

◆ sortouts()

static void sortouts ( struct nfa nfa,
struct state s 
)
static

Definition at line 695 of file regc_nfa.c.

References assert, FREE, i, MALLOC, NERR, state::nouts, arc::outchain, arc::outchainRev, state::outs, qsort, REG_ESPACE, and sortouts_cmp().

Referenced by copyouts(), and moveouts().

697 {
698  struct arc **sortarray;
699  struct arc *a;
700  int n = s->nouts;
701  int i;
702 
703  if (n <= 1)
704  return; /* nothing to do */
705  /* make an array of arc pointers ... */
706  sortarray = (struct arc **) MALLOC(n * sizeof(struct arc *));
707  if (sortarray == NULL)
708  {
709  NERR(REG_ESPACE);
710  return;
711  }
712  i = 0;
713  for (a = s->outs; a != NULL; a = a->outchain)
714  sortarray[i++] = a;
715  assert(i == n);
716  /* ... sort the array */
717  qsort(sortarray, n, sizeof(struct arc *), sortouts_cmp);
718  /* ... and rebuild arc list in order */
719  /* it seems worth special-casing first and last items to simplify loop */
720  a = sortarray[0];
721  s->outs = a;
722  a->outchain = sortarray[1];
723  a->outchainRev = NULL;
724  for (i = 1; i < n - 1; i++)
725  {
726  a = sortarray[i];
727  a->outchain = sortarray[i + 1];
728  a->outchainRev = sortarray[i - 1];
729  }
730  a = sortarray[i];
731  a->outchain = NULL;
732  a->outchainRev = sortarray[i - 1];
733  FREE(sortarray);
734 }
#define NERR(e)
Definition: regc_nfa.c:40
#define REG_ESPACE
Definition: regex.h:151
Definition: regguts.h:290
#define FREE(ptr)
Definition: cryptohash.c:37
#define MALLOC(n)
Definition: regcustom.h:60
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static int sortouts_cmp(const void *a, const void *b)
Definition: regc_nfa.c:737
int nouts
Definition: regguts.h:323
int i
#define qsort(a, b, c, d)
Definition: port.h:504
struct arc * outchainRev
Definition: regguts.h:297

◆ sortouts_cmp()

static int sortouts_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 737 of file regc_nfa.c.

References arc::co, state::no, arc::to, and arc::type.

Referenced by copyouts(), moveouts(), and sortouts().

738 {
739  const struct arc *aa = *((const struct arc *const *) a);
740  const struct arc *bb = *((const struct arc *const *) b);
741 
742  /* we check the fields in the order they are most likely to be different */
743  if (aa->to->no < bb->to->no)
744  return -1;
745  if (aa->to->no > bb->to->no)
746  return 1;
747  if (aa->co < bb->co)
748  return -1;
749  if (aa->co > bb->co)
750  return 1;
751  if (aa->type < bb->type)
752  return -1;
753  if (aa->type > bb->type)
754  return 1;
755  return 0;
756 }
int no
Definition: regguts.h:319
int type
Definition: regguts.h:292
Definition: regguts.h:290
struct state * to
Definition: regguts.h:295
color co
Definition: regguts.h:293

◆ specialcolors()

static void specialcolors ( struct nfa nfa)
static

Definition at line 1582 of file regc_nfa.c.

References assert, nfa::bos, nfa::cm, COLORLESS, nfa::eos, nfa::parent, and pseudocolor().

1583 {
1584  /* false colors for BOS, BOL, EOS, EOL */
1585  if (nfa->parent == NULL)
1586  {
1587  nfa->bos[0] = pseudocolor(nfa->cm);
1588  nfa->bos[1] = pseudocolor(nfa->cm);
1589  nfa->eos[0] = pseudocolor(nfa->cm);
1590  nfa->eos[1] = pseudocolor(nfa->cm);
1591  }
1592  else
1593  {
1594  assert(nfa->parent->bos[0] != COLORLESS);
1595  nfa->bos[0] = nfa->parent->bos[0];
1596  assert(nfa->parent->bos[1] != COLORLESS);
1597  nfa->bos[1] = nfa->parent->bos[1];
1598  assert(nfa->parent->eos[0] != COLORLESS);
1599  nfa->eos[0] = nfa->parent->eos[0];
1600  assert(nfa->parent->eos[1] != COLORLESS);
1601  nfa->eos[1] = nfa->parent->eos[1];
1602  }
1603 }
color eos[2]
Definition: regguts.h:360
static color pseudocolor(struct colormap *cm)
Definition: regc_color.c:312
color bos[2]
Definition: regguts.h:359
#define assert(TEST)
Definition: imath.c:73
struct colormap * cm
Definition: regguts.h:358
struct nfa * parent
Definition: regguts.h:365
#define COLORLESS
Definition: regguts.h:153