3030extern (C++ ) struct Array (T)
3131{
3232 size_t length;
33+ alias opSlice this ;
3334
3435private :
3536 T[] data;
@@ -64,10 +65,11 @@ public:
6465 // int, and c++ header generation doesn't accept wrapping this in static if
6566 extern (D ) this ()(T[] elems ... ) pure nothrow if (is (T == struct ) || is (T == class ))
6667 {
67- this (elems.length);
68+ this .reserve (elems.length);
69+ this .length = elems.length;
6870 foreach (i; 0 .. elems.length)
6971 {
70- this [i] = elems[i];
72+ this .data [i] = elems[i];
7173 }
7274 }
7375
@@ -76,50 +78,61 @@ public:
7678 {
7779 static const (char )[] toStringImpl (alias toStringFunc, Array)(Array* a, bool quoted = false )
7880 {
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
81+ const size_t n = a.length;
82+ if (n == 0 ) return " []" ;
83+
84+ const (char )[] nullStr = " null" ;
85+ const (char )[][] buf = (cast (const (char )[]* )mem.xcalloc((char []).sizeof, n))[0 .. n];
86+ size_t len = 2 ;
87+ foreach (u; 0 .. n)
8588 {
86- foreach (u; 0 .. a.length )
89+ static if ( is ( typeof (a.data[u] is null )) )
8790 {
88- static if (is ( typeof ( a.data[u] is null )) )
91+ if (a.data[u] is null )
8992 {
90- if (a.data[u] is null )
91- buf[u] = " null" ;
92- else
93- buf[u] = toStringFunc(a.data[u]);
93+ buf[u] = nullStr;
9494 }
9595 else
9696 {
9797 buf[u] = toStringFunc(a.data[u]);
9898 }
99-
100- len += buf[u].length + seplen;
10199 }
100+ else
101+ {
102+ buf[u] = toStringFunc(a.data[u]);
103+ }
104+
105+ len += buf[u].length;
106+ if (u > 0 ) len += 1 ;
107+ if (quoted) len += 2 ;
102108 }
103- char [] str = (cast (char * )mem.xmalloc_noscan(len))[0 .. len];
104109
105- str[0 ] = ' [' ;
106- char * p = str.ptr + 1 ;
107- foreach (u; 0 .. a.length)
110+ char * str = cast (char * )mem.xmalloc_noscan(len + 1 );
111+ char * p = str;
112+
113+ * p++ = ' [' ;
114+ foreach (u; 0 .. n)
108115 {
109- if (u)
116+ if (u > 0 )
110117 * p++ = ' ,' ;
118+
111119 if (quoted)
112120 * p++ = ' "' ;
113- memcpy(p, buf[u].ptr, buf[u].length);
114- p += buf[u].length;
121+
122+ if (buf[u].length)
123+ {
124+ memcpy(p, buf[u].ptr, buf[u].length);
125+ p += buf[u].length;
126+ }
127+
115128 if (quoted)
116129 * p++ = ' "' ;
117130 }
118131 * p++ = ' ]' ;
119132 * p = 0 ;
120- assert (p - str.ptr == str.length - 1 ); // null terminator
133+ assert (cast ( size_t )( p - str) == len);
121134 mem.xfree(buf.ptr);
122- return str[0 .. $ - 1 ];
135+ return str[0 .. len ];
123136 }
124137
125138 static if (is (typeof (T.init.toString())))
@@ -205,10 +218,13 @@ public:
205218 memcpy(p, data.ptr, length * T.sizeof);
206219 memset(data.ptr, 0xFF , data.length * T.sizeof);
207220 mem.xfree(data.ptr);
221+ data = p[0 .. allocdim];
208222 }
209223 else
224+ {
210225 auto p = cast (T* )mem.xrealloc(data.ptr, allocdim * T.sizeof);
211- data = p[0 .. allocdim];
226+ data = p[0 .. allocdim];
227+ }
212228 }
213229
214230 debug (stomp)
0 commit comments