// SPDX-License-Identifier: GPL-2.0-or-later /* * lib/ts_fsm.c A naive finite state machine text search approach * * Authors: Thomas Graf <tgraf@suug.ch> * * ========================================================================== * * A finite state machine consists of n states (struct ts_fsm_token) * representing the pattern as a finite automaton. The data is read * sequentially on an octet basis. Every state token specifies the number * of recurrences and the type of value accepted which can be either a * specific character or ctype based set of characters. The available * type of recurrences include 1, (0|1), [0 n], and [1 n]. * * The algorithm differs between strict/non-strict mode specifying * whether the pattern has to start at the first octet. Strict mode * is enabled by default and can be disabled by inserting * TS_FSM_HEAD_IGNORE as the first token in the chain. * * The runtime performance of the algorithm should be around O(n), * however while in strict mode the average runtime can be better.
*/
for (tok_idx = 0; tok_idx < fsm->ntokens; tok_idx++) {
cur = &fsm->tokens[tok_idx];
if (likely(tok_idx < (fsm->ntokens - 1)))
next = &fsm->tokens[tok_idx + 1]; else
next = NULL;
switch (cur->recur) { case TS_FSM_SINGLE: if (end_of_data()) goto no_match;
if (!match_token(cur, data[block_idx]))
TOKEN_MISMATCH(); break;
case TS_FSM_PERHAPS: if (end_of_data() ||
!match_token(cur, data[block_idx])) continue; break;
case TS_FSM_MULTI: if (end_of_data()) goto no_match;
if (!match_token(cur, data[block_idx]))
TOKEN_MISMATCH();
block_idx++;
fallthrough;
case TS_FSM_ANY: if (next == NULL) goto found_match;
if (end_of_data()) continue;
while (!match_token(next, data[block_idx])) { if (!match_token(cur, data[block_idx]))
TOKEN_MISMATCH();
block_idx++; if (end_of_data()) goto no_match;
} continue;
/* * Optimization: Prefer small local loop over jumping * back and forth until garbage at head is munched.
*/ case TS_FSM_HEAD_IGNORE: if (end_of_data()) continue;
while (!match_token(next, data[block_idx])) { /* * Special case, don't start over upon * a mismatch, give the user the * chance to specify the type of data * allowed to be ignored.
*/ if (!match_token(cur, data[block_idx])) goto no_match;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.