learncrypto/source/utility.d

29 lines
813 B
D
Raw Normal View History

2016-07-05 20:34:52 +02:00
module utility;
auto concat(T : E[n], E, size_t n)(in E[][] args...) @nogc
{
size_t offset = 0;
T result = void;
2016-08-01 01:42:29 +02:00
foreach (arr; args)
{
result[offset .. offset + arr.length] = arr;
2016-07-05 20:34:52 +02:00
offset += arr.length;
}
assert(offset == result.length);
return result;
}
@safe unittest
{
assert(concat!(int[0])() == []);
assert(concat!(int[0])([]) == []);
assert(concat!(int[0])([], []) == []);
assert(concat!(int[1])([1]) == [1]);
assert(concat!(int[1])([1], []) == [1]);
assert(concat!(int[1])([], [1]) == [1]);
assert(concat!(int[2])([1, 2]) == [1, 2]);
assert(concat!(int[2])([1], [2]) == [1, 2]);
assert(concat!(int[6])([1], [2, 3], [4, 5, 6]) == [1, 2, 3, 4, 5, 6]);
assert(concat!(char[12])("Hello", " ", "World!") == "Hello World!");
}