Skip to content

Commit 734302a

Browse files
committed
Refactor strcasestr implementation for improved readability and efficiency
1 parent 9d775a6 commit 734302a

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/sqlite-vector.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,18 @@
2121
#include <stddef.h>
2222

2323
#ifdef _WIN32
24-
char* strcasestr(const char* haystack, const char* needle) {
25-
if (!haystack || !needle) {
26-
return NULL;
27-
}
28-
29-
if (*needle == '\0') {
30-
return (char*)haystack;
31-
}
32-
33-
size_t needle_len = strlen(needle);
34-
size_t haystack_len = strlen(haystack);
35-
36-
if (needle_len > haystack_len) {
37-
return NULL;
38-
}
39-
40-
for (size_t i = 0; i <= haystack_len - needle_len; i++) {
41-
if (strnicmp(haystack + i, needle, needle_len) == 0) {
42-
return (char*)(haystack + i);
24+
char *strcasestr(const char *haystack, const char *needle) {
25+
if (!haystack || !needle) return NULL;
26+
if (!*needle) return (char *)haystack;
27+
for (; *haystack; ++haystack) {
28+
const char *h = haystack;
29+
const char *n = needle;
30+
while (*h && *n && tolower((unsigned char)*h) == tolower((unsigned char)*n)) {
31+
++h;
32+
++n;
4333
}
34+
if (!*n) return (char *)haystack;
4435
}
45-
4636
return NULL;
4737
}
4838
#endif

0 commit comments

Comments
 (0)