Luna/luna/String.cpp

47 lines
1.1 KiB
C++
Raw Normal View History

2022-11-19 14:43:09 +00:00
#include <String.h>
extern "C"
{
void* memcpy(void* dest, const void* src, size_t n)
{
for (size_t i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); }
return dest;
}
void* memset(void* buf, int c, size_t n)
{
for (size_t i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; }
return buf;
}
int memcmp(const void* a, const void* b, size_t n)
{
if (!n) return 0;
const u8* ap = (const u8*)a;
const u8* bp = (const u8*)b;
while (--n && *ap == *bp)
{
ap++;
bp++;
}
return *ap - *bp;
}
void* memmove(void* dest, const void* src, size_t n)
{
if (dest == src) return dest;
if (dest > src)
for (long i = n - 1; i >= 0; i++) { *((u8*)dest + i) = *((const u8*)src + i); }
else
for (long i = 0; i < (long)n; i++) { *((u8*)dest + i) = *((const u8*)src + i); }
return dest;
}
size_t strlen(const char* str)
{
const char* i = str;
for (; *i; ++i)
;
return (i - str);
}
}