File: conv.d

package info (click to toggle)
mir-core 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 560 kB
  • sloc: makefile: 9; sh: 7
file content (353 lines) | stat: -rw-r--r-- 10,007 bytes parent folder | download
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
/++
Conversion utilities.

License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
Authors: Ilia Ki
+/
module mir.conv;

import mir.exception: toMutable;
public import core.lifetime: emplace;

import std.traits;

private template isMirString(T)
{
    static if (isSomeString!T)
    {
        enum isMirString = true;
    }
    else
    {
        static if (__traits(compiles, {import mir.small_string;}))
        {
            import mir.small_string;
            enum isMirString = is(T : SmallString!size, size_t size);
        }
        else
        {
            enum isMirString = false;
        }
    }
}

/++
The `to` template converts a value from one type _to another.
The source type is deduced and the target type must be specified, for example the
expression `to!int(42.0)` converts the number 42 from
`double` _to `int`. The conversion is "unsafe", i.e.,
it does not check for overflow.
+/
template to(T)
{
    ///
    auto ref T to(A...)(auto ref A args)
        if (A.length > 0)
    {
        import mir.utility;
        import mir.functional: forward;
        static if (A.length == 1 && isImplicitlyConvertible!(A[0], T))
            return args[0];
        else
        static if (is(T == class) && is(typeof(new T(forward!args))))
            return new T(forward!args);
        else
        static if (is(typeof(T(args))))
            return T(forward!args);
        else
        static if (A.length == 1)
        {
            alias I = A[0];
            alias arg = args[0];
            static if (is(typeof(cast(T) arg)) && !(isDynamicArray!T && isDynamicArray!I) && !isSomeString!T)
                return cast(T) forward!arg;
            else
            static if (isSomeString!I && is(T == enum))
            {
                import mir.enums;
                uint index = void;
                if (getEnumIndexFromKey!T(arg, index)._expect(true))
                    return index.unsafeEnumFromIndex!T;
                static immutable msg = "Can not convert string to the enum " ~ T.stringof;
                version (D_Exceptions)
                {
                    static immutable Exception exc = new Exception(msg);
                    throw exc.toMutable;
                }
                else
                {
                    assert(0, msg);
                }
            }
            else
            static if (is(I == enum) && isSomeString!T)
            {
                import mir.enums;
                uint id = void;
                if (getEnumIndex(arg, id)._expect(true))
                    return enumStrings!I[id];
                assert(0);
            }
            else
            static if (isMirString!I && !isSomeString!T)
            {
                static assert (__traits(compiles, { import mir.parse: fromString; }));
                import mir.parse: fromString;
                return fromString!(Unqual!T)(arg[]);
            }
            else
            static if (!isSomeString!I && isMirString!T)
            {
                // static if (is(Unqual!I == typeof(null)))
                // {
                //     enum immutable(T) ret = "null";
                //     static if (isImplicitlyConvertible!(immutable T, T))
                //         return ret;
                //     else
                //         return .to!T(ret[]);
                // }
                // else
                static if (is(Unqual!I == bool))
                {
                    enum immutable(T) t = "true";
                    enum immutable(T) f = "false";
                    auto ret = arg ? t : f;
                    static if (isImplicitlyConvertible!(immutable T, T))
                        return ret;
                    else
                        return .to!T(ret[]);
                }
                else
                {
                    static if (isImplicitlyConvertible!(T, string) && __traits(compiles, () {const(char)[] s =  arg.toString; return s;}))
                    {
                        auto ret = arg.toString;
                        static if (is(typeof(ret) == string))
                            return ret;
                        else
                            return ret.idup;
                    }
                    else
                    {
                        static assert (__traits(compiles, { import mir.format: print; }));
                        import mir.format: print;
                        static if (isSomeString!T)
                        {
                            static if (isNumeric!I)
                            {
                                import mir.appender: UnsafeArrayBuffer;
                                alias C = Unqual!(ForeachType!T);
                                C[64] array = void;
                                auto buffer = UnsafeArrayBuffer!C(array);
                            }
                            else
                            {
                                import mir.appender: scopedBuffer;
                                auto buffer = scopedBuffer!(Unqual!(ForeachType!T));
                            }
                            buffer.print(arg);
                            static if (isMutable!(ForeachType!(T)))
                                return buffer.data.dup;
                            else
                                return buffer.data.idup;
                        }
                        else
                        {
                            Unqual!T buffer;
                            buffer.print(arg);
                            return buffer;
                        }
                    }
                }
            }
            else
            static if (is(I : const(C)[], C) && is(T : immutable(C)[]))
            {
                static if (is(I : immutable(C)[]))
                    return arg;
                else
                    return idup(arg);
            }
            else
            static if (is(I : const(D)[], D) && is(T : D[]))
            {
                static if (is(I : D[]))
                    return arg;
                else
                    return dup(arg);
            }
            else 
                static assert(0, T.stringof);
        }
        else
            static assert(0, T.stringof);
    }
}

///
@safe pure @nogc
version(mir_core_test) unittest
{
    enum E
    {
        A,
        B,
        C,
    }

    assert(to!E("B") == E.B);
    assert(to!string(E.B) == "B");
    assert(to!string(null) is null);
    assert(to!string(true) == "true");
    assert(to!string(false) == "false");

    enum S : wstring
    {
        a = "A",
        b = "B",
    }

    assert(to!wstring(S.b) == "B"w);
    assert(to!S("B"w) == S.b);
}

/++
Emplace helper function.
+/
void emplaceInitializer(T)(scope ref T chunk) @trusted pure nothrow

{
    // Emplace T.init.
    // Previously, an immutable static and memcpy were used to hold an initializer.
    // With improved unions, this is no longer needed.
    union UntypedInit
    {
        T dummy;
    }
    static struct UntypedStorage
    {
        align(T.alignof) void[T.sizeof] dummy;
    }

    () @trusted {
        *cast(UntypedStorage*) &chunk = cast(UntypedStorage) UntypedInit.init;
    } ();
}

/++
+/
T[] uninitializedFillDefault(T)(return scope T[] array) nothrow @nogc
{
    static if (__VERSION__ < 2083)
    {
        static if (__traits(isIntegral, T) && 0 == cast(T) (T.init + 1))
        {
            import core.stdc.string : memset;
            memset(array.ptr, 0xff, T.sizeof * array.length);
            return array;
        }
        else
        {
            pragma(inline, false);
            foreach(ref e; array)
                emplaceInitializer(e);
            return array;
        }
    }
    else
    {
        static if (__traits(isZeroInit, T))
        {
            import core.stdc.string : memset;
            memset(array.ptr, 0, T.sizeof * array.length);
            return array;
        }
        else static if (__traits(isIntegral, T) && 0 == cast(T) (T.init + 1))
        {
            import core.stdc.string : memset;
            memset(array.ptr, 0xff, T.sizeof * array.length);
            return array;
        }
        else
        {
            pragma(inline, false);
            foreach(ref e; array)
                emplaceInitializer(e);
            return array;
        }
    }
}

///
pure nothrow @nogc @system
version(mir_core_test) unittest
{
    static struct S { int x = 42; @disable this(this); }

    int[5] expected = [42, 42, 42, 42, 42];
    S[5] arr = void;
    uninitializedFillDefault(arr);
    assert((cast(int*) arr.ptr)[0 .. arr.length] == expected);
}

///
@system 
version(mir_core_test) unittest
{
    int[] a = [1, 2, 4];
    uninitializedFillDefault(a);
    assert(a == [0, 0, 0]);
}

/++
Destroy structs and unions usnig `__xdtor` member if any.
Do nothing for other types.
+/
void xdestroy(T)(scope T[] ar)
{
    static if ((is(T == struct) || is(T == union)) && __traits(hasMember, T, "__xdtor"))
    {
        static if (__traits(isSame, T, __traits(parent, ar[0].__xdtor)))
        {
            pragma(inline, false);
            foreach_reverse (ref e; ar)
                e.__xdtor();
        }
    }
}

///
nothrow @nogc version(mir_core_test) unittest
{
    __gshared int d;
    __gshared int c;
    struct D { ~this() nothrow @nogc {d++;} }
    extern(C++)
    struct C { ~this() nothrow @nogc {c++;} }
    C[2] carray;
    D[2] darray;
    carray.xdestroy;
    darray.xdestroy;
    assert(c == 2);
    assert(d == 2);
    c = 0;
    d = 0;
}


template emplaceRef(T)
{
    void emplaceRef(UT, Args...)(ref UT chunk, auto ref Args args)
    {
        import core.lifetime: forward;
        import core.internal.lifetime: emplaceRef;
        return emplaceRef!T(chunk, forward!args);
    }
}

void emplaceRef(UT, Args...)(ref UT chunk, auto ref Args args)
if (is(UT == Unqual!UT))
{
    import core.lifetime: forward;
    emplaceRef!UT(chunk, forward!args);
}