From 43248244b8753f9ee2162e7e8ce3f407c2149d6f Mon Sep 17 00:00:00 2001 From: Vladimir Gavrilov <105977161+0xA50C1A1@users.noreply.github.com> Date: Mon, 12 May 2025 10:46:54 +0300 Subject: Micro-optimizations of 'ndpi_strncasestr' and 'LINE_*' macros (#2808) --- src/lib/ndpi_main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/lib/ndpi_main.c') diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 0146f5b18..0cc57e19b 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -10533,10 +10533,18 @@ const char * ndpi_strncasestr(const char *s, const char *find, size_t len) { const size_t s_len = strnlen(s, len); + /* If 'find' is longer than 's', no match is possible */ + if (find_len > s_len) { + return NULL; + } + const char *const end_of_search = s + s_len - find_len + 1; + /* Cache the lowercased first character of 'find' */ + const unsigned char fc = tolower((unsigned char) *find); + for (; s < end_of_search; ++s) { - if (tolower((unsigned char)*s) == tolower((unsigned char)*find)) { + if (tolower((unsigned char)*s) == fc) { if (strncasecmp(s + 1, find + 1, find_len - 1) == 0) { return s; } -- cgit v1.2.3