diff --git a/libs/libc/include/string.h b/libs/libc/include/string.h index a7697433..9f891ff6 100644 --- a/libs/libc/include/string.h +++ b/libs/libc/include/string.h @@ -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); diff --git a/libs/libc/src/string.cpp b/libs/libc/src/string.cpp index 443fee15..64e70dd0 100644 --- a/libs/libc/src/string.cpp +++ b/libs/libc/src/string.cpp @@ -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(haystack); + } + else { return NULL; } + } + haystack++; + haystack_size--; + } + return NULL; + } + #pragma GCC push_options #pragma GCC diagnostic ignored "-Wwrite-strings"