1-
21/**
32 * Dynamic array implementation.
43 *
@@ -64,10 +63,23 @@ public:
6463 // int, and c++ header generation doesn't accept wrapping this in static if
6564 extern (D ) this ()(T[] elems ... ) pure nothrow if (is (T == struct ) || is (T == class ))
6665 {
67- this (elems.length);
68- foreach (i; 0 .. elems.length)
66+ this .reserve (elems.length);
67+ this .length = elems.length;
68+
69+ static if (__traits(isPOD, T))
6970 {
70- this [i] = elems[i];
71+ if (elems.length)
72+ memcpy(data.ptr, elems.ptr, elems.length * T.sizeof);
73+ }
74+ else
75+ {
76+ auto p = data.ptr;
77+ auto q = elems.ptr;
78+ const n = elems.length;
79+ for (size_t i = 0 ; i < n; ++ i)
80+ {
81+ p[i] = q[i];
82+ }
7183 }
7284 }
7385
@@ -76,50 +88,66 @@ public:
7688 {
7789 static const (char )[] toStringImpl (alias toStringFunc, Array)(Array* a, bool quoted = false )
7890 {
79- const (char )[][] buf = (cast (const (char )[]* )mem.xcalloc((char []).sizeof, a.length))[0 .. a.length];
80- size_t len = 2 ; // [ and ]
81- const seplen = quoted ? 3 : 1 ; // ',' or null terminator and optionally '"'
82- if (a.length == 0 )
83- len += 1 ; // null terminator
84- else
91+ const size_t n = a.length;
92+ if (n == 0 ) return " []" ;
93+
94+ const (char )[] nullStr = " null" ;
95+ const (char )[][] buf = (cast (const (char )[]* )mem.xcalloc((char []).sizeof, n))[0 .. n];
96+ size_t len = 2 ;
97+ foreach (u; 0 .. n)
8598 {
86- foreach (u; 0 .. a.length )
99+ static if ( is ( typeof (a.data[u] is null )) )
87100 {
88- static if (is ( typeof ( a.data[u] is null )) )
101+ if (a.data[u] is null )
89102 {
90- if (a.data[u] is null )
91- buf[u] = " null" ;
92- else
93- buf[u] = toStringFunc(a.data[u]);
103+ buf[u] = nullStr;
94104 }
95105 else
96106 {
97107 buf[u] = toStringFunc(a.data[u]);
98108 }
99-
100- len += buf[u].length + seplen;
101109 }
110+ else
111+ {
112+ buf[u] = toStringFunc(a.data[u]);
113+ }
114+
115+ len += buf[u].length;
116+ if (u > 0 ) len += 1 ;
117+ if (quoted) len += 2 ;
102118 }
103- char [] str = (cast (char * )mem.xmalloc_noscan(len))[0 .. len];
119+ char * str = cast (char * )mem.xmalloc_noscan(len + 1 );
120+ char * p = str;
104121
105- str[0 ] = ' [' ;
106- char * p = str.ptr + 1 ;
107- foreach (u; 0 .. a.length)
122+ * p++ = ' [' ;
123+ foreach (u; 0 .. n)
108124 {
109- if (u)
125+ if (u > 0 )
126+ {
110127 * p++ = ' ,' ;
128+ }
129+
111130 if (quoted)
131+ {
112132 * p++ = ' "' ;
113- memcpy(p, buf[u].ptr, buf[u].length);
114- p += buf[u].length;
133+ }
134+
135+ if (buf[u].length)
136+ {
137+ memcpy(p, buf[u].ptr, buf[u].length);
138+ p += buf[u].length;
139+ }
140+
115141 if (quoted)
142+ {
116143 * p++ = ' "' ;
144+ }
117145 }
118146 * p++ = ' ]' ;
119147 * p = 0 ;
120- assert (p - str.ptr == str.length - 1 ); // null terminator
148+ assert (cast ( size_t )( p - str) == len);
121149 mem.xfree(buf.ptr);
122- return str[0 .. $ - 1 ];
150+ return str[0 .. len ];
123151 }
124152
125153 static if (is (typeof (T.init.toString())))
0 commit comments