This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
hilbert-os/euler/source/std/cstring.cpp

36 lines
875 B
C++

#include <cstring>
extern "C" void *memset(void *dest, int ch, size_t count) {
unsigned char c = static_cast<unsigned char>(ch);
unsigned char *d = (unsigned char *)dest;
for (size_t i = 0; i < count; ++i)
d[i] = c;
return dest;
}
extern "C" void *memcpy(void *dest, const void *src, size_t count) {
unsigned char *d = (unsigned char *)dest;
const unsigned char *s = (const unsigned char *)src;
for (size_t i = 0; i < count; ++i)
d[i] = s[i];
return dest;
}
extern "C" int strcmp(const char *lhs, const char *rhs) {
const unsigned char *l = (const unsigned char *)lhs;
const unsigned char *r = (const unsigned char *)rhs;
while (*l == *r) {
if (*l == 0)
return 0;
++l;
++r;
}
return *l < *r ? -1 : 1;
}
extern "C" size_t strlen(const char *str) {
size_t len = 0;
while (str[len] != '\0')
++len;
return len;
}