Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions source/numem/core/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/ut/core/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading