For those interested, here's the extra testing for "backtracking" in the match function:
char expected[30] = "defdefg"; // watch for this
size_t expectedLen = strlen( expected );
size_t matchCount = 0; // how many chars have matched so far
void setup()
{
Serial.begin( 9600 );
}
void loop()
{
if (Serial.available())
{
char c = Serial.read();
if (foundExpected( c )) {
Serial.print( F("Found ") );
Serial.println( expected );
}
}
}
bool foundExpected( char c )
{
bool found = false;
if (c == expected[ matchCount ]) {
matchCount++;
if (matchCount == expectedLen) {
found = true;
matchCount = 0; // reset for next time
}
} else {
// Backtrack
int origIndex = matchCount;
while (matchCount > 0) {
// First, search for the current char
matchCount--;
if (c == expected[matchCount]) {
// Then check the previously found characters
// for the current submatch.
size_t diff = origIndex - matchCount;
size_t i;
for (i = 0; i < matchCount; i++) {
if (expected[i] != expected[i + diff])
break;
}
// If we successfully got through the previous loop,
// we found a previous submatch.
if (i == matchCount) {
matchCount++;
break;
}
}
}
}
return found;
}
It's loosely based on the Stream method findMulti, which blocks. ![]()