2022-12-25 08:58:00 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
int LIBCLAYER(memcmp)(const void *s1, const void *s2, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
for (const unsigned char *p1 = s1, *p2 = s2; n--; ++p1, ++p2) {
|
|
|
|
if (*p1 != *p2) return *p1 - *p2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
void *LIBCLAYER(memcpy)(void *dest, const void *src, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *dest_cell = dest;
|
|
|
|
char *src_cell = (char*)src;
|
|
|
|
while (n--) *dest_cell++ = *src_cell++;
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
void *LIBCLAYER(memmove)(void *dest, const void *src, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *dest_cell = dest;
|
|
|
|
char *src_cell = (char*)src;
|
|
|
|
if (dest_cell <= src_cell) {
|
|
|
|
while (n--) *dest_cell++ = *src_cell++;
|
|
|
|
} else {
|
|
|
|
dest_cell += n;
|
|
|
|
src_cell += n;
|
|
|
|
while (n--) *--dest_cell = *--src_cell;
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
void *LIBCLAYER(memchr)(const void *s, int c, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
for (const unsigned char *p = s; n--; ++p) {
|
|
|
|
if ((unsigned char)c == *p) return (void*)p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
void *LIBCLAYER(memset)(void *s, int c, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *ss = s;
|
|
|
|
while (n--) *ss++ = c;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
char *LIBCLAYER(strcat)(char *dest, const char *src)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *const dest_start = dest;
|
|
|
|
while (*dest) ++dest;
|
|
|
|
while ((*dest++ = *src++));
|
|
|
|
return dest_start;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
char *LIBCLAYER(strchr)(const char *s, int c)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
for (; *s != (char)c; ++s) if (*s == '\0') return NULL;
|
|
|
|
return (char*)s;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
int LIBCLAYER(strcmp)(const char *s1, const char *s2)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
for (; *s1; ++s1, ++s2) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
char *LIBCLAYER(strcpy)(char *dest, const char *src)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
while ((*dest++ = *src++) != '\0');
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
size_t LIBCLAYER(strlen)(const char *s)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
const char *ss = s;
|
|
|
|
while (*ss != '\0') ++ss;
|
|
|
|
return ss - s;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
char *LIBCLAYER(strncat)(char *dest, const char *src, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *const dest_start = dest;
|
|
|
|
if (n) {
|
|
|
|
while (*dest) ++dest;
|
|
|
|
while ((*dest++ = *src++)) {
|
|
|
|
if (--n == 0) {
|
|
|
|
*dest = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dest_start;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
int LIBCLAYER(strncmp)(const char *s1, const char *s2, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
for (; *s1 && n; ++s1, ++s2, --n) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
char *LIBCLAYER(strncpy)(char *dest, const char *src, size_t n)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
while (n-- && (*dest++ = *src++) != '\0');
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
size_t LIBCLAYER(strnlen)(const char *s, size_t maxlen)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
|
|
|
const char *ss = s;
|
|
|
|
while (*ss != '\0' && maxlen--) ++ss;
|
|
|
|
return ss - s;
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
char *LIBCLAYER(strstr)(const char *haystack, const char *needle)
|
2022-12-25 08:58:00 +00:00
|
|
|
{
|
2022-12-25 11:23:09 +00:00
|
|
|
const size_t needle_slen = LIBCLAYER(strlen)(needle);
|
2022-12-25 08:58:00 +00:00
|
|
|
if (!needle_slen) return (char*)haystack;
|
|
|
|
|
2022-12-25 11:23:09 +00:00
|
|
|
size_t haystack_slen = LIBCLAYER(strlen)(haystack);
|
2022-12-25 08:58:00 +00:00
|
|
|
while (haystack_slen >= needle_slen) {
|
|
|
|
--haystack_slen;
|
2022-12-25 11:23:09 +00:00
|
|
|
if (!LIBCLAYER(memcmp)(haystack, needle, needle_slen)) {
|
|
|
|
return (char*)haystack;
|
|
|
|
}
|
2022-12-25 08:58:00 +00:00
|
|
|
++haystack;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|