Luna/luna/String.h

74 lines
1.4 KiB
C
Raw Normal View History

2022-11-13 09:30:10 +00:00
#pragma once
#include <Types.h>
2022-11-13 11:20:53 +00:00
#ifndef NO_EXTERN_C
extern "C"
2022-11-13 09:30:10 +00:00
{
2022-11-13 11:20:53 +00:00
#endif
void* memcpy(void* dest, const void* src, size_t n)
#ifdef _LUNA_IMPLEMENTATION
{
for (size_t i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); }
return dest;
}
2022-11-13 09:30:10 +00:00
#else
;
#endif
2022-11-13 11:20:53 +00:00
void* memset(void* buf, int c, size_t n)
2022-11-13 09:30:10 +00:00
#ifdef _LUNA_IMPLEMENTATION
2022-11-13 11:20:53 +00:00
{
for (size_t i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; }
return buf;
}
2022-11-13 09:30:10 +00:00
#else
;
#endif
2022-11-13 11:20:53 +00:00
int memcmp(const void* a, const void* b, size_t n)
2022-11-13 09:30:10 +00:00
#ifdef _LUNA_IMPLEMENTATION
{
2022-11-13 11:20:53 +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-13 09:30:10 +00:00
}
#else
;
#endif
2022-11-13 11:20:53 +00:00
void* memmove(void* dest, const void* src, size_t n)
2022-11-13 09:30:10 +00:00
#ifdef _LUNA_IMPLEMENTATION
2022-11-13 11:20:53 +00:00
{
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;
}
2022-11-13 09:30:10 +00:00
#else
;
#endif
2022-11-13 11:20:53 +00:00
size_t strlen(const char* str)
2022-11-13 09:30:10 +00:00
#ifdef _LUNA_IMPLEMENTATION
2022-11-13 11:20:53 +00:00
{
const char* i = str;
for (; *i; ++i)
;
return (i - str);
}
2022-11-13 09:30:10 +00:00
#else
;
2022-11-13 11:20:53 +00:00
#endif
#ifndef NO_EXTERN_C
}
2022-11-13 09:30:10 +00:00
#endif