mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 14:10:30 +03:00
Fix "use up capacity" in Appender
This commit is contained in:
parent
277b7ce8c0
commit
8e66abb2fc
1 changed files with 22 additions and 6 deletions
28
std/array.d
28
std/array.d
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue