Implement most of the string.c functions.
This commit is contained in:
25
src/string.c
25
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. */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user