-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathappender.d
366 lines (326 loc) · 8.72 KB
/
appender.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/++
$(H1 Scoped Buffer)
License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
Authors: Ilya Yaroshenko
+/
module mir.appender;
// import std.traits: isAssignable, hasElaborateDestructorhasElaborateCopyConstructor, hasElaborateAssign;
import mir.conv: _mir_destroy = xdestroy;
private extern(C) @system nothrow @nogc pure void* memcpy(scope void* s1, scope const void* s2, size_t n);
///
struct ScopedBuffer(T, size_t bytes = 4096)
if (bytes && T.sizeof <= bytes)
{
import std.traits: Unqual, isMutable, isIterable, hasElaborateAssign, isAssignable, isArray;
import mir.primitives: hasLength;
import mir.conv: emplaceRef;
private enum size_t _bufferLength = bytes / T.sizeof + (bytes % T.sizeof != 0);
private T[] _buffer;
private size_t _currentLength;
version (mir_secure_memory)
private align(T.alignof) ubyte[_bufferLength * T.sizeof] _scopeBufferPayload;
else
private align(T.alignof) ubyte[_bufferLength * T.sizeof] _scopeBufferPayload = void;
private ref inout(T[_bufferLength]) _scopeBuffer() inout @trusted return scope
{
return *cast(inout(T[_bufferLength])*)&_scopeBufferPayload;
}
private T[] prepare(size_t n) @trusted return scope
{
import mir.internal.memory: realloc, malloc;
_currentLength += n;
if (_buffer.length == 0)
{
if (_currentLength <= _bufferLength)
{
return _scopeBuffer[0 .. _currentLength];
}
else
{
auto newLen = _currentLength << 1;
if (auto p = malloc(T.sizeof * newLen))
{
_buffer = (cast(T*)p)[0 .. newLen];
}
else assert(0);
version (mir_secure_memory)
{
(cast(ubyte[])_buffer)[] = 0;
}
memcpy(cast(void*)_buffer.ptr, _scopeBuffer.ptr, T.sizeof * (_currentLength - n));
}
}
else
if (_currentLength > _buffer.length)
{
auto newLen = _currentLength << 1;
_buffer = (cast(T*)realloc(cast(void*)_buffer.ptr, T.sizeof * newLen))[0 .. newLen];
version (mir_secure_memory)
{
(cast(ubyte[])_buffer[_currentLength .. $])[] = 0;
}
}
return _buffer[0 .. _currentLength];
}
static if (isAssignable!(T, const T))
private alias R = const T;
else
private alias R = T;
/// Copy constructor is enabled only if `T` is mutable type without eleborate assign.
static if (isMutable!T && !hasElaborateAssign!T)
this(this)
{
import mir.internal.memory: malloc;
if (_buffer.ptr)
{
typeof(_buffer) buffer;
if (auto p = malloc(T.sizeof * _buffer.length))
{
buffer = (cast(T*)p)[0 .. T.sizeof * _buffer.length];
}
else assert(0);
version (mir_secure_memory)
{
(cast(ubyte[])buffer)[] = 0;
}
buffer[0 .. _currentLength] = _buffer[0 .. _currentLength];
_buffer = buffer;
}
}
else
@disable this(this);
///
~this()
{
import mir.internal.memory: free;
data._mir_destroy;
version(mir_secure_memory)
_currentLength = 0;
(() @trusted { if (_buffer.ptr) free(cast(void*)_buffer.ptr); })();
}
///
void shrinkTo(size_t length)
{
assert(length <= _currentLength);
data[length .. _currentLength]._mir_destroy;
_currentLength = length;
}
///
size_t length() scope const @property
{
return _currentLength;
}
///
void popBackN(size_t n)
{
sizediff_t t = _currentLength - n;
if (t < 0)
assert(0, "ScopedBffer.popBackN: n is too large.");
data[t .. _currentLength]._mir_destroy;
_currentLength = t;
}
///
void put(T e) @safe scope
{
auto cl = _currentLength;
auto d = prepare(1);
static if (isMutable!T)
{
import core.lifetime: moveEmplace;
()@trusted{moveEmplace(e, d[cl]);}();
}
else
{
emplaceRef!(Unqual!T)(d[cl], e);
}
}
static if (T.sizeof > 8 || hasElaborateAssign!T)
///
void put(ref R e) scope
{
auto cl = _currentLength;
auto d = prepare(1);
emplaceRef!(Unqual!T)(d[cl], e);
}
static if (!hasElaborateAssign!T)
///
void put(scope R[] e) scope
{
auto cl = _currentLength;
auto d = prepare(e.length);
if (!__ctfe)
(()@trusted=>memcpy(cast(void*)(d.ptr + cl), e.ptr, e.length * T.sizeof))();
else
static if (isMutable!T)
(()@trusted=> d[cl .. cl + e.length] = e)();
else
assert(0);
}
///
void put(Iterable)(Iterable range) scope
if (isIterable!Iterable && !__traits(isStaticArray, Iterable) && (!isArray!Iterable || hasElaborateAssign!T))
{
static if (hasLength!Iterable)
{
auto cl = _currentLength;
auto d = prepare(range.length);
foreach(ref e; range)
emplaceRef!(Unqual!T)(d[cl++], e);
assert(_currentLength == cl);
}
else
{
foreach(ref e; range)
put(e);
}
}
///
void reset() scope nothrow
{
this.__dtor;
_currentLength = 0;
_buffer = null;
}
///
void initialize() @system scope nothrow @nogc
{
_currentLength = 0;
_buffer = null;
}
///
inout(T)[] data() inout @property @safe return scope
{
return _buffer.length ? _buffer[0 .. _currentLength] : _scopeBuffer[0 .. _currentLength];
}
/++
Copies data into an array of the same length using `memcpy` C routine.
Shrinks the length to `0`.
+/
void moveDataAndEmplaceTo(T[] array) @system
in {
assert(array.length == _currentLength);
}
do {
memcpy(cast(void*)array.ptr, data.ptr, _currentLength * T.sizeof);
_currentLength = 0;
}
}
/// ditto
auto scopedBuffer(T, size_t bytes = 4096)() @trusted
{
ScopedBuffer!(T, bytes) buffer = void;
buffer.initialize;
return buffer;
}
///
@safe pure nothrow @nogc
version (mir_test) unittest
{
auto buf = scopedBuffer!char;
buf.put('c');
buf.put("str");
assert(buf.data == "cstr");
buf.popBackN(2);
assert(buf.data == "cs");
}
/// immutable
@safe pure nothrow @nogc
version (mir_test) unittest
{
auto buf = scopedBuffer!(immutable char);
buf.put('c');
buf.put("str");
assert(buf.data == "cstr");
buf.popBackN(2);
assert(buf.data == "cs");
}
@safe pure nothrow @nogc
version (mir_test) unittest
{
auto buf = scopedBuffer!(char, 3);
buf.put('c');
buf.put("str");
assert(buf.data == "cstr");
buf.popBackN(2);
assert(buf.data == "cs");
}
///
struct UnsafeArrayBuffer(T)
{
import std.traits: isImplicitlyConvertible;
///
T[] buffer;
///
size_t length;
///
void put(T a)
{
import core.lifetime: move;
assert(length < buffer.length);
buffer[length++] = move(a);
}
static if (isImplicitlyConvertible!(const T, T))
private alias E = const T;
else
private alias E = T;
///
void put(E[] a)
{
import core.lifetime: move;
assert(buffer.length >= a.length + length);
buffer[length .. length + a.length] = a;
length += a.length;
}
///
inout(T)[] data() inout @property @safe scope
{
return buffer[0 .. length];
}
///
void popBackN(size_t n)
{
sizediff_t t = length - n;
if (t < 0)
assert(0, "UnsafeBuffer.popBackN: n is too large.");
buffer[t .. length]._mir_destroy;
length = t;
}
}
///
@safe pure nothrow @nogc
version (mir_test) unittest
{
char[4] array;
auto buf = UnsafeArrayBuffer!char(array);
buf.put('c');
buf.put("str");
assert(buf.data == "cstr");
buf.popBackN(2);
assert(buf.data == "cs");
}
version(mir_bignum_test) // for DIP1000
@safe pure nothrow
unittest
{
import mir.conv: to;
import mir.algebraic : Algebraic;
static struct S
{
@safe pure nothrow @nogc:
@property string toString() const
{
return "_";
}
}
Algebraic!(int, string, double) x;
x = 42;
auto s = x.to!string;
assert(s == "42", s);
x = "abc";
assert(x.to!string == "abc");
x = 42.0;
assert(x.to!string == "42.0");
Algebraic!S y;
y = S();
assert(y.to!string == "_");
}