From: Jeremy Sowden <jeremy@azazel.net>
To: Netfilter Devel <netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH nf-next] lib/ts_bm: add helper to reduce indentation and improve readability
Date: Tue, 20 Jun 2023 19:10:50 +0100 [thread overview]
Message-ID: <20230620181050.GF82872@azazel.net> (raw)
In-Reply-To: <20230620180925.2010176-1-jeremy@azazel.net>
[-- Attachment #1: Type: text/plain, Size: 2316 bytes --]
On 2023-06-20, at 19:09:25 +0100, Jeremy Sowden wrote:
> 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.
>
> Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
> ---
> lib/ts_bm.c | 42 +++++++++++++++++++++++++++++-------------
> 1 file changed, 29 insertions(+), 13 deletions(-)
Sent the wrong version. Apologies.
J.
> diff --git a/lib/ts_bm.c b/lib/ts_bm.c
> index 1f2234221dd1..d74fdb87d269 100644
> --- a/lib/ts_bm.c
> +++ b/lib/ts_bm.c
> @@ -55,6 +55,24 @@ struct ts_bm
> unsigned int good_shift[];
> };
>
> +static bool patmtch(const u8 *pattern, const u8 *text, unsigned int patlen,
> + bool icase)
> +{
> + unsigned int i;
> +
> + for (i = 0; i < patlen; i++) {
> + u8 t = *(text-i);
> +
> + if (icase)
> + t = toupper(t);
> +
> + if (t != *(pattern-i))
> + return false;
> + }
> +
> + return true;
> +}
> +
> static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
> {
> struct ts_bm *bm = ts_config_priv(conf);
> @@ -70,19 +88,17 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
> break;
>
> while (shift < text_len) {
> - DEBUGP("Searching in position %d (%c)\n",
> - shift, text[shift]);
> - for (i = 0; i < bm->patlen; i++)
> - if ((icase ? toupper(text[shift-i])
> - : text[shift-i])
> - != bm->pattern[bm->patlen-1-i])
> - goto next;
> -
> - /* London calling... */
> - DEBUGP("found!\n");
> - return consumed + (shift-(bm->patlen-1));
> -
> -next: bs = bm->bad_shift[text[shift-i]];
> + DEBUGP("Searching in position %d (%c)\n",
> + shift, text[shift]);
> +
> + if (patmtch(&bm->pattern[bm->patlen-1], &text[shift],
> + bm->patlen, icase)) {
> + /* London calling... */
> + DEBUGP("found!\n");
> + return consumed + (shift-(bm->patlen-1));
> + }
> +
> + bs = bm->bad_shift[text[shift-i]];
>
> /* Now jumping to... */
> shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
> --
> 2.39.2
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2023-06-20 18:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-20 18:09 [PATCH nf-next] lib/ts_bm: add helper to reduce indentation and improve readability Jeremy Sowden
2023-06-20 18:10 ` Jeremy Sowden [this message]
-- strict thread matches above, loose matches on Subject: below --
2023-06-19 19:08 Jeremy Sowden
2023-06-20 10:42 ` kernel test robot
2023-06-20 11:14 ` Jeremy Sowden
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230620181050.GF82872@azazel.net \
--to=jeremy@azazel.net \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).