From 65d135d5243a3363ef7f04c02524dfe5e2c75430 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 23 Dec 2025 21:42:48 +0100 Subject: [PATCH] Implement most of the string.c functions. --- src/string.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/string.c b/src/string.c index b6a2816..69ef1d5 100644 --- a/src/string.c +++ b/src/string.c @@ -1,22 +1,33 @@ #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. - * Returning if non-zero or out of bounds. */ + const u8 *s1 = m1, *s2 = m2; + + 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) { - /* 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) { - /* TODO: Moves memory area, allowing overlap */ + /* TODO: Moves memory area, allowing overlap. As though an array is used. */ }