// start in state 0
long lCurrentState = 0;
long iStartIndex = 0;
for(int i=0; i<strlength; i++)
{
long lPrevState = lCurrentState;
// save a pointer to the starting character,
// if we are in state 0
if(lCurrentState==0)
iStartIndex=i;
// follow up the table states
lCurrentState = FSM[lCurrentState][(unsigned char)pStr[i]];
// if we reach a -1, return the position of the
// start of the substring - we have found a full match
if(lCurrentState == -1)
return iStartIndex;
// have we followed a false lead?
// if so, backtrack i to position to continue search
// with state reset to zero set i to iStartIndex,
// and it will be incremented as the loop re-enters
if(lPrevState != 0 && lCurrentState == 0)
i = iStartIndex;
}
// If we get here, no matches were found
return -1;
End of Listing