| | 1026 | struct _BoolSet(string OP, T...) { |
|---|
| | 1027 | T values; |
|---|
| | 1028 | int opEquals(U)(U other) { |
|---|
| | 1029 | bool res = other == values[0]; |
|---|
| | 1030 | foreach (value; values[1..$]) |
|---|
| | 1031 | res = mixin("res "~OP~" value"); |
|---|
| | 1032 | return res; |
|---|
| | 1033 | } |
|---|
| | 1034 | } |
|---|
| | 1035 | |
|---|
| | 1036 | template BoolSet(string OP) { |
|---|
| | 1037 | _BoolSet!(OP, T) BoolSet(T...)(T t) { |
|---|
| | 1038 | _BoolSet!(OP, T) res = void; |
|---|
| | 1039 | foreach (i, v; t) res.values[i] = v; |
|---|
| | 1040 | return res; |
|---|
| | 1041 | } |
|---|
| | 1042 | } |
|---|
| | 1043 | |
|---|
| | 1044 | alias BoolSet!("||") OrSet; alias BoolSet!("&&") AndSet; |
|---|
| | 1045 | mixin(Operator!("or", "return OrSet(lhs, rhs); ")); |
|---|
| | 1046 | |
|---|
| | 1073 | |
|---|
| | 1074 | string between(string text, string from, string to) { |
|---|
| | 1075 | int pos1; |
|---|
| | 1076 | if (from.length) pos1 = text.find(from); |
|---|
| | 1077 | else pos1 = 0; |
|---|
| | 1078 | if (pos1 == -1) return null; |
|---|
| | 1079 | text = text[pos1 + from.length .. $]; |
|---|
| | 1080 | int pos2; |
|---|
| | 1081 | if (to.length) pos2 = text.find(to); |
|---|
| | 1082 | else pos2 = text.length; |
|---|
| | 1083 | if (pos2 == -1) return null; |
|---|
| | 1084 | return text[0 .. pos2]; |
|---|
| | 1085 | } |
|---|
| | 1086 | |
|---|
| | 1087 | string[] betweens(string text, string from, string to) { |
|---|
| | 1088 | string[] res; |
|---|
| | 1089 | while (true) { |
|---|
| | 1090 | auto pos1 = text.find(from); if (pos1 == -1) break; |
|---|
| | 1091 | text = text[pos1 + from.length .. $]; |
|---|
| | 1092 | auto pos2 = text.find(to); if (pos2 == -1) break; |
|---|
| | 1093 | res ~= text[0 .. pos2]; |
|---|
| | 1094 | text = text[pos2 + to.length .. $]; |
|---|
| | 1095 | } |
|---|
| | 1096 | return res; |
|---|
| | 1097 | } |
|---|
| | 1098 | |
|---|
| | 1099 | void glomp_parse(string text, void delegate(string pre, ref string post)[string] words, void delegate(string) rest) { |
|---|
| | 1100 | while (true) { |
|---|
| | 1101 | void delegate(string, ref string) dg; int min = int.max; string match; |
|---|
| | 1102 | foreach (key, value; words) { |
|---|
| | 1103 | auto pos = text.find(key); |
|---|
| | 1104 | if (pos == -1) continue; |
|---|
| | 1105 | if (pos < min) { |
|---|
| | 1106 | min = pos; |
|---|
| | 1107 | dg = value; |
|---|
| | 1108 | match = key; |
|---|
| | 1109 | } |
|---|
| | 1110 | } |
|---|
| | 1111 | if (dg) { |
|---|
| | 1112 | auto pre = text[0 .. min]; |
|---|
| | 1113 | text = text[min + match.length .. $]; |
|---|
| | 1114 | // logln("Found at ", min, ", pre ", pre, ", text ", text); |
|---|
| | 1115 | dg(pre, text); |
|---|
| | 1116 | } else { |
|---|
| | 1117 | rest(text); |
|---|
| | 1118 | break; |
|---|
| | 1119 | } |
|---|
| | 1120 | } |
|---|
| | 1121 | } |
|---|