diff --git a/src/string.c b/src/string.c index e21c106..3558cd8 100644 --- a/src/string.c +++ b/src/string.c @@ -29,5 +29,19 @@ void *memcpy(void *restrict dst, const void *restrict src, usize n) void *memmove(void *dst, const void *src, usize n) { - /* TODO: Moves memory area, allowing overlap. As though an array is used. */ + const u8 *ssrc = src; + u8 *sdst = dst; + + /* If the destination is before, or equal to the source, + * we needn't worry about accidentally overriding the source + * before it is written to the destination. */ + if (sdst > ssrc) { + sdst += n; + ssrc += n; + while (n--) *(--sdst) = *(--ssrc); + } else { + while (n--) *(sdst++) = *(ssrc++); + } + + return dst; }