Skip to content

Commit

Permalink
Merge pull request #3600 from dsp/master
Browse files Browse the repository at this point in the history
Remove dead insert() comment
  • Loading branch information
yebblies committed Aug 29, 2015
2 parents fa50be0 + f5fb09c commit f0bba25
Showing 1 changed file with 0 additions and 115 deletions.
115 changes: 0 additions & 115 deletions std/array.d
Expand Up @@ -786,121 +786,6 @@ inout(T)[] overlap(T)(inout(T)[] r1, inout(T)[] r2) @trusted pure nothrow
static assert(isRandomAccessRange!Wrapper);
}

/+
Commented out until the insert which has been deprecated has been removed.
I'd love to just remove it in favor of insertInPlace, but then code would then
use this version of insert and silently break. So, it's here so that it can
be used once insert has not only been deprecated but removed, but until then,
it's commented out.
/++
Creates a new array which is a copy of $(D array) with $(D stuff) (which
must be an input range or a single item) inserted at position $(D pos).
Examples:
--------------------
int[] a = [ 1, 2, 3, 4 ];
auto b = a.insert(2, [ 1, 2 ]);
assert(a == [ 1, 2, 3, 4 ]);
assert(b == [ 1, 2, 1, 2, 3, 4 ]);
--------------------
+/
T[] insert(T, Range)(T[] array, size_t pos, Range stuff)
if(isInputRange!Range &&
(is(ElementType!Range : T) ||
isSomeString!(T[]) && is(ElementType!Range : dchar)))
{
static if(hasLength!Range && is(ElementEncodingType!Range : T))
{
import std.algorithm : copy;
auto retval = new Unqual!(T)[](array.length + stuff.length);
retval[0 .. pos] = array[0 .. pos];
copy(stuff, retval[pos .. pos + stuff.length]);
retval[pos + stuff.length .. $] = array[pos .. $];
return cast(T[])retval;
}
else
{
auto app = appender!(T[])();
app.put(array[0 .. pos]);
app.put(stuff);
app.put(array[pos .. $]);
return app.data;
}
}
/++ Ditto +/
T[] insert(T)(T[] array, size_t pos, T stuff)
{
auto retval = new T[](array.length + 1);
retval[0 .. pos] = array[0 .. pos];
retval[pos] = stuff;
retval[pos + 1 .. $] = array[pos .. $];
return retval;
}
//Verify Example.
unittest
{
int[] a = [ 1, 2, 3, 4 ];
auto b = a.insert(2, [ 1, 2 ]);
assert(a == [ 1, 2, 3, 4 ]);
assert(b == [ 1, 2, 1, 2, 3, 4 ]);
}
unittest
{
import core.exception;
import std.conv : to;
import std.exception;
import std.algorithm;
auto a = [1, 2, 3, 4];
assert(a.insert(0, [6, 7]) == [6, 7, 1, 2, 3, 4]);
assert(a.insert(2, [6, 7]) == [1, 2, 6, 7, 3, 4]);
assert(a.insert(a.length, [6, 7]) == [1, 2, 3, 4, 6, 7]);
assert(a.insert(0, filter!"true"([6, 7])) == [6, 7, 1, 2, 3, 4]);
assert(a.insert(2, filter!"true"([6, 7])) == [1, 2, 6, 7, 3, 4]);
assert(a.insert(a.length, filter!"true"([6, 7])) == [1, 2, 3, 4, 6, 7]);
assert(a.insert(0, 22) == [22, 1, 2, 3, 4]);
assert(a.insert(2, 22) == [1, 2, 22, 3, 4]);
assert(a.insert(a.length, 22) == [1, 2, 3, 4, 22]);
assert(a == [1, 2, 3, 4]);
auto testStr(T, U)(string file = __FILE__, size_t line = __LINE__)
{
auto l = to!T("hello");
auto r = to!U(" world");
enforce(insert(l, 0, r) == " worldhello",
new AssertError("testStr failure 1", file, line));
enforce(insert(l, 3, r) == "hel worldlo",
new AssertError("testStr failure 2", file, line));
enforce(insert(l, l.length, r) == "hello world",
new AssertError("testStr failure 3", file, line));
enforce(insert(l, 0, filter!"true"(r)) == " worldhello",
new AssertError("testStr failure 4", file, line));
enforce(insert(l, 3, filter!"true"(r)) == "hel worldlo",
new AssertError("testStr failure 5", file, line));
enforce(insert(l, l.length, filter!"true"(r)) == "hello world",
new AssertError("testStr failure 6", file, line));
}
testStr!(string, string)();
testStr!(string, wstring)();
testStr!(string, dstring)();
testStr!(wstring, string)();
testStr!(wstring, wstring)();
testStr!(wstring, dstring)();
testStr!(dstring, string)();
testStr!(dstring, wstring)();
testStr!(dstring, dstring)();
}
+/

private void copyBackwards(T)(T[] src, T[] dest)
{
import core.stdc.string;
Expand Down

0 comments on commit f0bba25

Please sign in to comment.