diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 8f12027c..e50897e8 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -14,6 +14,7 @@ set(LIBC_SOURCES stdio.cpp stdlib.cpp string.cpp + strings.cpp sys/banan-os.cpp sys/mman.cpp sys/stat.cpp diff --git a/libc/strings.cpp b/libc/strings.cpp new file mode 100644 index 00000000..e8d0911c --- /dev/null +++ b/libc/strings.cpp @@ -0,0 +1,30 @@ +#include +#include + +int ffs(int i) +{ + for (unsigned idx = 0; idx < sizeof(i) * 8; idx++) + if (i & (1 << idx)) + return i + 1; + return 0; +} + +int strcasecmp(const char* s1, const char* s2) +{ + const unsigned char* u1 = (unsigned char*)s1; + const unsigned char* u2 = (unsigned char*)s2; + for (; *u1 && *u2; u1++, u2++) + if (tolower(*u1) != tolower(*u2)) + break; + return tolower(*u1) - tolower(*u2); +} + +int strncasecmp(const char* s1, const char* s2, size_t n) +{ + const unsigned char* u1 = (unsigned char*)s1; + const unsigned char* u2 = (unsigned char*)s2; + for (; --n && *u1 && *u2; u1++, u2++) + if (tolower(*u1) != tolower(*u2)) + break; + return tolower(*u1) - tolower(*u2); +}