Kernel: add memmem

This commit is contained in:
apio 2022-10-15 18:59:05 +02:00
parent 7ab80014e1
commit 397b1a2819
2 changed files with 14 additions and 8 deletions

View File

@ -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* strncat(char* dest, const char* src, size_t n);
char* strstr(char* haystack, const char* needle); 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* memcpy(void* dest, const void* src, size_t n);
void* memset(void* dest, int c, size_t n); void* memset(void* dest, int c, size_t n);

View File

@ -87,20 +87,25 @@ char* strcat(char* dest, const char* src)
char* strstr(char* haystack, const char* needle) char* strstr(char* haystack, const char* needle)
{ {
size_t needle_size = strlen(needle); return (char*)memmem(haystack, strlen(haystack), needle, strlen(needle));
size_t haystack_size = strlen(haystack); }
while (*haystack)
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<char*>(hs);
} }
else { return NULL; } else { return NULL; }
} }
haystack++; hs++;
haystack_size--; haystacklen--;
} }
return NULL; return NULL;
} }