Skip to content

Instantly share code, notes, and snippets.

@tom-tan
Last active December 23, 2015 12:19
Show Gist options
  • Save tom-tan/6634316 to your computer and use it in GitHub Desktop.
Save tom-tan/6634316 to your computer and use it in GitHub Desktop.
memmove in D
void* memmove(void* dest_, const void* src_, size_t n)
{
auto dest = (cast(byte*)dest_)[0..n];
auto src = (cast(byte*)src_)[0..n];
if (dest.ptr < src.ptr || (src.ptr < dest.ptr && src.ptr+n < dest.ptr))
{
// move forward order
foreach(i; 0..n)
{
dest[i] = src[i];
}
}
else if (src.ptr < dest.ptr && src.ptr+n >= dest.ptr)
{
// move backward order
foreach_reverse(i; 0..n)
{
dest[i] = src[i];
}
}
return dest_;
}
void move(T)(ref T src, ref T dest)
{
auto tmp = T.init;
memmove(&dest, &src, T.sizeof);
memmove(&src, &tmp, T.sizeof);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment