Commit 86e9c9aa authored by Jeremy Sowden's avatar Jeremy Sowden Committed by Florian Westphal

lib/ts_bm: add helper to reduce indentation and improve readability

The flow-control of `bm_find` is very deeply nested with a conditional
comparing a ternary expression against the pattern inside a for-loop
inside a while-loop inside a for-loop.

Move the inner for-loop into a helper function to reduce the amount of
indentation and make the code easier to read.

Fix indentation and trailing white-space in preceding debug logging
statement.
Signed-off-by: default avatarJeremy Sowden <jeremy@azazel.net>
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
parent 0c805e80
...@@ -55,6 +55,24 @@ struct ts_bm ...@@ -55,6 +55,24 @@ struct ts_bm
unsigned int good_shift[]; unsigned int good_shift[];
}; };
static unsigned int matchpat(const u8 *pattern, unsigned int patlen,
const u8 *text, bool icase)
{
unsigned int i;
for (i = 0; i < patlen; i++) {
u8 t = *(text-i);
if (icase)
t = toupper(t);
if (t != *(pattern-i))
break;
}
return i;
}
static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
{ {
struct ts_bm *bm = ts_config_priv(conf); struct ts_bm *bm = ts_config_priv(conf);
...@@ -72,19 +90,18 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) ...@@ -72,19 +90,18 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
break; break;
while (shift < text_len) { while (shift < text_len) {
DEBUGP("Searching in position %d (%c)\n", DEBUGP("Searching in position %d (%c)\n",
shift, text[shift]); shift, text[shift]);
for (i = 0; i < bm->patlen; i++)
if ((icase ? toupper(text[shift-i]) i = matchpat(&bm->pattern[bm->patlen-1], bm->patlen,
: text[shift-i]) &text[shift], icase);
!= bm->pattern[bm->patlen-1-i]) if (i == bm->patlen) {
goto next; /* London calling... */
DEBUGP("found!\n");
/* London calling... */ return consumed + (shift-(bm->patlen-1));
DEBUGP("found!\n"); }
return consumed + (shift-(bm->patlen-1));
bs = bm->bad_shift[text[shift-i]];
next: bs = bm->bad_shift[text[shift-i]];
/* Now jumping to... */ /* Now jumping to... */
shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]); shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment