diff --git a/source/numem/core/memory.d b/source/numem/core/memory.d index 8d0474b..8b0e966 100644 --- a/source/numem/core/memory.d +++ b/source/numem/core/memory.d @@ -229,6 +229,9 @@ T[] nu_reverse(T)(auto ref T[] range) @nogc nothrow { /** Creates a shallow duplicate of the given buffer. + `nu_dup` is preserving the constness qualifiers. + If you're looking for a mutable duplicate, see also `nu_mdup`. + Params: buffer = Buffer to duplicate. @@ -267,6 +270,25 @@ immutable(T)[] nu_idup(T)(inout(T)[] buffer) @trusted @nogc nothrow pure { return cast(immutable(T)[])nu_dup(buffer); } +/** + Creates a shallow mutable duplicate of the given buffer. + + Params: + buffer = Buffer to duplicate. + + Memorysafety: + This function copies data out of the slice into a new + memory allocation; as such it has to be freed. + It is otherwise safe, in that it won't modify + the original memory provided. + + Returns: + Duplicated slice, must be freed with $(D nu_resize) +*/ +T[] nu_mdup(T)(inout(T)[] buffer) @trusted @nogc nothrow pure { + return cast(T[])nu_dup(buffer); +} + /** Gets the retain function for type T and calls it. */ diff --git a/tests/ut/core/memory.d b/tests/ut/core/memory.d index 2976975..bff1b75 100644 --- a/tests/ut/core/memory.d +++ b/tests/ut/core/memory.d @@ -11,11 +11,14 @@ unittest { @("nu_dup") unittest { + char[] str0 = "Hello, world!".nu_mdup(); const(char)[] str1 = "Hello, world!".nu_dup(); immutable(char)[] str2 = "Hello, world!".nu_idup(); + assert(str0 == "Hello, world!"); assert(str1 == "Hello, world!"); assert(str2 == "Hello, world!"); + assert(!str0.nu_resize(0).ptr); assert(!str1.nu_resize(0).ptr); assert(!str2.nu_resize(0).ptr); }