Implement most of the string.c functions.

This commit is contained in:
2025-12-23 21:42:48 +01:00
parent 69a8ee7115
commit 65d135d524

View File

@@ -1,22 +1,33 @@
#include "string.h" #include "string.h"
int memcmp(const void *s1, const void *s2, usize n) int memcmp(const void *m1, const void *m2, usize n)
{ {
/* TODO: Compare n bytes of s1 with s2, performing *s1-*s2. const u8 *s1 = m1, *s2 = m2;
* Returning if non-zero or out of bounds. */
int v = 0;
while (n > 0 && !(v = *s1 - *s2))
s1++, s2++, n--;
return v;
} }
void *memset(void *s, int c, usize n) void *memset(void *m, int c, usize n)
{ {
/* TODO: Fill memory with c */ u8 *s = m;
for (; n > 0; n--)
*(s++) = c;
return m;
} }
void *memcpy(void *restrict dst, const void *restrict src, usize n) void *memcpy(void *restrict dst, const void *restrict src, usize n)
{ {
/* TODO: Copies memory area, not allowing overlap */ const u8 *ssrc = src;
u8 *sdst = dst;
for (; n > 0; n--)
*(sdst++) = *(ssrc++);
return dst;
} }
void *memmove(void *dst, const void *src, usize n) void *memmove(void *dst, const void *src, usize n)
{ {
/* TODO: Moves memory area, allowing overlap */ /* TODO: Moves memory area, allowing overlap. As though an array is used. */
} }