#include <string.h>
#include <locale.h>
+/* Another name for ptrdiff_t, for sizes of objects and nonnegative
+ indexes into objects. It is signed to help catch integer overflow.
+ It has its own name because it is for nonnegative values only. */
+typedef ptrdiff_t idx_t;
+static idx_t const IDX_MAX = PTRDIFF_MAX;
+
static bool
streq (char const *a, char const *b)
{
codes are returned by the lexical analyzer. */
typedef ptrdiff_t token;
-static ptrdiff_t const TOKEN_MAX = PTRDIFF_MAX;
+static token const TOKEN_MAX = PTRDIFF_MAX;
/* States are indexed by state_num values. These are normally
nonnegative but -1 is used as a special value. */
a constraint. */
typedef struct
{
- ptrdiff_t index; /* Index into the parse array. */
+ idx_t index; /* Index into the parse array. */
unsigned int constraint; /* Constraint for matching this position. */
} position;
typedef struct
{
position *elems; /* Elements of this position set. */
- ptrdiff_t nelem; /* Number of elements in this set. */
- ptrdiff_t alloc; /* Number of elements allocated in ELEMS. */
+ idx_t nelem; /* Number of elements in this set. */
+ idx_t alloc; /* Number of elements allocated in ELEMS. */
} position_set;
/* A state of the dfa consists of a set of positions, some flags,
ptrdiff_t cset;
bool invert;
wchar_t *chars; /* Normal characters. */
- ptrdiff_t nchars;
- ptrdiff_t nchars_alloc;
+ idx_t nchars;
+ idx_t nchars_alloc;
};
struct regex_syntax
struct lexer_state
{
char const *ptr; /* Pointer to next input character. */
- ptrdiff_t left; /* Number of characters remaining. */
+ idx_t left; /* Number of characters remaining. */
token lasttok; /* Previous token returned; initially END. */
- ptrdiff_t parens; /* Count of outstanding left parens. */
+ idx_t parens; /* Count of outstanding left parens. */
int minrep, maxrep; /* Repeat counts for {m,n}. */
/* Wide character representation of the current multibyte character,
struct parser_state
{
token tok; /* Lookahead token. */
- ptrdiff_t depth; /* Current depth of a hypothetical stack
+ idx_t depth; /* Current depth of a hypothetical stack
holding deferred productions. This is
used to determine the depth that will be
required of the real stack later on in
/* Fields filled by the scanner. */
charclass *charclasses; /* Array of character sets for CSET tokens. */
- ptrdiff_t cindex; /* Index for adding new charclasses. */
- ptrdiff_t calloc; /* Number of charclasses allocated. */
+ idx_t cindex; /* Index for adding new charclasses. */
+ idx_t calloc; /* Number of charclasses allocated. */
ptrdiff_t canychar; /* Index of anychar class, or -1. */
/* Scanner state */
/* Fields filled by the parser. */
token *tokens; /* Postfix parse array. */
- ptrdiff_t tindex; /* Index for adding new tokens. */
- ptrdiff_t talloc; /* Number of tokens currently allocated. */
- ptrdiff_t depth; /* Depth required of an evaluation stack
+ idx_t tindex; /* Index for adding new tokens. */
+ idx_t talloc; /* Number of tokens currently allocated. */
+ idx_t depth; /* Depth required of an evaluation stack
used for depth-first traversal of the
parse tree. */
- ptrdiff_t nleaves; /* Number of leaves on the parse tree. */
- ptrdiff_t nregexps; /* Count of parallel regexps being built
+ idx_t nleaves; /* Number of leaves on the parse tree. */
+ idx_t nregexps; /* Count of parallel regexps being built
with dfaparse. */
bool fast; /* The DFA is fast. */
token utf8_anychar_classes[5]; /* To lower ANYCHAR in UTF-8 locales. */
/* Fields filled by the state builder. */
dfa_state *states; /* States of the dfa. */
state_num sindex; /* Index for adding new states. */
- ptrdiff_t salloc; /* Number of states currently allocated. */
+ idx_t salloc; /* Number of states currently allocated. */
/* Fields filled by the parse tree->NFA conversion. */
position_set *follows; /* Array of follow sets, indexed by position
/* dfaexec implementation. */
char *(*dfaexec) (struct dfa *, char const *, char *,
- bool, size_t *, bool *);
+ bool, ptrdiff_t *, bool *);
/* The locale is simple, like the C locale. These locales can be
processed more efficiently, as they are single-byte, their native
{ free (A); A = xpalloc (NULL, &AITEMS, ...); }. */
static void *
-xpalloc (void *pa, ptrdiff_t *nitems, ptrdiff_t nitems_incr_min,
- ptrdiff_t nitems_max, ptrdiff_t item_size)
+xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
+ ptrdiff_t nitems_max, idx_t item_size)
{
- ptrdiff_t n0 = *nitems;
+ idx_t n0 = *nitems;
/* The approximate size to use for initial small allocation
requests. This is the largest "small" request for the GNU C
Adjust the growth according to three constraints: NITEMS_INCR_MIN,
NITEMS_MAX, and what the C language can represent safely. */
- ptrdiff_t n, nbytes;
+ idx_t n, nbytes;
if (INT_ADD_WRAPV (n0, n0 >> 1, &n))
- n = PTRDIFF_MAX;
+ n = IDX_MAX;
if (0 <= nitems_max && nitems_max < n)
n = nitems_max;
- ptrdiff_t adjusted_nbytes
+ idx_t adjusted_nbytes
= ((INT_MULTIPLY_WRAPV (n, item_size, &nbytes) || SIZE_MAX < nbytes)
- ? MIN (PTRDIFF_MAX, SIZE_MAX)
+ ? MIN (IDX_MAX, SIZE_MAX)
: nbytes < DEFAULT_MXFAST ? DEFAULT_MXFAST : 0);
if (adjusted_nbytes)
{
ITEM_SIZE is the size of one item; it must be positive.
Avoid O(N**2) behavior on arrays growing linearly. */
static void *
-maybe_realloc (void *pa, ptrdiff_t i, ptrdiff_t *nitems,
- ptrdiff_t nitems_max, ptrdiff_t item_size)
+maybe_realloc (void *pa, idx_t i, idx_t *nitems,
+ ptrdiff_t nitems_max, idx_t item_size)
{
if (i < *nitems)
return pa;
}
/* In DFA D, find the index of charclass S, or allocate a new one. */
-static ptrdiff_t
+static idx_t
charclass_index (struct dfa *d, charclass *s)
{
- ptrdiff_t i;
+ idx_t i;
for (i = 0; i < d->cindex; ++i)
if (equal (s, &d->charclasses[i]))
struct lexptr
{
char const *ptr;
- ptrdiff_t left;
+ idx_t left;
};
static void
/* Extract wide characters into alternations for better performance.
This does not require UTF-8. */
- for (ptrdiff_t i = 0; i < dfa->lex.brack.nchars; i++)
+ for (idx_t i = 0; i < dfa->lex.brack.nchars; i++)
{
addtok_wc (dfa, dfa->lex.brack.chars[i]);
if (need_or)
}
/* Return the number of tokens in the given subexpression. */
-static ptrdiff_t _GL_ATTRIBUTE_PURE
-nsubtoks (struct dfa const *dfa, ptrdiff_t tindex)
+static idx_t _GL_ATTRIBUTE_PURE
+nsubtoks (struct dfa const *dfa, idx_t tindex)
{
switch (dfa->tokens[tindex - 1])
{
case CAT:
case OR:
{
- ptrdiff_t ntoks1 = nsubtoks (dfa, tindex - 1);
+ idx_t ntoks1 = nsubtoks (dfa, tindex - 1);
return 1 + ntoks1 + nsubtoks (dfa, tindex - 1 - ntoks1);
}
}
/* Copy the given subexpression to the top of the tree. */
static void
-copytoks (struct dfa *dfa, ptrdiff_t tindex, ptrdiff_t ntokens)
+copytoks (struct dfa *dfa, idx_t tindex, idx_t ntokens)
{
if (dfa->localeinfo.multibyte)
- for (ptrdiff_t i = 0; i < ntokens; ++i)
+ for (idx_t i = 0; i < ntokens; i++)
addtok_mb (dfa, dfa->tokens[tindex + i],
dfa->multibyte_prop[tindex + i]);
else
- for (ptrdiff_t i = 0; i < ntokens; ++i)
+ for (idx_t i = 0; i < ntokens; i++)
addtok_mb (dfa, dfa->tokens[tindex + i], 3);
}
|| dfa->parse.tok == PLUS || dfa->parse.tok == REPMN)
if (dfa->parse.tok == REPMN && (dfa->lex.minrep || dfa->lex.maxrep))
{
- ptrdiff_t ntokens = nsubtoks (dfa, dfa->tindex);
- ptrdiff_t tindex = dfa->tindex - ntokens;
+ idx_t ntokens = nsubtoks (dfa, dfa->tindex);
+ idx_t tindex = dfa->tindex - ntokens;
if (dfa->lex.maxrep < 0)
addtok (dfa, PLUS);
if (dfa->lex.minrep == 0)
/* Parse a string S of length LEN into D. S can include NUL characters.
This is the main entry point for the parser. */
void
-dfaparse (char const *s, size_t len, struct dfa *d)
+dfaparse (char const *s, idx_t len, struct dfa *d)
{
d->lex.ptr = s;
d->lex.left = len;
}
static void
-alloc_position_set (position_set *s, ptrdiff_t size)
+alloc_position_set (position_set *s, idx_t size)
{
s->elems = xnmalloc (size, sizeof *s->elems);
s->alloc = size;
static void
insert (position p, position_set *s)
{
- ptrdiff_t count = s->nelem;
- ptrdiff_t lo = 0, hi = count;
+ idx_t count = s->nelem;
+ idx_t lo = 0, hi = count;
while (lo < hi)
{
- ptrdiff_t mid = (lo + hi) >> 1;
+ idx_t mid = (lo + hi) >> 1;
if (s->elems[mid].index < p.index)
lo = mid + 1;
else if (s->elems[mid].index == p.index)
}
s->elems = maybe_realloc (s->elems, count, &s->alloc, -1, sizeof *s->elems);
- for (ptrdiff_t i = count; i > lo; i--)
+ for (idx_t i = count; i > lo; i--)
s->elems[i] = s->elems[i - 1];
s->elems[lo] = p;
++s->nelem;
static void
append (position p, position_set *s)
{
- ptrdiff_t count = s->nelem;
+ idx_t count = s->nelem;
s->elems = maybe_realloc (s->elems, count, &s->alloc, -1, sizeof *s->elems);
s->elems[s->nelem++] = p;
}
merge_constrained (position_set const *s1, position_set const *s2,
unsigned int c2, position_set *m)
{
- ptrdiff_t i = 0, j = 0;
+ idx_t i = 0, j = 0;
if (m->alloc - s1->nelem < s2->nelem)
{
{
if (src->nelem < 4)
{
- for (ptrdiff_t i = 0; i < src->nelem; ++i)
+ for (idx_t i = 0; i < src->nelem; i++)
insert (src->elems[i], dst);
}
else
/* Delete a position from a set. Return the nonzero constraint of the
deleted position, or zero if there was no such position. */
static unsigned int
-delete (ptrdiff_t del, position_set *s)
+delete (idx_t del, position_set *s)
{
- ptrdiff_t count = s->nelem;
- ptrdiff_t lo = 0, hi = count;
+ idx_t count = s->nelem;
+ idx_t lo = 0, hi = count;
while (lo < hi)
{
- ptrdiff_t mid = (lo + hi) >> 1;
+ idx_t mid = (lo + hi) >> 1;
if (s->elems[mid].index < del)
lo = mid + 1;
else if (s->elems[mid].index == del)
{
unsigned int c = s->elems[mid].constraint;
- ptrdiff_t i;
+ idx_t i;
for (i = mid; i + 1 < count; i++)
s->elems[i] = s->elems[i + 1];
s->nelem = i;
/* Replace a position with the followed set. */
static void
-replace (position_set *dst, ptrdiff_t del, position_set *add,
+replace (position_set *dst, idx_t del, position_set *add,
unsigned int constraint, position_set *tmp)
{
unsigned int c = delete (del, dst) & constraint;
{
position_set tmp;
alloc_position_set (&tmp, d->nleaves);
- for (ptrdiff_t i = 0; i < d->tindex; ++i)
+ for (idx_t i = 0; i < d->tindex; i++)
if (d->follows[i].nelem > 0 && d->tokens[i] >= NOTCHAR
&& d->tokens[i] != BACKREF && d->tokens[i] != ANYCHAR
&& d->tokens[i] != MBCSET && d->tokens[i] < CSET)
delete (i, &d->follows[i]);
- for (ptrdiff_t j = 0; j < d->tindex; j++)
+ for (idx_t j = 0; j < d->tindex; j++)
if (i != j && d->follows[j].nelem > 0)
replace (&d->follows[j], i, &d->follows[i], constraint, &tmp);
}
{
int separate_contexts = 0;
- for (ptrdiff_t j = 0; j < s->nelem; j++)
+ for (idx_t j = 0; j < s->nelem; j++)
separate_contexts |= d->separates[s->elems[j].index];
return separate_contexts;
};
static void
-merge_nfa_state (struct dfa *d, ptrdiff_t tindex, char *flags,
+merge_nfa_state (struct dfa *d, idx_t tindex, char *flags,
position_set *merged)
{
position_set *follows = d->follows;
- ptrdiff_t nelem = 0;
+ idx_t nelem = 0;
d->constraints[tindex] = 0;
- for (ptrdiff_t i = 0; i < follows[tindex].nelem; i++)
+ for (idx_t i = 0; i < follows[tindex].nelem; i++)
{
- ptrdiff_t sindex = follows[tindex].elems[i].index;
+ idx_t sindex = follows[tindex].elems[i].index;
/* Skip the node as pruned in future. */
unsigned int iconstraint = follows[tindex].elems[i].constraint;
if (!(flags[sindex] & (OPT_LPAREN | OPT_RPAREN)))
{
- ptrdiff_t j;
+ idx_t j;
for (j = 0; j < nelem; j++)
{
- ptrdiff_t dindex = follows[tindex].elems[j].index;
+ idx_t dindex = follows[tindex].elems[j].index;
if (follows[tindex].elems[j].constraint != iconstraint)
continue;
static void
reorder_tokens (struct dfa *d)
{
- ptrdiff_t nleaves;
+ idx_t nleaves;
ptrdiff_t *map;
token *tokens;
position_set *follows;
map[0] = nleaves++;
- for (ptrdiff_t i = 1; i < d->tindex; i++)
+ for (idx_t i = 1; i < d->tindex; i++)
map[i] = -1;
tokens = xnmalloc (d->nleaves, sizeof *tokens);
else
multibyte_prop = NULL;
- for (ptrdiff_t i = 0; i < d->tindex; i++)
+ for (idx_t i = 0; i < d->tindex; i++)
{
if (map[i] == -1)
{
if (multibyte_prop != NULL)
multibyte_prop[map[i]] = d->multibyte_prop[i];
- for (ptrdiff_t j = 0; j < d->follows[i].nelem; j++)
+ for (idx_t j = 0; j < d->follows[i].nelem; j++)
{
if (map[d->follows[i].elems[j].index] == -1)
map[d->follows[i].elems[j].index] = nleaves++;
sizeof *d->follows[i].elems, compare);
}
- for (ptrdiff_t i = 0; i < nleaves; i++)
+ for (idx_t i = 0; i < nleaves; i++)
{
d->tokens[i] = tokens[i];
d->follows[i] = follows[i];
flags = xmalloc (d->tindex * sizeof *flags);
memset (flags, 0, d->tindex * sizeof *flags);
- for (ptrdiff_t i = 0; i < d->tindex; i++)
+ for (idx_t i = 0; i < d->tindex; i++)
{
- for (ptrdiff_t j = 0; j < d->follows[i].nelem; j++)
+ for (idx_t j = 0; j < d->follows[i].nelem; j++)
{
if (d->follows[i].elems[j].index == i)
flags[d->follows[i].elems[j].index] |= OPT_REPEAT;
d->constraints = xnmalloc (d->tindex, sizeof *d->constraints);
- for (ptrdiff_t i = 0; i < d->tindex; i++)
+ for (idx_t i = 0; i < d->tindex; i++)
if (flags[i] & OPT_QUEUED)
merge_nfa_state (d, i, flags, merged);
bool nullable;
/* Counts of firstpos and lastpos sets. */
- ptrdiff_t nfirstpos;
- ptrdiff_t nlastpos;
+ idx_t nfirstpos;
+ idx_t nlastpos;
} *stkalloc = xnmalloc (d->depth, sizeof *stkalloc), *stk = stkalloc;
position_set merged; /* Result of merging sets. */
#ifdef DEBUG
fprintf (stderr, "dfaanalyze:\n");
- for (ptrdiff_t i = 0; i < d->tindex; ++i)
+ for (idx_t i = 0; i < d->tindex; i++)
{
fprintf (stderr, " %td:", i);
prtok (d->tokens[i]);
alloc_position_set (&merged, d->nleaves);
d->follows = xcalloc (d->tindex, sizeof *d->follows);
- for (ptrdiff_t i = 0; i < d->tindex; ++i)
+ for (idx_t i = 0; i < d->tindex; i++)
{
switch (d->tokens[i])
{
tmp.elems = firstpos - stk[-1].nfirstpos;
tmp.nelem = stk[-1].nfirstpos;
position *p = lastpos - stk[-1].nlastpos;
- for (ptrdiff_t j = 0; j < stk[-1].nlastpos; j++)
+ for (idx_t j = 0; j < stk[-1].nlastpos; j++)
{
merge (&tmp, &d->follows[p[j].index], &merged);
copy (&merged, &d->follows[p[j].index]);
tmp.nelem = stk[-1].nfirstpos;
tmp.elems = firstpos - stk[-1].nfirstpos;
position *p = lastpos - stk[-1].nlastpos - stk[-2].nlastpos;
- for (ptrdiff_t j = 0; j < stk[-2].nlastpos; j++)
+ for (idx_t j = 0; j < stk[-2].nlastpos; j++)
{
merge (&tmp, &d->follows[p[j].index], &merged);
copy (&merged, &d->follows[p[j].index]);
else
{
position *p = lastpos - stk[-1].nlastpos - stk[-2].nlastpos;
- for (ptrdiff_t j = 0; j < stk[-1].nlastpos; j++)
+ for (idx_t j = 0; j < stk[-1].nlastpos; j++)
p[j] = p[j + stk[-2].nlastpos];
lastpos -= stk[-2].nlastpos;
stk[-2].nlastpos = stk[-1].nlastpos;
fprintf (stderr,
stk[-1].nullable ? " nullable: yes\n" : " nullable: no\n");
fprintf (stderr, " firstpos:");
- for (ptrdiff_t j = 0; j < stk[-1].nfirstpos; j++)
+ for (idx_t j = 0; j < stk[-1].nfirstpos; j++)
{
fprintf (stderr, " %td:", firstpos[j - stk[-1].nfirstpos].index);
prtok (d->tokens[firstpos[j - stk[-1].nfirstpos].index]);
}
fprintf (stderr, "\n lastpos:");
- for (ptrdiff_t j = 0; j < stk[-1].nlastpos; j++)
+ for (idx_t j = 0; j < stk[-1].nlastpos; j++)
{
fprintf (stderr, " %td:", lastpos[j - stk[-1].nlastpos].index);
prtok (d->tokens[lastpos[j - stk[-1].nlastpos].index]);
dfaoptimize (d);
#ifdef DEBUG
- for (ptrdiff_t i = 0; i < d->tindex; ++i)
+ for (idx_t i = 0; i < d->tindex; i++)
if (d->tokens[i] == BEG || d->tokens[i] < NOTCHAR
|| d->tokens[i] == BACKREF || d->tokens[i] == ANYCHAR
|| d->tokens[i] == MBCSET || d->tokens[i] >= CSET)
fprintf (stderr, "follows(%td:", i);
prtok (d->tokens[i]);
fprintf (stderr, "):");
- for (ptrdiff_t j = 0; j < d->follows[i].nelem; j++)
+ for (idx_t j = 0; j < d->follows[i].nelem; j++)
{
fprintf (stderr, " %td:", d->follows[i].elems[j].index);
prtok (d->tokens[d->follows[i].elems[j].index]);
d->separates = xnmalloc (d->tindex, sizeof *d->separates);
- for (ptrdiff_t i = 0; i < d->tindex; i++)
+ for (idx_t i = 0; i < d->tindex; i++)
{
d->separates[i] = 0;
if (prev_letter_dependent (d->constraints[i]))
d->separates[i] |= CTX_LETTER;
- for (ptrdiff_t j = 0; j < d->follows[i].nelem; j++)
+ for (idx_t j = 0; j < d->follows[i].nelem; j++)
{
if (prev_newline_dependent (d->follows[i].elems[j].constraint))
d->separates[i] |= CTX_NEWLINE;
if (oldalloc < d->sindex)
{
state_num **realtrans = d->trans ? d->trans - 2 : NULL;
- ptrdiff_t newalloc1 = realtrans ? d->tralloc + 2 : 0;
+ idx_t newalloc1 = realtrans ? d->tralloc + 2 : 0;
realtrans = xpalloc (realtrans, &newalloc1, d->sindex - oldalloc,
-1, sizeof *realtrans);
realtrans[0] = realtrans[1] = NULL;
d->trans = realtrans + 2;
- ptrdiff_t newalloc = d->tralloc = newalloc1 - 2;
+ idx_t newalloc = d->tralloc = newalloc1 - 2;
d->fails = xnrealloc (d->fails, newalloc, sizeof *d->fails);
d->success = xnrealloc (d->success, newalloc, sizeof *d->success);
d->newlines = xnrealloc (d->newlines, newalloc, sizeof *d->newlines);
/* Find the union of the follows of the positions of the group.
This is a hideously inefficient loop. Fix it someday. */
- for (ptrdiff_t j = 0; j < d->states[s].elems.nelem; j++)
- for (ptrdiff_t k = 0;
+ for (idx_t j = 0; j < d->states[s].elems.nelem; j++)
+ for (idx_t k = 0;
k < d->follows[d->states[s].elems.elems[j].index].nelem; ++k)
insert (d->follows[d->states[s].elems.elems[j].index].elems[k],
&follows);
charclass label;
fillset (&label);
- for (ptrdiff_t i = 0; i < follows.nelem; ++i)
+ for (idx_t i = 0; i < follows.nelem; i++)
{
charclass matches; /* Set of matching characters. */
position pos = follows.elems[i];
if (!mergeit)
{
mergeit = true;
- for (ptrdiff_t j = 0; mergeit && j < group.nelem; j++)
+ for (idx_t j = 0; mergeit && j < group.nelem; j++)
mergeit &= d->multibyte_prop[group.elems[j].index];
}
if (mergeit)
static inline char *
dfaexec_main (struct dfa *d, char const *begin, char *end, bool allow_nl,
- size_t *count, bool multibyte)
+ ptrdiff_t *count, bool multibyte)
{
if (MAX_TRCOUNT <= d->sindex)
{
alloc_position_set (&d->mb_follows, d->nleaves);
}
- ptrdiff_t nlcount = 0;
+ idx_t nlcount = 0;
for (;;)
{
state_num *t;
static char *
dfaexec_mb (struct dfa *d, char const *begin, char *end,
- bool allow_nl, size_t *count, bool *backref)
+ bool allow_nl, ptrdiff_t *count, bool *backref)
{
return dfaexec_main (d, begin, end, allow_nl, count, true);
}
static char *
dfaexec_sb (struct dfa *d, char const *begin, char *end,
- bool allow_nl, size_t *count, bool *backref)
+ bool allow_nl, ptrdiff_t *count, bool *backref)
{
return dfaexec_main (d, begin, end, allow_nl, count, false);
}
any regexp that uses a construct not supported by this code. */
static char *
dfaexec_noop (struct dfa *d, char const *begin, char *end,
- bool allow_nl, size_t *count, bool *backref)
+ bool allow_nl, ptrdiff_t *count, bool *backref)
{
*backref = true;
return (char *) begin;
char *
dfaexec (struct dfa *d, char const *begin, char *end,
- bool allow_nl, size_t *count, bool *backref)
+ bool allow_nl, ptrdiff_t *count, bool *backref)
{
return d->dfaexec (d, begin, end, allow_nl, count, backref);
}
static bool _GL_ATTRIBUTE_PURE
dfa_supported (struct dfa const *d)
{
- for (ptrdiff_t i = 0; i < d->tindex; i++)
+ for (idx_t i = 0; i < d->tindex; i++)
{
switch (d->tokens[i])
{
return;
bool have_backref = false;
- for (ptrdiff_t i = 0; i < d->tindex; ++i)
+ for (idx_t i = 0; i < d->tindex; i++)
{
switch (d->tokens[i])
{
bool have_achar = false;
bool have_nchar = false;
- ptrdiff_t j;
- for (ptrdiff_t i = j = 0; i < d->tindex; i++)
+ idx_t j;
+ for (idx_t i = j = 0; i < d->tindex; i++)
{
switch (d->tokens[i])
{
Then analyze D and build a matcher for it.
SEARCHFLAG says whether to build a searching or an exact matcher. */
void
-dfacomp (char const *s, size_t len, struct dfa *d, bool searchflag)
+dfacomp (char const *s, idx_t len, struct dfa *d, bool searchflag)
{
if (s != NULL)
dfaparse (s, len, d);
free (d->constraints);
free (d->separates);
- for (ptrdiff_t i = 0; i < d->sindex; ++i)
+ for (idx_t i = 0; i < d->sindex; i++)
{
free (d->states[i].elems.elems);
free (d->states[i].mbps.elems);
if (d->follows)
{
- for (ptrdiff_t i = 0; i < d->tindex; ++i)
+ for (idx_t i = 0; i < d->tindex; i++)
free (d->follows[i].elems);
free (d->follows);
}
if (d->trans)
{
- for (ptrdiff_t i = 0; i < d->tralloc; ++i)
+ for (idx_t i = 0; i < d->tralloc; i++)
{
free (d->trans[i]);
free (d->fails[i]);
static char *
icatalloc (char *old, char const *new)
{
- ptrdiff_t newsize = strlen (new);
+ idx_t newsize = strlen (new);
if (newsize == 0)
return old;
- ptrdiff_t oldsize = strlen (old);
+ idx_t oldsize = strlen (old);
char *result = xrealloc (old, oldsize + newsize + 1);
memcpy (result + oldsize, new, newsize + 1);
return result;
}
static char **
-enlist (char **cpp, char *new, ptrdiff_t len)
+enlist (char **cpp, char *new, idx_t len)
{
new = memcpy (xmalloc (len + 1), new, len);
new[len] = '\0';
/* Is there already something in the list that's new (or longer)? */
- ptrdiff_t i;
+ idx_t i;
for (i = 0; cpp[i] != NULL; i++)
if (strstr (cpp[i], new) != NULL)
{
return cpp;
}
/* Eliminate any obsoleted strings. */
- for (ptrdiff_t j = 0; cpp[j] != NULL; )
+ for (idx_t j = 0; cpp[j] != NULL; )
if (strstr (new, cpp[j]) == NULL)
++j;
else
for (char *lcp = left; *lcp != '\0'; lcp++)
{
- ptrdiff_t len = 0;
+ idx_t len = 0;
char *rcp = strchr (right, *lcp);
while (rcp != NULL)
{
- ptrdiff_t i;
+ idx_t i;
for (i = 1; lcp[i] != '\0' && lcp[i] == rcp[i]; ++i)
continue;
if (i > len)
{
char **both = xzalloc (sizeof *both);
- for (ptrdiff_t lnum = 0; left[lnum] != NULL; lnum++)
+ for (idx_t lnum = 0; left[lnum] != NULL; lnum++)
{
- for (ptrdiff_t rnum = 0; right[rnum] != NULL; rnum++)
+ for (idx_t rnum = 0; right[rnum] != NULL; rnum++)
{
char **temp = comsubs (left[lnum], right[rnum]);
both = addlists (both, temp);
};
static must *
-allocmust (must *mp, ptrdiff_t size)
+allocmust (must *mp, idx_t size)
{
must *new_mp = xmalloc (sizeof *new_mp);
new_mp->in = xzalloc (sizeof *new_mp->in);
bool need_endline = false;
bool case_fold_unibyte = d->syntax.case_fold && MB_CUR_MAX == 1;
- for (ptrdiff_t ri = 1; ri + 1 < d->tindex; ri++)
+ for (idx_t ri = 1; ri + 1 < d->tindex; ri++)
{
token t = d->tokens[ri];
switch (t)
char **new;
must *rmp = mp;
must *lmp = mp = mp->prev;
- ptrdiff_t j, ln, rn, n;
+ idx_t j, ln, rn, n;
/* Guaranteed to be. Unlikely, but ... */
if (streq (lmp->is, rmp->is))
lmp->endline = false;
}
/* Left side--easy */
- ptrdiff_t i = 0;
+ idx_t i = 0;
while (lmp->left[i] != '\0' && lmp->left[i] == rmp->left[i])
++i;
lmp->left[i] = '\0';
case END:
assert (!mp->prev);
- for (ptrdiff_t i = 0; mp->in[i] != NULL; i++)
+ for (idx_t i = 0; mp->in[i] != NULL; i++)
if (strlen (mp->in[i]) > strlen (result))
result = mp->in[i];
if (streq (result, mp->is))
lmp->in = addlists (lmp->in, rmp->in);
if (lmp->right[0] != '\0' && rmp->left[0] != '\0')
{
- ptrdiff_t lrlen = strlen (lmp->right);
- ptrdiff_t rllen = strlen (rmp->left);
+ idx_t lrlen = strlen (lmp->right);
+ idx_t rllen = strlen (rmp->left);
char *tp = xmalloc (lrlen + rllen);
memcpy (tp, lmp->right, lrlen);
memcpy (tp + lrlen, rmp->left, rllen);
}
}
- ptrdiff_t rj = ri + 2;
+ idx_t rj = ri + 2;
if (d->tokens[ri + 1] == CAT)
{
for (; rj < d->tindex - 1; rj += 2)
mp->is[0] = mp->left[0] = mp->right[0]
= case_fold_unibyte ? toupper (t) : t;
- ptrdiff_t i;
+ idx_t i;
for (i = 1; ri + 2 < rj; i++)
{
ri += 2;