Luna/luna/src/CString.cpp

68 lines
1.5 KiB
C++
Raw Normal View History

2022-12-16 19:37:57 +00:00
#include <luna/Alloc.h>
#include <luna/CString.h>
2022-11-19 14:43:09 +00:00
extern "C"
{
2022-11-20 17:29:23 +00:00
void* memcpy(void* dest, const void* src, usize n)
2022-11-19 14:43:09 +00:00
{
2022-11-20 17:29:23 +00:00
for (usize i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); }
2022-11-19 14:43:09 +00:00
return dest;
}
2022-11-20 17:29:23 +00:00
void* memset(void* buf, int c, usize n)
2022-11-19 14:43:09 +00:00
{
2022-11-20 17:29:23 +00:00
for (usize i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; }
2022-11-19 14:43:09 +00:00
return buf;
}
2022-11-20 17:29:23 +00:00
int memcmp(const void* a, const void* b, usize n)
2022-11-19 14:43:09 +00:00
{
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;
}
2022-11-20 17:29:23 +00:00
void* memmove(void* dest, const void* src, usize n)
2022-11-19 14:43:09 +00:00
{
if (dest == src) return dest;
if (dest > src)
for (long i = (long)n - 1; i >= 0; i++) { *((u8*)dest + i) = *((const u8*)src + i); }
2022-11-19 14:43:09 +00:00
else
for (long i = 0; i < (long)n; i++) { *((u8*)dest + i) = *((const u8*)src + i); }
return dest;
}
2022-11-20 17:29:23 +00:00
usize strlen(const char* str)
2022-11-19 14:43:09 +00:00
{
const char* i = str;
for (; *i; ++i)
;
return (usize)(i - str);
2022-11-19 14:43:09 +00:00
}
2022-12-16 19:37:57 +00:00
usize wcslen(const wchar_t* str)
{
const wchar_t* i = str;
for (; *i; ++i)
;
return (usize)(i - str);
}
2022-12-16 19:37:57 +00:00
char* strdup(const char* str)
{
const usize len = strlen(str);
char* dest = (char*)raw_malloc(len + 1);
if (!dest) return nullptr;
memcpy(dest, str, len + 1);
return dest;
}
2022-11-19 14:43:09 +00:00
}