diff --git a/LibC/string.cpp b/LibC/string.cpp index 43e545852f5..1f36864a77d 100644 --- a/LibC/string.cpp +++ b/LibC/string.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "ctype.h" extern "C" { @@ -91,6 +92,35 @@ int strncmp(const char* s1, const char* s2, size_t n) return 0; } +int strcasecmp(const char* s1, const char* s2) +{ + int c1, c2; + for (;;) { + c1 = tolower(*s1++); + c2 = tolower(*s2++); + if (c1 == 0 || c1 != c2) { + return c1 - c2; + } + } +} + +int strncasecmp(const char* s1, const char* s2, size_t n) +{ + if (n == 0) { + return 0; + } + + while (n-- != 0 && tolower(*s1) == tolower(*s2)) { + if (n == 0 || *s1 == '\0' || *s2 == '\0') { + break; + } + s1++; + s2++; + } + + return tolower(*s1) - tolower(*s2); +} + int memcmp(const void* v1, const void* v2, size_t n) { auto* s1 = (const uint8_t*)v1; diff --git a/LibC/string.h b/LibC/string.h index 4f168c39d1e..b71f6c602c9 100644 --- a/LibC/string.h +++ b/LibC/string.h @@ -8,6 +8,8 @@ __BEGIN_DECLS size_t strlen(const char*); int strcmp(const char*, const char*); int strncmp(const char*, const char*, size_t); +int strcasecmp(const char*, const char*); +int strncasecmp(const char*, const char*, size_t); int memcmp(const void*, const void*, size_t); void* memcpy(void*, const void*, size_t); void* memmove(void*, const void*, size_t);