1 module buffer.utils;
2 
3 import std.conv;
4 import std.string;
5 import std.digest.md;
6 import std.array;
7 
8 string MD5(scope const(void[])[] src...)
9 {
10     auto md5 = new MD5Digest();
11     ubyte[] hash = md5.digest(src);
12 
13     return toHexString(hash).toUpper();
14 }
15 
16 ubyte[] strToByte_hex(const string input)
17 {
18     Appender!(ubyte[]) app;
19 
20     for (int i; i < input.length; i += 2)
21     {
22         app.put(input[i .. i + 2].to!ubyte(16));
23     }
24 
25     return app.data;
26 }
27 
28 string byteToStr_hex(T = byte)(const T[] buffer)
29 {
30     Appender!string app;
31 
32     foreach (b; buffer)
33     {
34         app.put(rightJustify(b.to!string(16).toUpper(), 2, '0'));
35     }
36     return app.data;
37 }
38 
39 string getClassSimpleName(const string input)
40 {
41     int pos = cast(int)lastIndexOf(input, '.');
42 
43     return input[pos < 0 ? 0 : pos + 1 .. $];
44 }
45 
46 ubyte[] realToUBytes(scope const real value)
47 {
48     ubyte[] buf = new ubyte[real.sizeof];
49     ubyte* p = cast(ubyte*)&value;
50     int i = real.sizeof;
51 
52     while (i-- > 0)
53     {
54         buf[real.sizeof - i - 1] = p[i];
55     }
56 
57     return buf;
58 }
59 
60 real ubytesToReal(scope const ubyte[] value)
61 {
62     real r;
63     ubyte* p = cast(ubyte*)&r;
64     
65     for (int i = 0; i < value.length; i++)
66     {
67         p[value.length - i - 1] = value[i];
68     }
69     
70     return r;
71 }