mirror of
https://github.com/dlang/phobos.git
synced 2025-05-02 08:00:48 +03:00
Merge pull request #6034 from n8sh/fillWithMemcpy-memset
Fix Issue 18239: fillWithMemcpy use memset when T.sizeof == 1
This commit is contained in:
commit
f760414f76
1 changed files with 22 additions and 1 deletions
|
@ -895,7 +895,17 @@ nothrow @safe @nogc unittest
|
||||||
assertThrown(make!InvalidImpureStruct(Mallocator.instance, 42));
|
assertThrown(make!InvalidImpureStruct(Mallocator.instance, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fillWithMemcpy(T)(void[] array, auto ref T filler) nothrow
|
private void fillWithMemcpy(T)(scope void[] array, auto ref T filler) nothrow
|
||||||
|
if (T.sizeof == 1)
|
||||||
|
{
|
||||||
|
import core.stdc.string : memset;
|
||||||
|
import std.traits : CopyConstness;
|
||||||
|
if (!array.length) return;
|
||||||
|
memset(array.ptr, *cast(CopyConstness!(T*, ubyte*)) &filler, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillWithMemcpy(T)(scope void[] array, auto ref T filler) nothrow
|
||||||
|
if (T.sizeof != 1)
|
||||||
{
|
{
|
||||||
import core.stdc.string : memcpy;
|
import core.stdc.string : memcpy;
|
||||||
import std.algorithm.comparison : min;
|
import std.algorithm.comparison : min;
|
||||||
|
@ -910,6 +920,17 @@ private void fillWithMemcpy(T)(void[] array, auto ref T filler) nothrow
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@system unittest
|
||||||
|
{
|
||||||
|
// Test T.sizeof == 1 path of fillWithMemcpy.
|
||||||
|
ubyte[] a;
|
||||||
|
fillWithMemcpy(a, ubyte(42));
|
||||||
|
assert(a.length == 0);
|
||||||
|
a = [ 1, 2, 3, 4, 5 ];
|
||||||
|
fillWithMemcpy(a, ubyte(42));
|
||||||
|
assert(a == [ 42, 42, 42, 42, 42]);
|
||||||
|
}
|
||||||
|
|
||||||
@system unittest
|
@system unittest
|
||||||
{
|
{
|
||||||
int[] a;
|
int[] a;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue