From 47a81dfb648f2c8d3b9924c87b8d24ddaed172bf Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 26 Jan 2024 13:25:08 -0500 Subject: Use strcasestr() when ignoring case, which improves performance --- match.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/match.c b/match.c index d821f20..20d485b 100644 --- a/match.c +++ b/match.c @@ -337,8 +337,10 @@ static match_t *_next_match(match_ctx_t *ctx, const char *str, pat_t *pat, pat_t // Performance optimization: if the pattern starts with a string literal, // we can just rely on the highly optimized memmem() implementation to skip // past areas where we know we won't find a match. - if (!skip && first->type == BP_STRING && first->min_matchlen > 0 && !ctx->ignorecase) { - char *found = memmem(str, (size_t)(ctx->end - str), Match(first, BP_STRING)->string, first->min_matchlen); + if (!skip && first->type == BP_STRING && first->min_matchlen > 0) { + char *found = ctx->ignorecase ? + strcasestr(str, Match(first, BP_STRING)->string) + : memmem(str, (size_t)(ctx->end - str), Match(first, BP_STRING)->string, first->min_matchlen); str = found ? found : ctx->end; } else if (!skip && str > ctx->start && (first->type == BP_START_OF_LINE || first->type == BP_END_OF_LINE)) { char *found = memchr(str, '\n', (size_t)(ctx->end - str)); -- cgit v1.2.3