From 54a12d34eb3acd5ceb29fb9c49d429a4cb0ee2f7 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 25 Mar 2022 00:01:46 +0200 Subject: [PATCH] Kernel: Move {strnlen, strcmp, memcmp, strncmp, strstr} to MiniStdLib This lets the Prekernel also use these simple (and standalone) C functions. --- Kernel/MiniStdLib.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ Kernel/StdLib.cpp | 59 ------------------------------------------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/Kernel/MiniStdLib.cpp b/Kernel/MiniStdLib.cpp index 8995094f7e..0bd0768b5b 100644 --- a/Kernel/MiniStdLib.cpp +++ b/Kernel/MiniStdLib.cpp @@ -102,4 +102,63 @@ size_t strlen(const char* str) ++len; return len; } + +size_t strnlen(const char* str, size_t maxlen) +{ + size_t len = 0; + for (; len < maxlen && *str; str++) + len++; + return len; +} + +int strcmp(const char* s1, const char* s2) +{ + for (; *s1 == *s2; ++s1, ++s2) { + if (*s1 == 0) + return 0; + } + return *(const u8*)s1 < *(const u8*)s2 ? -1 : 1; +} + +int memcmp(const void* v1, const void* v2, size_t n) +{ + auto const* s1 = (const u8*)v1; + auto const* s2 = (const u8*)v2; + while (n-- > 0) { + if (*s1++ != *s2++) + return s1[-1] < s2[-1] ? -1 : 1; + } + return 0; +} + +int strncmp(const char* s1, const char* s2, size_t n) +{ + if (!n) + return 0; + do { + if (*s1 != *s2++) + return *(const unsigned char*)s1 - *(const unsigned char*)--s2; + if (*s1++ == 0) + break; + } while (--n); + return 0; +} + +char* strstr(const char* haystack, const char* needle) +{ + char nch; + char hch; + + if ((nch = *needle++) != 0) { + size_t len = strlen(needle); + do { + do { + if ((hch = *haystack++) == 0) + return nullptr; + } while (hch != nch); + } while (strncmp(haystack, needle, len) != 0); + --haystack; + } + return const_cast(haystack); +} } diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index 6ab298649b..0f3c21c249 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -225,65 +225,6 @@ const void* memmem(const void* haystack, size_t haystack_length, const void* nee return AK::memmem(haystack, haystack_length, needle, needle_length); } -size_t strnlen(const char* str, size_t maxlen) -{ - size_t len = 0; - for (; len < maxlen && *str; str++) - len++; - return len; -} - -int strcmp(const char* s1, const char* s2) -{ - for (; *s1 == *s2; ++s1, ++s2) { - if (*s1 == 0) - return 0; - } - return *(const u8*)s1 < *(const u8*)s2 ? -1 : 1; -} - -int memcmp(const void* v1, const void* v2, size_t n) -{ - auto const* s1 = (const u8*)v1; - auto const* s2 = (const u8*)v2; - while (n-- > 0) { - if (*s1++ != *s2++) - return s1[-1] < s2[-1] ? -1 : 1; - } - return 0; -} - -int strncmp(const char* s1, const char* s2, size_t n) -{ - if (!n) - return 0; - do { - if (*s1 != *s2++) - return *(const unsigned char*)s1 - *(const unsigned char*)--s2; - if (*s1++ == 0) - break; - } while (--n); - return 0; -} - -char* strstr(const char* haystack, const char* needle) -{ - char nch; - char hch; - - if ((nch = *needle++) != 0) { - size_t len = strlen(needle); - do { - do { - if ((hch = *haystack++) == 0) - return nullptr; - } while (hch != nch); - } while (strncmp(haystack, needle, len) != 0); - --haystack; - } - return const_cast(haystack); -} - // Functions that are automatically called by the C++ compiler. // Declare them first, to tell the silly compiler that they are indeed being used. [[noreturn]] void __stack_chk_fail() __attribute__((used));