@@ -7,6 +7,7 @@ import binary.section;
7
7
import binary.types;
8
8
9
9
import std.algorithm : each;
10
+ import std.array : appender, array;
10
11
import std.bitmanip : read;
11
12
import std.exception : enforce;
12
13
import std.range : front, popFront, popFrontExactly;
@@ -118,13 +119,13 @@ Limits decodeLimits(ref const(ubyte)[] input)
118
119
// /
119
120
const (Instruction)[] decodeExpr (ref const (ubyte )[] input)
120
121
{
121
- typeof ( return ) insns = [] ;
122
+ auto insns = appender ! ( const (Instruction)[]) ;
122
123
while (true )
123
124
{
124
125
if (OpCode.End == input.front)
125
126
{
126
127
input.popFront();
127
- return insns;
128
+ return insns.array ;
128
129
}
129
130
insns ~= input.decodeInstruction();
130
131
}
@@ -135,7 +136,8 @@ const(Instruction)[] decodeExpr(ref const(ubyte)[] input)
135
136
const (Data)[] decodeDataSection (ref const (ubyte )[] input)
136
137
{
137
138
const count = input.leb128! uint ();
138
- const (Data)[] data;
139
+ auto data = appender! (const (Data)[]);
140
+ data.reserve (count);
139
141
foreach (_; 0 .. count)
140
142
{
141
143
const memoryIndex = input.leb128! uint ();
@@ -149,7 +151,7 @@ const(Data)[] decodeDataSection(ref const(ubyte)[] input)
149
151
bytes: bytes
150
152
);
151
153
}
152
- return data;
154
+ return data.array ;
153
155
}
154
156
155
157
// /
@@ -161,8 +163,9 @@ ValueType decodeValueSection(ref const(ubyte)[] input)
161
163
// /
162
164
const (FuncType)[] decodeTypeSection (ref const (ubyte )[] input)
163
165
{
164
- const (FuncType)[] funcTypes ;
166
+ auto funcTypes = appender ! ( const (FuncType)[]) ;
165
167
const count = input.leb128! uint ();
168
+ funcTypes.reserve (count);
166
169
foreach (_; 0 .. count)
167
170
{
168
171
input.read! ubyte ();
@@ -174,33 +177,35 @@ const(FuncType)[] decodeTypeSection(ref const(ubyte)[] input)
174
177
input.popFrontExactly(size);
175
178
funcTypes ~= FuncType(params: params, results: results);
176
179
}
177
- return funcTypes;
180
+ return funcTypes.array ;
178
181
}
179
182
180
183
// /
181
184
const (uint )[] decodeFunctionSection (ref const (ubyte )[] input)
182
185
{
183
- const (uint )[] funcIdxList ;
186
+ auto funcIdxList = appender ! ( const (uint )[]) ;
184
187
const count = input.leb128! uint ();
188
+ funcIdxList.reserve (count);
185
189
foreach (_; 0 .. count)
186
190
{
187
191
const idx = input.leb128! uint ();
188
192
funcIdxList ~= idx;
189
193
}
190
- return funcIdxList;
194
+ return funcIdxList.array ;
191
195
}
192
196
193
197
// /
194
198
Function[] decodeCodeSection (ref const (ubyte )[] input)
195
199
{
196
- Function[] functions ;
200
+ auto functions = appender ! ( Function[]) ;
197
201
const count = input.leb128! uint ();
202
+ functions.reserve (count);
198
203
foreach (_; 0 .. count)
199
204
{
200
205
auto size = input.leb128! uint (); // func body size
201
206
functions ~= input.decodeFunctionBody(size);
202
207
}
203
- return functions;
208
+ return functions.array ;
204
209
}
205
210
206
211
// /
@@ -302,8 +307,9 @@ Block decodeBlockSection(ref const(ubyte)[] input, ref uint remaining)
302
307
// /
303
308
const (Export)[] decodeExportSection (ref const (ubyte )[] input)
304
309
{
310
+ auto exports = appender! (const (Export)[]);
305
311
const count = input.leb128! uint ();
306
- const (Export)[] exports ;
312
+ exports. reserve (count) ;
307
313
foreach (_; 0 .. count)
308
314
{
309
315
const name = input.decodeName();
@@ -313,14 +319,15 @@ const(Export)[] decodeExportSection(ref const(ubyte)[] input)
313
319
ExportDesc desc = Func(idx);
314
320
exports ~= Export(name: name, desc: desc);
315
321
}
316
- return exports;
322
+ return exports.array ;
317
323
}
318
324
319
325
// /
320
326
const (Import)[] decodeImportSection (ref const (ubyte )[] input)
321
327
{
328
+ auto imports = appender! (const (Import)[]);
322
329
const count = input.leb128! uint ();
323
- const (Import)[] imports ;
330
+ imports. reserve (count) ;
324
331
foreach (_; 0 .. count)
325
332
{
326
333
const moduleName = input.decodeName();
@@ -335,7 +342,7 @@ const(Import)[] decodeImportSection(ref const(ubyte)[] input)
335
342
desc: desc
336
343
);
337
344
}
338
- return imports;
345
+ return imports.array ;
339
346
}
340
347
341
348
string decodeName (ref const (ubyte )[] input)
0 commit comments