1 module dpmatch.util;
2 import std..string;
3 import std.algorithm;
4 import std.traits;
5 
6 string patternReplaceWithTable(string code, string[string] table) {
7   string[] codes;
8   size_t[][string] targets;
9   bool in_bracket;
10   string buf;
11 
12   for (size_t i = 0; i < code.length; i++) {
13     char ch = code[i];
14 
15     if (!in_bracket) {
16       if (ch == '#') {
17         if (i + 1 < code.length && code[i + 1] == '{') {
18           in_bracket = true;
19           i++;
20 
21           codes ~= buf;
22           buf = "";
23           continue;
24         } else {
25           throw new Error("Syntax Error");
26         }
27       } else {
28         buf ~= ch;
29       }
30     } else {
31       if (ch == '}') {
32         if (i + 1 < code.length && code[i + 1] == '#') {
33           in_bracket = false;
34           i++;
35 
36           codes ~= buf;
37           targets[buf] ~= codes.length - 1;
38           buf = "";
39           continue;
40         } else {
41           buf ~= ch;
42         }
43       } else {
44         buf ~= ch;
45       }
46     }
47   }
48 
49   if (buf.length) {
50     codes ~= buf;
51   }
52 
53   foreach (key, value; table) {
54     size_t[] idxes = targets[key];
55     foreach (idx; idxes) {
56       codes[idx] = value;
57     }
58   }
59 
60   return codes.join;
61 }
62 
63 string[] getOriginalMembers(T)() {
64   string[] ret;
65   string[] reserved = [
66     "__ctor", "type", "toString", "toHash", "opCmp", "opEquals", "Monitor",
67     "factory"
68   ];
69 
70   foreach (elem; __traits(allMembers, T)) {
71     if (!reserved.canFind(elem)) {
72       ret ~= elem;
73     }
74   }
75 
76   return ret;
77 }
78 
79 bool isTemplate(T)() {
80   static if (is(TemplateOf!T == void)) {
81     return false;
82   } else {
83     return true;
84   }
85 }