string.h OCD

This commit is contained in:
bumbread 2022-08-10 14:13:47 +11:00
parent 0abfd8cf3d
commit 23a025787a
2 changed files with 62 additions and 53 deletions

View File

@ -1,67 +1,71 @@
void *memcpy(void *restrict s1, const void *restrict s2, size_t n) { void *memcpy(void *restrict to, void const *restrict from, size_t n) {
const u8 *restrict c2 = s2; u8 *restrict dst = to;
u8 *restrict c1 = s1; u8 const *restrict src = from;
while (n--) { while (n--) {
*c1++ = *c2++; *dst++ = *src++;
} }
return s1; return dst;
} }
void *memmove(void *s1, const void *s2, size_t n) { void *memmove(void *to, const void *from, size_t n) {
u8* c1 = s1; u8 *dst = to;
const u8* c2 = s2; u8 const *src = from;
if (c1 != c2) { if (src != dst) {
if (c1 < c2) { if (src < dst) {
// reverse copy // reverse copy
for (size_t i = n; i--;) c1[i] = c2[i]; for (size_t i = n; i--;) dst[i] = src[i];
} else { } else {
// normal copy // normal copy
for (size_t i = 0; i < n; i++) c1[i] = c2[i]; for (size_t i = 0; i < n; i++) dst[i] = src[i];
} }
} }
return dst;
}
return s1; char *strcpy(char *restrict dst, char const *restrict src) {
}
char *strcpy(char *restrict s1, const char *restrict s2) {
while(*s2 != 0) {
*s1++ = *s2++;
}
*s1 = 0;
return s1;
}
char *strncpy(char *restrict s1, const char *restrict s2, size_t n) {
size_t i = 0; size_t i = 0;
for(; i < n && *s2 != 0; ++ i) { for(i = 0; src[i]; ++i) {
*s1++ = *s2++; dst[i] = src[i];
} }
for(; i < n; ++i) { dst[i] = 0;
*s1++ = 0; return dst;
} }
return s1;
char *strncpy(char *restrict dst, char const *restrict src, size_t n) {
size_t i;
for(i = 0; i != n && src[i]; ++i) {
dst[i] = src[i];
}
for(; i != n; ++i) {
dst[i] = 0;
}
return dst;
} }
char *strcat(char *restrict s1, const char *restrict s2) { char *strcat(char *restrict dst, const char *restrict src) {
size_t start = strlen(s1); size_t start = strlen(dst);
return strcpy(s1+start, s2); return strcpy(dst+start, src);
} }
char *strncat(char *restrict s1, const char *restrict s2, size_t n) { char *strncat(char *restrict dst, char const *restrict src, size_t n) {
size_t start = strlen(s1); size_t start = strlen(dst);
strncpy(s1+start, s2, n); strncpy(dst+start, src, n);
return s1; return dst;
} }
int memcmp(const void *s1, const void *s2, size_t n) { int memcmp(void const *p1, void const *p2, size_t n) {
const u8 *u1 = s1; u8 const *s1 = p1;
const u8 *u2 = s2; u8 const *s2 = p2;
for (; n--; u1++, u2++) { size_t i;
if (*u1 != *u2) return *u1 - *u2; for(i = 0; i != n; ++i) {
if(s1[i] != s2[i]) break;
}
if(i != n) {
if(s1[i] < s2[i]) return -1;
if(s1[i] > s2[i]) return 1;
} }
return 0; return 0;
} }
@ -74,21 +78,24 @@ void *memset(void *s, int c, size_t n) {
return s; return s;
} }
int strcmp(const char *s1, const char *s2) { int strcmp(char const *s1, char const *s2) {
int diff; size_t i;
do { for(i = 0; s1[i] && s2[i]; ++i) {
diff = *s1 - *s2; if(s1[i] != s2[i]) break;
} while(diff == 0 && *s1++ != 0 && *s2++ != 0); }
return diff; if(s1[i] < s2[i]) return -1;
if(s1[i] > s2[i]) return +1;
return 0;
} }
int strncmp(const char *s1, const char *s2, size_t n) { int strncmp(const char *s1, const char *s2, size_t n) {
int diff = 0; size_t i;
size_t i = 0; for(i = 0; i != n && s1[i] && s2[i]; ++i) {
if(n != 0) do { if(s1[i] != s2[i]) break;
diff = *s1 - *s2; }
} while(++i < n && diff != 0 && *s1 != 0 && *s2 != 0); if(s1[i] < s2[i]) return -1;
return diff; if(s1[i] > s2[i]) return +1;
return 0;
} }
int strcoll(const char *s1, const char *s2) { int strcoll(const char *s1, const char *s2) {

View File

@ -38,6 +38,7 @@ wchar_t *wcscpy(wchar_t *restrict dst, wchar_t const *restrict src) {
} }
wchar_t *wcsncpy(wchar_t *restrict dst, wchar_t const *restrict src, size_t n) { wchar_t *wcsncpy(wchar_t *restrict dst, wchar_t const *restrict src, size_t n) {
// TODO: more than one null terminator, not write if n was reached
size_t i; size_t i;
for(i = 0; i != n && src[i]; ++i) { for(i = 0; i != n && src[i]; ++i) {
dst[i] = src[i]; dst[i] = src[i];
@ -87,6 +88,7 @@ int wcsncmp(wchar_t const *s1, wchar_t const *s2, size_t n) {
} }
int wmemcmp(wchar_t const *s1, wchar_t const *s2, size_t n) { int wmemcmp(wchar_t const *s1, wchar_t const *s2, size_t n) {
// TODO: OOB when ranges are equal
size_t i; size_t i;
for(i = 0; i != n; ++i) { for(i = 0; i != n; ++i) {
if(s1[i] != s2[i]) break; if(s1[i] != s2[i]) break;