diff --git a/libc/include/string.h b/libc/include/string.h index aff7a35b..19f835c4 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -1,4 +1,4 @@ -/* string.h: String manipulation. */ +/* string.h: String and memory manipulation. */ #ifndef _STRING_H #define _STRING_H @@ -89,6 +89,12 @@ extern "C" /* Locate a string (needle) in another one (haystack). */ char* strstr(const char* haystack, const char* needle); + /* Compare two null-terminated strings, ignoring case. */ + int strcasecmp(const char* a, const char* b); + + /* Compare two fixed-size null-terminated strings, ignoring case. */ + int strncasecmp(const char* a, const char* b, size_t max); + #ifdef __cplusplus } #endif diff --git a/libc/include/strings.h b/libc/include/strings.h new file mode 100644 index 00000000..def7de10 --- /dev/null +++ b/libc/include/strings.h @@ -0,0 +1,23 @@ +/* strings.h: String operations. */ + +#ifndef _STRINGS_H +#define _STRINGS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Compare two null-terminated strings, ignoring case. */ + int strcasecmp(const char* a, const char* b); + + /* Compare two fixed-size null-terminated strings, ignoring case. */ + int strncasecmp(const char* a, const char* b, size_t max); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libluna/include/luna/CString.h b/libluna/include/luna/CString.h index 13ff6855..b7ee966f 100644 --- a/libluna/include/luna/CString.h +++ b/libluna/include/luna/CString.h @@ -7,7 +7,7 @@ extern "C" void* memset(void* buf, int c, usize n); int memcmp(const void* a, const void* b, usize n); void* memmove(void* dest, const void* src, usize n); - void* memchr(const void* buf, int c, size_t n); + void* memchr(const void* buf, int c, usize n); usize strlen(const char* str); usize strnlen(const char* str, usize max); @@ -41,4 +41,7 @@ extern "C" char* strpbrk(const char* s, const char* accept); char* strstr(const char* haystack, const char* needle); + + int strcasecmp(const char* a, const char* b); + int strncasecmp(const char* a, const char* b, usize max); } diff --git a/libluna/src/CString.cpp b/libluna/src/CString.cpp index 3dcc4581..fc66de79 100644 --- a/libluna/src/CString.cpp +++ b/libluna/src/CString.cpp @@ -1,5 +1,6 @@ #include #include +#include extern "C" { @@ -297,4 +298,25 @@ extern "C" return nullptr; } + + int strcasecmp(const char* a, const char* b) + { + while (*a && (_tolower(*a) == _tolower(*b))) + { + a++; + b++; + } + return _tolower(*(const u8*)a) - _tolower(*(const u8*)b); + } + + int strncasecmp(const char* a, const char* b, usize max) + { + const char* s = a; + while (*a && (_tolower(*a) == _tolower(*b)) && (usize)(a - s) < (max - 1)) + { + a++; + b++; + } + return _tolower(*(const u8*)a) - _tolower(*(const u8*)b); + } }