LibC: Implement str{n}casecmp
This commit is contained in:
parent
2f4e7edee5
commit
f0f5dd6cef
Notes:
sideshowbarker
2024-07-19 14:06:58 +09:00
Author: https://github.com/rburchell Commit: https://github.com/SerenityOS/serenity/commit/f0f5dd6cef4 Pull-request: https://github.com/SerenityOS/serenity/pull/45 Reviewed-by: https://github.com/awesomekling
2 changed files with 32 additions and 0 deletions
|
@ -6,6 +6,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include "ctype.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
@ -91,6 +92,35 @@ int strncmp(const char* s1, const char* s2, size_t n)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int strcasecmp(const char* s1, const char* s2)
|
||||
{
|
||||
int c1, c2;
|
||||
for (;;) {
|
||||
c1 = tolower(*s1++);
|
||||
c2 = tolower(*s2++);
|
||||
if (c1 == 0 || c1 != c2) {
|
||||
return c1 - c2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int strncasecmp(const char* s1, const char* s2, size_t n)
|
||||
{
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
|
||||
if (n == 0 || *s1 == '\0' || *s2 == '\0') {
|
||||
break;
|
||||
}
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return tolower(*s1) - tolower(*s2);
|
||||
}
|
||||
|
||||
int memcmp(const void* v1, const void* v2, size_t n)
|
||||
{
|
||||
auto* s1 = (const uint8_t*)v1;
|
||||
|
|
|
@ -8,6 +8,8 @@ __BEGIN_DECLS
|
|||
size_t strlen(const char*);
|
||||
int strcmp(const char*, const char*);
|
||||
int strncmp(const char*, const char*, size_t);
|
||||
int strcasecmp(const char*, const char*);
|
||||
int strncasecmp(const char*, const char*, size_t);
|
||||
int memcmp(const void*, const void*, size_t);
|
||||
void* memcpy(void*, const void*, size_t);
|
||||
void* memmove(void*, const void*, size_t);
|
||||
|
|
Loading…
Add table
Reference in a new issue