2022-12-16 19:37:57 +00:00
|
|
|
#include <luna/Alloc.h>
|
2022-12-16 19:40:04 +00:00
|
|
|
#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)
|
2022-12-07 10:47:46 +00:00
|
|
|
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)
|
|
|
|
;
|
2022-12-07 10:47:46 +00:00
|
|
|
return (usize)(i - str);
|
2022-11-19 14:43:09 +00:00
|
|
|
}
|
2022-12-16 19:37:57 +00:00
|
|
|
|
2023-01-22 10:27:37 +00:00
|
|
|
usize strnlen(const char* str, usize max)
|
|
|
|
{
|
|
|
|
const char* i = str;
|
|
|
|
for (; max, *i; ++i, --max)
|
|
|
|
;
|
|
|
|
return (usize)(i - str);
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:20:56 +00:00
|
|
|
int strcmp(const char* a, const char* b)
|
|
|
|
{
|
|
|
|
while (*a && (*a == *b))
|
|
|
|
{
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
return *(const u8*)a - *(const u8*)b;
|
|
|
|
}
|
|
|
|
|
2022-12-18 13:33:13 +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);
|
|
|
|
|
2023-01-22 10:27:37 +00:00
|
|
|
char* dest = (char*)calloc_impl(len + 1, 1).value_or(nullptr);
|
|
|
|
if (!dest) return nullptr;
|
|
|
|
|
|
|
|
memcpy(dest, str, len);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* strndup(const char* str, usize max)
|
|
|
|
{
|
|
|
|
const usize len = strnlen(str, max);
|
|
|
|
|
|
|
|
char* dest = (char*)calloc_impl(len + 1, 1).value_or(nullptr);
|
2022-12-16 19:37:57 +00:00
|
|
|
if (!dest) return nullptr;
|
|
|
|
|
2023-01-22 10:27:37 +00:00
|
|
|
memcpy(dest, str, len);
|
2022-12-16 19:37:57 +00:00
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2022-12-18 15:31:02 +00:00
|
|
|
|
|
|
|
void nullcpy(char* dest, const char* src, usize len)
|
|
|
|
{
|
|
|
|
memcpy(dest, src, len);
|
|
|
|
dest[len] = 0;
|
|
|
|
}
|
2023-01-06 15:20:35 +00:00
|
|
|
|
|
|
|
char* strcpy(char* dest, const char* src)
|
|
|
|
{
|
|
|
|
char* s = dest;
|
|
|
|
for (; *src; dest++, src++) *dest = *src;
|
|
|
|
*dest = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* strcat(char* dest, const char* src)
|
|
|
|
{
|
|
|
|
char* s = dest;
|
|
|
|
while (*dest) dest++;
|
|
|
|
for (; *src; dest++, src++) *dest = *src;
|
|
|
|
*dest = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* strchr(const char* str, int c)
|
|
|
|
{
|
|
|
|
while (*str && *str != c) str++;
|
|
|
|
if (*str) return const_cast<char*>(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-02 12:07:29 +00:00
|
|
|
}
|