#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 (; *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++; } return *(const u8*)a - *(const u8*)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(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(s); if (*s == (char)c) return const_cast(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(char* str, const char* delim) { static char* s = nullptr; 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* strpbrk(const char* s, const char* accept) { usize index = strcspn(s, accept); const char* ptr = s + index; if (*ptr) return const_cast(ptr); return nullptr; } }