#include #include 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; } 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 (; max, *i; ++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; } 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).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); if (!dest) return nullptr; memcpy(dest, str, len); return dest; } void nullcpy(char* dest, const char* src, usize len) { memcpy(dest, src, len); dest[len] = 0; } 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(str); return NULL; } }