diff --git a/compiler/analyser.c b/compiler/analyser.c index bb574245..c69a6b43 100644 --- a/compiler/analyser.c +++ b/compiler/analyser.c @@ -1,4 +1,5 @@ #include +#include /* for INT_MAX */ #include /* printf etc */ #include /* exit */ #include /* memmove */ @@ -760,6 +761,7 @@ static struct node * make_among(struct analyser * a, struct node * p, struct nod x->nocommand_count = 0; x->amongvar_needed = false; x->always_matches = false; + x->shortest_size = INT_MAX; if (q->type == c_bra) { starter = q; q = q->right; } @@ -847,6 +849,8 @@ static struct node * make_among(struct analyser * a, struct node * p, struct nod int size = w0->size; struct amongvec * w; + if (size && size < x->shortest_size) x->shortest_size = size; + for (w = w0 - 1; w >= v; w--) { if (w->size < size && memcmp(w->b, b, w->size * sizeof(symbol)) == 0) { w0->i = w - v; /* fill in index of longest substring */ diff --git a/compiler/generator.c b/compiler/generator.c index ec017ab3..a85276cc 100644 --- a/compiler/generator.c +++ b/compiler/generator.c @@ -1,5 +1,4 @@ #include -#include /* for INT_MAX */ #include /* for fprintf etc */ #include /* for free etc */ #include /* for strlen */ @@ -1460,7 +1459,7 @@ static void generate_substring(struct generator * g, struct node * p) { int empty_case = -1; int n_cases = 0; symbol cases[2]; - int shortest_size = INT_MAX; + int shortest_size = x->shortest_size; int shown_comment = 0; g->S[0] = p->mode == m_forward ? "" : "_b"; @@ -1474,13 +1473,6 @@ static void generate_substring(struct generator * g, struct node * p) { * In backward mode, we can't match if there are fewer characters before * the current position than the minimum length. */ - for (c = 0; c < x->literalstring_count; ++c) { - int size = among_cases[c].size; - if (size != 0 && size < shortest_size) { - shortest_size = size; - } - } - for (c = 0; c < x->literalstring_count; ++c) { symbol ch; if (among_cases[c].size == 0) { diff --git a/compiler/generator_ada.c b/compiler/generator_ada.c index 3eec8f56..cbd4eed0 100644 --- a/compiler/generator_ada.c +++ b/compiler/generator_ada.c @@ -3,7 +3,6 @@ #include /* for strlen */ #include /* for fprintf etc */ #include -#include #include "header.h" /* prototypes */ @@ -1164,7 +1163,7 @@ static void generate_substring(struct generator * g, struct node * p) { int empty_case = -1; int n_cases = 0; symbol cases[2]; - int shortest_size = INT_MAX; + int shortest_size = x->shortest_size; int call_done = 0; int need_among_handler = (x->function_count > 0); @@ -1180,13 +1179,6 @@ static void generate_substring(struct generator * g, struct node * p) { * In backward mode, we can't match if there are fewer characters before * the current position than the minimum length. */ - for (c = 0; c < x->literalstring_count; ++c) { - int size = among_cases[c].size; - if (size != 0 && size < shortest_size) { - shortest_size = size; - } - } - for (c = 0; c < x->literalstring_count; ++c) { symbol ch; if (among_cases[c].size == 0) { diff --git a/compiler/header.h b/compiler/header.h index 6fd1b58c..c391d5a2 100644 --- a/compiler/header.h +++ b/compiler/header.h @@ -242,6 +242,7 @@ struct among { int function_count; /* in this among */ int amongvar_needed; /* do we need to set among_var? */ int always_matches; /* will this among always match? */ + int shortest_size; /* smallest non-zero string length in this among */ struct node * substring; /* i.e. substring ... among ( ... ) */ struct node ** commands; /* array with command_count entries */ };