-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_struct.d
65 lines (47 loc) · 1.44 KB
/
core_struct.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
module d_glat.core_struct;
import std.algorithm : canFind, map;
import std.array : split;
import std.range : join;
import std.string : strip;
/*
A few tool functions for structs and classes
By Guillaume Lathoud, 2023
glat@glat.info
The Boost license applies to this file, as described in ./LICENSE
*/
string set_thisC(string csv_0)() pure
{ return set_thisC( csv_0 ); }
string set_thisC( in string csv_0 ) pure
{
return set_structC( "this", csv_0 );
}
string set_structC(string sname, string csv_0)() pure
{ return set_structC( sname, csv_0 ); }
string set_structC( in string sname, in string csv_0 ) pure
{
auto csv = csv_0.strip;
if (csv[ 0 ] == '{')
return set_structC( sname, csv[ 1..$-1 ] );
return csv.split( ',' )
.map!((a) => a.canFind( '=' ) ? sname~"."~a~";" : sname~"."~(a.strip)~" = "~(a.strip)~";\n")
.join( "" )
;
}
string struct_initC( in string StructName, in string v_name, in string csv_0 ) pure
// Example of use:
//
// mixin(struct_initC("MyStruct", "s", "{a, b, c:d}"));
{
return StructName~' '~v_name~" = "~struct_initC( csv_0 )~';';
}
string struct_initC( in string csv_0 ) pure
// "{a, b, c:d}" => code string equivalent to "{a:a, b:b, c:d}"
{
auto csv = csv_0.strip;
if (csv[ 0 ] == '{')
return csv[ 0 ]~struct_initC( csv[ 1..$-1 ] )~csv[ $-1 ];
return csv.split( ',' )
.map!`a.canFind( ':' ) ? a : a~" : "~a.strip`
.join( ",\n" )
;
}