mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 06:30:28 +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.
|
// 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
|
// 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.
|
// safe WRT built-in append, and we can use the entire block.
|
||||||
auto cap = arr.capacity; //trusted
|
// We only do this for mutable types that can be extended.
|
||||||
if (cap > arr.length)
|
static if (isMutable!T && is(typeof(arr.length = size_t.max)))
|
||||||
arr = arr.ptr[0 .. cap]; //trusted
|
{
|
||||||
|
auto cap = arr.capacity; //trusted
|
||||||
// we assume no reallocation occurred
|
// Replace with "GC.setAttr( Not Appendable )" once pure (and fixed)
|
||||||
assert(arr.ptr is _data.arr.ptr);
|
if (cap > arr.length)
|
||||||
|
arr.length = cap;
|
||||||
|
}
|
||||||
_data.capacity = arr.length;
|
_data.capacity = arr.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3051,6 +3053,20 @@ unittest
|
||||||
assert(app.data == [1, 2, 3]);
|
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
|
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
|
of an array. Experimental duplication of the built-in slice - do not
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue