Kernel: add memmem
This commit is contained in:
parent
7ab80014e1
commit
397b1a2819
@ -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);
|
||||
|
@ -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<char*>(hs);
|
||||
}
|
||||
else { return NULL; }
|
||||
}
|
||||
haystack++;
|
||||
haystack_size--;
|
||||
hs++;
|
||||
haystacklen--;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user