implement memmove
This commit is contained in:
16
src/string.c
16
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)
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user