1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.util.TypeUtils;
13 
14 import std.string;
15 import std.typecons;
16 
17 /**
18 */
19 alias Pair(F, S) = Tuple!(F, "first", S, "second");
20 Pair!(F, S) makePair(F, S)(F first, S second) {
21     return tuple!("first", "second")(first, second);
22 }
23 
24 unittest {
25     Pair!(string, int) p = makePair("age", 20);
26 
27     assert(p.first == "age");
28     assert(p.second == 20);
29 }
30 
31 /**
32  * 
33  */
34 struct TypeUtils {
35 
36     static string getSimpleName(TypeInfo info) {
37         if(info is null)
38             return "null";
39         string name = info.toString();
40         ptrdiff_t index = lastIndexOf(name, '.');
41         if(index == -1)
42             return name;
43         else
44             return name[index+1 .. $];
45     }
46 
47     static bool isIntegral(TypeInfo typeInfo) {
48         return typeInfo == typeid(int) || typeInfo == typeid(short) || 
49             typeInfo == typeid(long) || typeInfo == typeid(byte);
50     }
51 
52     static bool isUsignedIntegral(TypeInfo typeInfo) {
53         return typeInfo == typeid(uint) || typeInfo == typeid(ushort) || 
54             typeInfo == typeid(ulong) || typeInfo == typeid(ubyte);
55     }
56 
57     static bool isFloatingPoint(TypeInfo typeInfo) {
58         return typeInfo == typeid(float) || typeInfo == typeid(double) || 
59             typeInfo == typeid(real);
60     }
61 
62     static bool isString(TypeInfo typeInfo) {
63         return typeInfo == typeid(string) || typeInfo == typeid(const(char)[]) ||
64             typeInfo == typeid(const(wchar)[]) || typeInfo == typeid(const(dchar)[]) ||
65             typeInfo == typeid(char[]) || typeInfo == typeid(wchar[]) || 
66             typeInfo == typeid(dchar[]);
67     }
68 
69     /**
70      * Returns the number of zero bits preceding the highest-order
71      * ("leftmost") one-bit in the two's complement binary representation
72      * of the specified {@code int} value.  Returns 32 if the
73      * specified value has no one-bits in its two's complement representation,
74      * in other words if it is equal to zero.
75      *
76      * <p>Note that this method is closely related to the logarithm base 2.
77      * For all positive {@code int} values x:
78      * <ul>
79      * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
80      * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
81      * </ul>
82      *
83      * @param i the value whose number of leading zeros is to be computed
84      * @return the number of zero bits preceding the highest-order
85      *     ("leftmost") one-bit in the two's complement binary representation
86      *     of the specified {@code int} value, or 32 if the value
87      *     is equal to zero.
88      */
89     static int numberOfLeadingZeros(int i) {
90         // HD, Figure 5-6
91         if (i == 0)
92             return 32;
93         int n = 1;
94         if (i >>> 16 == 0) {
95             n += 16;
96             i <<= 16;
97         }
98         if (i >>> 24 == 0) {
99             n += 8;
100             i <<= 8;
101         }
102         if (i >>> 28 == 0) {
103             n += 4;
104             i <<= 4;
105         }
106         if (i >>> 30 == 0) {
107             n += 2;
108             i <<= 2;
109         }
110         n -= i >>> 31;
111         return n;
112     }
113 }