LibC: Implement str{n}casecmp

This commit is contained in:
Robin Burchell 2019-05-16 09:27:42 +02:00 committed by Andreas Kling
parent 2f4e7edee5
commit f0f5dd6cef
Notes: sideshowbarker 2024-07-19 14:06:58 +09:00
2 changed files with 32 additions and 0 deletions

View file

@ -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;

View file

@ -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);