You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
605 B
34 lines
605 B
9 years ago
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdint.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#include "matcher.h"
|
||
|
|
||
9 years ago
|
void matcher_reset(matcher_t *m)
|
||
|
{
|
||
|
m->cursor = 0;
|
||
|
}
|
||
|
|
||
9 years ago
|
|
||
|
/** Handle incoming char. Returns true if this char completed the match. */
|
||
|
bool matcher_test(matcher_t * m, uint8_t b)
|
||
|
{
|
||
|
// If mismatch, rewind (and check at 0)
|
||
|
if (m->pattern[m->cursor] != b) {
|
||
|
m->cursor = 0;
|
||
|
}
|
||
|
|
||
|
// Check for match
|
||
|
if (m->pattern[m->cursor] == b) {
|
||
|
// Good char
|
||
|
m->cursor++;
|
||
|
if (m->pattern[m->cursor] == 0) { // end of pattern
|
||
|
m->cursor = 0; // rewind
|
||
|
return true; // indicate success
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|