Fix "use up capacity" in Appender

This commit is contained in:
monarch dodra 2014-03-24 12:28:05 +00:00 committed by monarchdodra
parent 277b7ce8c0
commit 8e66abb2fc

View file

@ -2293,12 +2293,14 @@ struct Appender(A : T[], T)
// We want to use up as much of the block the array is in as possible.
// if we consume all the block that we can, then array appending is
// safe WRT built-in append, and we can use the entire block.
auto cap = arr.capacity; //trusted
if (cap > arr.length)
arr = arr.ptr[0 .. cap]; //trusted
// we assume no reallocation occurred
assert(arr.ptr is _data.arr.ptr);
// We only do this for mutable types that can be extended.
static if (isMutable!T && is(typeof(arr.length = size_t.max)))
{
auto cap = arr.capacity; //trusted
// Replace with "GC.setAttr( Not Appendable )" once pure (and fixed)
if (cap > arr.length)
arr.length = cap;
}
_data.capacity = arr.length;
}
@ -3051,6 +3053,20 @@ unittest
assert(app.data == [1, 2, 3]);
}
unittest
{
string s = "hello".idup;
char[] a = "hello".dup;
auto appS = appender(s);
auto appA = appender(a);
put(appS, 'w');
put(appA, 'w');
s ~= 'a'; //Clobbers here?
a ~= 'a'; //Clobbers here?
assert(appS.data == "hellow");
assert(appA.data == "hellow");
}
/*
A simple slice type only holding pointers to the beginning and the end
of an array. Experimental duplication of the built-in slice - do not