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* 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);

View File

@ -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)
{
if (*haystack == *needle)
const char* hs = (const char*)haystack;
const char* nd = (const char*)needle;
while (haystacklen)
{
if (needle_size <= haystack_size)
if (*hs == *nd)
{
if (!strncmp(haystack, needle, needle_size)) return haystack;
if (needlelen <= haystacklen)
{
if (!memcmp(hs, nd, needlelen)) return (void*)const_cast<char*>(hs);
}
else { return NULL; }
}
haystack++;
haystack_size--;
hs++;
haystacklen--;
}
return NULL;
}