Merge pull request #6034 from n8sh/fillWithMemcpy-memset

Fix Issue 18239: fillWithMemcpy use memset when T.sizeof == 1
This commit is contained in:
Sebastian Wilzbach 2018-01-17 22:36:32 +01:00 committed by GitHub
commit f760414f76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -895,7 +895,17 @@ nothrow @safe @nogc unittest
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 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
{
int[] a;