Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 55 additions & 27 deletions compiler/src/dmd/root/array.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Dynamic array implementation.
*
Expand Down Expand Up @@ -64,10 +63,23 @@ public:
// int, and c++ header generation doesn't accept wrapping this in static if
extern(D) this()(T[] elems ...) pure nothrow if (is(T == struct) || is(T == class))
{
this(elems.length);
foreach(i; 0 .. elems.length)
this.reserve(elems.length);
this.length = elems.length;

static if (__traits(isPOD, T))
{
this[i] = elems[i];
if (elems.length)
memcpy(data.ptr, elems.ptr, elems.length * T.sizeof);
}
else
{
auto p = data.ptr;
auto q = elems.ptr;
const n = elems.length;
for (size_t i = 0; i < n; ++i)
{
p[i] = q[i];
}
}
}

Expand All @@ -76,50 +88,66 @@ public:
{
static const(char)[] toStringImpl(alias toStringFunc, Array)(Array* a, bool quoted = false)
{
const(char)[][] buf = (cast(const(char)[]*)mem.xcalloc((char[]).sizeof, a.length))[0 .. a.length];
size_t len = 2; // [ and ]
const seplen = quoted ? 3 : 1; // ',' or null terminator and optionally '"'
if (a.length == 0)
len += 1; // null terminator
else
const size_t n = a.length;
if (n == 0) return "[]";

const(char)[] nullStr = "null";
const(char)[][] buf = (cast(const(char)[]*)mem.xcalloc((char[]).sizeof, n))[0..n];
size_t len = 2;
foreach (u; 0 .. n)
{
foreach (u; 0 .. a.length)
static if (is(typeof(a.data[u] is null)))
{
static if (is(typeof(a.data[u] is null)))
if (a.data[u] is null)
{
if (a.data[u] is null)
buf[u] = "null";
else
buf[u] = toStringFunc(a.data[u]);
buf[u] = nullStr;
}
else
{
buf[u] = toStringFunc(a.data[u]);
}

len += buf[u].length + seplen;
}
else
{
buf[u] = toStringFunc(a.data[u]);
}

len += buf[u].length;
if (u > 0) len += 1;
if (quoted) len += 2;
}
char[] str = (cast(char*)mem.xmalloc_noscan(len))[0..len];
char* str = cast(char*)mem.xmalloc_noscan(len + 1);
char* p = str;

str[0] = '[';
char* p = str.ptr + 1;
foreach (u; 0 .. a.length)
*p++ = '[';
foreach (u; 0 .. n)
{
if (u)
if (u > 0)
{
*p++ = ',';
}

if (quoted)
{
*p++ = '"';
memcpy(p, buf[u].ptr, buf[u].length);
p += buf[u].length;
}

if (buf[u].length)
{
memcpy(p, buf[u].ptr, buf[u].length);
p += buf[u].length;
}

if (quoted)
{
*p++ = '"';
}
}
*p++ = ']';
*p = 0;
assert(p - str.ptr == str.length - 1); // null terminator
assert(cast(size_t)(p - str) == len);
mem.xfree(buf.ptr);
return str[0 .. $-1];
return str[0 .. len];
}

static if (is(typeof(T.init.toString())))
Expand Down
Loading