moved to folders, updated etc
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
Character stream pattern matcher
|
||||
================================
|
||||
|
||||
Matcher can be used for detecting a pattern in a stream of characters.
|
||||
With each incoming character, call `matcher_test()`.
|
||||
|
||||
It will return true if the character completed a match.
|
||||
|
||||
Applications
|
||||
------------
|
||||
|
||||
This buffer was designed for parsing responses to AT commands from ESP8266.
|
||||
|
||||
It can be used for any similar purpose.
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "matcher.h"
|
||||
|
||||
void matcher_reset(matcher_t *m)
|
||||
{
|
||||
m->cursor = 0;
|
||||
}
|
||||
|
||||
|
||||
/** 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;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @file matcher.h
|
||||
* @author Ondřej Hruška, 2016
|
||||
*
|
||||
* String matching utility.
|
||||
*
|
||||
* Matcher can be used for detecting a pattern in a stream of characters.
|
||||
* With each incoming character, call matcher_test().
|
||||
*
|
||||
* It will return true if the character completed a match.
|
||||
*
|
||||
* MIT license
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
const char *pattern;
|
||||
size_t cursor;
|
||||
} matcher_t;
|
||||
|
||||
|
||||
/** reset match progress */
|
||||
void matcher_reset(matcher_t *m);
|
||||
|
||||
|
||||
/**
|
||||
* Consume an incoming character.
|
||||
* If this char was the last char of the pattern, returns true and resets matcher.
|
||||
*
|
||||
* If the char is not in the pattern, resets matcher.
|
||||
*
|
||||
* @returns true if the char concluded the expected pattern.
|
||||
*/
|
||||
bool matcher_test(matcher_t * mb, uint8_t b);
|
||||
Reference in New Issue
Block a user