1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 13:55:08 +00:00

Kernel: Move {strnlen, strcmp, memcmp, strncmp, strstr} to MiniStdLib

This lets the Prekernel also use these simple (and standalone)
C functions.
This commit is contained in:
Idan Horowitz 2022-03-25 00:01:46 +02:00 committed by Linus Groh
parent c02e6f991a
commit 54a12d34eb
2 changed files with 59 additions and 59 deletions

View file

@ -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<char*>(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));