libc: Add strstr()

This commit is contained in:
apio 2022-10-15 12:33:36 +02:00
parent 3fde7e46f5
commit a002e75725
2 changed files with 25 additions and 2 deletions

View File

@ -56,6 +56,9 @@ extern "C"
/* Compares at most max bytes of the strings a and b. */
int strncmp(const char* a, const char* b, size_t max);
/* Searches for the needle string in the haystack string. */
char* strstr(const char* haystack, const char* needle);
/* Returns the error string associated with the error number err. */
char* strerror(int err);

View File

@ -40,9 +40,9 @@ extern "C"
{
if (dest == src) return dest;
if (dest > src)
for (long i = n - 1; i >= 0; i++) { *((char*)dest + i) = *((char*)src + i); }
for (long i = n - 1; i >= 0; i++) { *((char*)dest + i) = *((const char*)src + i); }
else
for (long i = 0; i < (long)n; i++) { *((char*)dest + i) = *((char*)src + i); }
for (long i = 0; i < (long)n; i++) { *((char*)dest + i) = *((const char*)src + i); }
return dest;
}
@ -138,6 +138,26 @@ extern "C"
return memset(buf, 0, n);
}
char* strstr(const char* haystack, const char* needle)
{
size_t needle_size = strlen(needle);
size_t haystack_size = strlen(haystack);
while (*haystack)
{
if (*haystack == *needle)
{
if (needle_size <= haystack_size)
{
if (!strncmp(haystack, needle, needle_size)) return const_cast<char*>(haystack);
}
else { return NULL; }
}
haystack++;
haystack_size--;
}
return NULL;
}
#pragma GCC push_options
#pragma GCC diagnostic ignored "-Wwrite-strings"