From 397b1a28190472f704762f117e08ce07cd3320db Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 15 Oct 2022 18:59:05 +0200 Subject: [PATCH] Kernel: add memmem --- kernel/include/std/string.h | 1 + kernel/src/std/string.cpp | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/kernel/include/std/string.h b/kernel/include/std/string.h index ac1a2459..7c0db986 100644 --- a/kernel/include/std/string.h +++ b/kernel/include/std/string.h @@ -13,6 +13,7 @@ int strncmp(const char* a, const char* b, size_t n); char* strncat(char* dest, const char* src, size_t n); char* strstr(char* haystack, const char* needle); +void* memmem(void* haystack, size_t haystacklen, const void* needle, size_t needlelen); void* memcpy(void* dest, const void* src, size_t n); void* memset(void* dest, int c, size_t n); diff --git a/kernel/src/std/string.cpp b/kernel/src/std/string.cpp index ef5481fd..c133e9fa 100644 --- a/kernel/src/std/string.cpp +++ b/kernel/src/std/string.cpp @@ -87,20 +87,25 @@ char* strcat(char* dest, const char* src) char* strstr(char* haystack, const char* needle) { - size_t needle_size = strlen(needle); - size_t haystack_size = strlen(haystack); - while (*haystack) + return (char*)memmem(haystack, strlen(haystack), needle, strlen(needle)); +} + +void* memmem(void* haystack, size_t haystacklen, const void* needle, size_t needlelen) +{ + const char* hs = (const char*)haystack; + const char* nd = (const char*)needle; + while (haystacklen) { - if (*haystack == *needle) + if (*hs == *nd) { - if (needle_size <= haystack_size) + if (needlelen <= haystacklen) { - if (!strncmp(haystack, needle, needle_size)) return haystack; + if (!memcmp(hs, nd, needlelen)) return (void*)const_cast(hs); } else { return NULL; } } - haystack++; - haystack_size--; + hs++; + haystacklen--; } return NULL; }