/
conv.d
307 lines (269 loc) · 8 KB
/
conv.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
/++
+ This module contains functions that in one way or another converts its
+ arguments into something else.
+/
module kameloso.conv;
import std.typecons : Flag, No, Yes;
@safe:
// Enum
/++
+ Template housing optimised functions to get the string name of an enum
+ member, or the enum member of a name string.
+
+ `std.conv.to` is typically the go-to for this job; however it quickly bloats
+ the binary and is supposedly not performant on larger enums.
+
+ Params:
+ E = `enum` to base this template on.
+/
template Enum(E)
if (is(E == enum))
{
// fromString
/++
+ Takes the member of an enum by string and returns that enum member.
+
+ It lowers to a big switch of the enum member strings. It is faster than
+ `std.conv.to` and generates less template bloat.
+
+ Taken from: https://forum.dlang.org/post/bfnwstkafhfgihavtzsz@forum.dlang.org
+ written by Stephan Koch (https://github.com/UplinkCoder).
+
+ Example:
+ ---
+ enum SomeEnum { one, two, three };
+
+ SomeEnum foo = Enum!SomeEnum.fromString("one");
+ SomeEnum bar = Enum!SomeEnum.fromString("three");
+
+ assert(foo == SomeEnum.one);
+ assert(bar == SomeEnum.three);
+ ---
+
+ Params:
+ enumstring = the string name of an enum member.
+
+ Returns:
+ The enum member whose name matches the enumstring string (not whose
+ *value* matches the string).
+
+ Throws: `std.conv.ConvException` if no matching enum member with the
+ passed name could be found.
+/
E fromString(const string enumstring) pure
{
enum enumSwitch = ()
{
string enumSwitch = "import std.conv : ConvException;\n";
enumSwitch ~= "with (E) switch (enumstring)\n{\n";
foreach (immutable memberstring; __traits(allMembers, E))
{
enumSwitch ~= `case "` ~ memberstring ~ `":`;
enumSwitch ~= "return " ~ memberstring ~ ";\n";
}
enumSwitch ~= "default:\n" ~
"import std.traits : fullyQualifiedName;\n" ~
`throw new ConvException("No such " ~ fullyQualifiedName!E ~ ": " ~ enumstring);}`;
return enumSwitch;
}();
mixin(enumSwitch);
}
// toString
/++
+ The inverse of `fromString`, this function takes an enum member value
+ and returns its string identifier.
+
+ It lowers to a big switch of the enum members. It is faster than
+ `std.conv.to` and generates less template bloat.
+
+ Taken from: https://forum.dlang.org/post/bfnwstkafhfgihavtzsz@forum.dlang.org
+ written by Stephan Koch (https://github.com/UplinkCoder).
+
+ Example:
+ ---
+ enum SomeEnum { one, two, three };
+
+ string foo = Enum!SomeEnum.toString(one);
+ assert((foo == "one"), foo);
+ ---
+
+ Params:
+ value = Enum member whose string name we want.
+
+ Returns:
+ The string name of the passed enum member.
+/
string toString(E value) pure nothrow
{
switch (value)
{
foreach (immutable m; __traits(allMembers, E))
{
case mixin("E." ~ m) : return m;
}
default:
string result = "cast(" ~ E.stringof ~ ")";
uint val = value;
enum headLength = E.stringof.length + "cast()".length;
immutable log10Val =
(val < 10) ? 0 :
(val < 100) ? 1 :
(val < 1_000) ? 2 :
(val < 10_000) ? 3 :
(val < 100_000) ? 4 :
(val < 1_000_000) ? 5 :
(val < 10_000_000) ? 6 :
(val < 100_000_000) ? 7 :
(val < 1_000_000_000) ? 8 : 9;
result.length += log10Val + 1;
for (uint i; i != log10Val + 1; ++i)
{
cast(char)result[headLength + log10Val - i] = cast(char)('0' + (val % 10));
val /= 10;
}
return result;
}
}
}
///
@system
unittest
{
import std.conv : ConvException;
import std.exception : assertThrown;
enum T
{
UNSET,
QUERY,
PRIVMSG,
RPL_ENDOFMOTD
}
with (T)
{
assert(Enum!T.fromString("QUERY") == QUERY);
assert(Enum!T.fromString("PRIVMSG") == PRIVMSG);
assert(Enum!T.fromString("RPL_ENDOFMOTD") == RPL_ENDOFMOTD);
assert(Enum!T.fromString("UNSET") == UNSET);
assertThrown!ConvException(Enum!T.fromString("DOESNTEXIST")); // needs @system
}
with (T)
{
assert(Enum!T.toString(QUERY) == "QUERY");
assert(Enum!T.toString(PRIVMSG) == "PRIVMSG");
assert(Enum!T.toString(RPL_ENDOFMOTD) == "RPL_ENDOFMOTD");
}
}
// numFromHex
/++
+ Returns the decimal value of a hex number in string form.
+
+ Example:
+ ---
+ int fifteen = numFromHex("F");
+ int twofiftyfive = numFromHex("FF");
+ ---
+
+ Params:
+ acceptLowercase = Flag of whether or not to accept rrggbb in lowercase form.
+ hex = Hexadecimal number in string form.
+
+ Returns:
+ An integer equalling the value of the passed hexadecimal string.
+
+ Throws: `std.conv.ConvException` if the hex string was malformed.
+/
uint numFromHex(Flag!"acceptLowercase" acceptLowercase = No.acceptLowercase)(const string hex) pure
{
import std.string : representation;
int val = -1;
int total;
foreach (immutable c; hex.representation)
{
switch (c)
{
case '0':
..
case '9':
val = (c - 48);
goto case 'F';
static if (acceptLowercase)
{
case 'a':
..
case 'f':
val = (c - (55+32));
goto case 'F';
}
case 'A':
..
case 'F':
if (val < 0) val = (c - 55);
total *= 16;
total += val;
val = -1;
break;
default:
import std.conv : ConvException;
throw new ConvException("Invalid hex string: " ~ hex);
}
}
assert((total < 16^^hex.length), "numFromHex output is too large!");
return total;
}
// numFromHex
/++
+ Convenience wrapper that takes a hex string and maps the values to three
+ integers passed by ref.
+
+ This is to be used when mapping a #RRGGBB colour to their decimal
+ red/green/blue equivalents.
+
+ Params:
+ acceptLowercase = Whether or not to accept the rrggbb string in lowercase letters.
+ hexString = Hexadecimal number (colour) in string form.
+ r = Out-reference integer for the red part of the hex string.
+ g = Out-reference integer for the green part of the hex string.
+ b = Out-reference integer for the blue part of the hex string.
+/
void numFromHex(Flag!"acceptLowercase" acceptLowercase = No.acceptLowercase)
(const string hexString, out int r, out int g, out int b) pure
{
if (!hexString.length) return;
immutable hex = (hexString[0] == '#') ? hexString[1..$] : hexString;
r = numFromHex!acceptLowercase(hex[0..2]);
g = numFromHex!acceptLowercase(hex[2..4]);
b = numFromHex!acceptLowercase(hex[4..$]);
}
///
unittest
{
import std.conv : text;
{
int r, g, b;
numFromHex("000102", r, g, b);
assert((r == 0), r.text);
assert((g == 1), g.text);
assert((b == 2), b.text);
}
{
int r, g, b;
numFromHex("FFFFFF", r, g, b);
assert((r == 255), r.text);
assert((g == 255), g.text);
assert((b == 255), b.text);
}
{
int r, g, b;
numFromHex("3C507D", r, g, b);
assert((r == 60), r.text);
assert((g == 80), g.text);
assert((b == 125), b.text);
}
{
int r, g, b;
numFromHex!(Yes.acceptLowercase)("9a4B7c", r, g, b);
assert((r == 154), r.text);
assert((g == 75), g.text);
assert((b == 124), b.text);
}
}