mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 13:35:07 +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:
parent
c02e6f991a
commit
54a12d34eb
2 changed files with 59 additions and 59 deletions
|
@ -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<char*>(haystack);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue