From 8216d09e061335f279a830d976405b0e5f9ac4d9 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 14 Dec 2023 10:59:39 +0200 Subject: [PATCH] LibC: Implement non-locale specific functions from strings.h --- libc/CMakeLists.txt | 1 + libc/strings.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 libc/strings.cpp diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 8f12027cbe..e50897e897 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 0000000000..e8d0911cd6 --- /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); +}