Luna/libluna/src/CString.cpp

324 lines
7.4 KiB
C++

#include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/CType.h>
extern "C"
{
void* memcpy(void* dest, const void* src, usize n)
{
for (usize i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); }
return dest;
}
void* memset(void* buf, int c, usize n)
{
for (usize i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; }
return buf;
}
void* memchr(const void* buf, int c, usize n)
{
for (usize i = 0; i < n; ++i)
{
if (*((const u8*)buf + i) == (u8)c) return const_cast<void*>(buf);
}
return nullptr;
}
int memcmp(const void* a, const void* b, usize 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, usize n)
{
if (dest == src) return dest;
if (dest > src)
for (long i = (long)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;
}
usize strlen(const char* str)
{
const char* i = str;
for (; *i; ++i)
;
return (usize)(i - str);
}
usize strnlen(const char* str, usize max)
{
const char* i = str;
for (; *i && max; ++i, --max)
;
return (usize)(i - str);
}
int strcmp(const char* a, const char* b)
{
while (*a && (*a == *b))
{
a++;
b++;
}
return *(const u8*)a - *(const u8*)b;
}
int wcscmp(const wchar_t* a, const wchar_t* b)
{
while (*a && (*a == *b))
{
a++;
b++;
}
static_assert(sizeof(wchar_t) == sizeof(u32));
return *(const u32*)a - *(const u32*)b;
}
int strncmp(const char* a, const char* b, usize max)
{
const char* s = a;
while (*a && (*a == *b) && (usize)(a - s) < (max - 1))
{
a++;
b++;
}
return *(const u8*)a - *(const u8*)b;
}
usize wcslen(const wchar_t* str)
{
const wchar_t* i = str;
for (; *i; ++i)
;
return (usize)(i - str);
}
char* strdup(const char* str)
{
const usize len = strlen(str);
char* dest = (char*)calloc_impl(len + 1, 1, false).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, false).value_or(nullptr);
if (!dest) return nullptr;
memcpy(dest, str, len);
return dest;
}
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* strncpy(char* dest, const char* src, usize max)
{
usize i;
for (i = 0; i < max && src[i] != 0; i++) dest[i] = src[i];
for (; i < max; i++) dest[i] = 0;
return dest;
}
char* strncat(char* dest, const char* src, usize max)
{
usize len = strlen(dest);
usize i;
for (i = 0; i < max && *(src + i); i++) *(dest + len + i) = *(src + i);
*(dest + len + i) = '\0';
return dest;
}
char* strchr(const char* str, int c)
{
while (*str && *str != c) str++;
if (*str) return const_cast<char*>(str);
return NULL;
}
char* strrchr(const char* str, int c)
{
const char* s = str + strlen(str);
while (s != str && *s != (char)c) s--;
if (s != str) return const_cast<char*>(s);
if (*s == (char)c) return const_cast<char*>(s);
return NULL;
}
usize strlcpy(char* dest, const char* src, usize len)
{
usize src_len = strlen(src);
usize copy_len = src_len;
if (len == 0) return src_len;
if (src_len >= (len - 1)) copy_len = len - 1;
memcpy(dest, src, copy_len);
dest[copy_len] = 0;
return src_len;
}
usize strcspn(const char* str, const char* reject)
{
const char* s = str;
while (*s)
{
const char* rp = reject;
while (*rp)
{
if (*s == *rp) return (usize)(s - str);
rp++;
}
s++;
}
return (usize)(s - str);
}
usize strspn(const char* str, const char* accept)
{
const char* s = str;
while (*s)
{
const char* ap = accept;
bool match = false;
while (*ap)
{
if (*s == *ap)
{
match = true;
break;
}
ap++;
}
if (!match) return (usize)(s - str);
s++;
}
return (usize)(s - str);
}
char* strtok_r(char* str, const char* delim, char** savep)
{
auto& s = *savep;
if (str) s = str;
if (!s) return nullptr;
if (*s)
{
usize skip = strspn(s, delim);
s += skip;
if (*s == 0) return nullptr;
usize use = strcspn(s, delim);
char* result = s;
if (s[use] != 0)
{
s[use] = 0;
s += (use + 1);
}
else { s = nullptr; }
return result;
}
return nullptr;
}
char* strtok(char* str, const char* delim)
{
static char* s;
return strtok_r(str, delim, &s);
}
char* strpbrk(const char* s, const char* accept)
{
usize index = strcspn(s, accept);
const char* ptr = s + index;
if (*ptr) return const_cast<char*>(ptr);
return nullptr;
}
char* strstr(const char* haystack, const char* needle)
{
const char* i = haystack; // keeps track of the current haystack position
const char* j = needle; // keeps track of the current needle position
const char* k = haystack; // keeps track of the first matched character
while (*i)
{
if (*j == 0) return const_cast<char*>(k);
if (*i != *j)
{
j = needle;
k = i + 1;
}
else
j++;
i++;
}
if (*j == 0) return const_cast<char*>(k);
return nullptr;
}
int strcasecmp(const char* a, const char* b)
{
while (*a && (_tolower(*a) == _tolower(*b)))
{
a++;
b++;
}
return _tolower(*(const u8*)a) - _tolower(*(const u8*)b);
}
int strncasecmp(const char* a, const char* b, usize max)
{
const char* s = a;
while (*a && (_tolower(*a) == _tolower(*b)) && (usize)(a - s) < (max - 1))
{
a++;
b++;
}
return _tolower(*(const u8*)a) - _tolower(*(const u8*)b);
}
}