/* Run DFA to determine whether there is a matching string inside of s. */ int match(DState *start, char *s) { DState *d, *next; int c; char *realstart = s; char *spos = s; char *epos = NULL; d = start; for(; *s; s++){ c = *s & 0xFF; if( !d->l.n ) { /* what we have seen up to this point resulted in a rejecting match. There are no more states to transition to */ if (!epos) { /* and we never saw an accepting state on our way there, so reset the atch start position and restart with the initial state*/ spos = s; d = start; } else { /* on our way we encountered an accepting state, therefore we are done now */ break; } } else if (d->accept) { /* we are in an accepting state, advance the end point of the match, and then continue to see if there is a longer match */ epos = s; } if((next = d->next[c]) == NULL) next = nextstate(d, c); d = next; } if (ismatch(&d->l)) { epos = s; } if (epos) { printf("Start: %d End: %d | %.*s\n", spos-realstart, epos-realstart, epos-spos, spos); return 1; } return 0; }