2022-06-02 23:36:04 +00:00
|
|
|
#include <string.h>
|
2022-06-02 13:08:59 +00:00
|
|
|
|
2022-06-02 23:36:04 +00:00
|
|
|
void *memcpy(void *restrict s1, const void *restrict s2, size_t n) {
|
2022-06-03 02:02:19 +00:00
|
|
|
const unsigned char *restrict c2 = s2;
|
|
|
|
unsigned char *restrict c1 = s1;
|
2022-06-02 23:36:04 +00:00
|
|
|
|
2022-06-03 02:02:19 +00:00
|
|
|
while (n--) {
|
|
|
|
*c1++ = *c2++;
|
|
|
|
}
|
|
|
|
return s1;
|
2022-06-02 23:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int memcmp(const void *s1, const void *s2, size_t n) {
|
2022-06-03 02:02:19 +00:00
|
|
|
const unsigned char *u1 = s1;
|
|
|
|
const unsigned char *u2 = s2;
|
2022-06-02 23:36:04 +00:00
|
|
|
|
|
|
|
for (; n--; u1++, u2++) {
|
2022-06-03 02:02:19 +00:00
|
|
|
if (*u1 != *u2) return *u1 - *u2;
|
2022-06-02 13:08:59 +00:00
|
|
|
}
|
2022-06-02 23:36:04 +00:00
|
|
|
|
2022-06-02 13:08:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-02 23:36:04 +00:00
|
|
|
|
|
|
|
void *memset(void *s, int c, size_t n) {
|
2022-06-03 02:02:19 +00:00
|
|
|
unsigned char *restrict buf = s;
|
|
|
|
while (n--) {
|
|
|
|
*buf++ = c;
|
|
|
|
}
|
|
|
|
return s;
|
2022-06-02 23:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int strcmp(const char *s1, const char *s2) {
|
2022-06-03 02:02:19 +00:00
|
|
|
while (*s1 != 0 && *s2 != 0) {
|
|
|
|
if (*s1 != *s2) break;
|
|
|
|
}
|
2022-06-02 23:36:04 +00:00
|
|
|
|
2022-06-03 02:02:19 +00:00
|
|
|
if(*s1 > *s2) return 1;
|
|
|
|
if(*s1 < *s2) return -1;
|
|
|
|
return 0;
|
2022-06-02 23:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t strlen(const char *s) {
|
2022-06-03 02:02:19 +00:00
|
|
|
size_t i = 0;
|
|
|
|
while (s[i]) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
2022-06-02 23:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// __STDC_WANT_LIB_EXT1__
|
|
|
|
size_t strnlen_s(const char *s, size_t maxsize) {
|
2022-06-06 00:16:44 +00:00
|
|
|
if (s == NULL) return 0;
|
|
|
|
|
2022-06-03 02:02:19 +00:00
|
|
|
size_t i = 0;
|
|
|
|
while (s[i] && i < maxsize) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
2022-06-02 23:36:04 +00:00
|
|
|
}
|