1 /**
2  * Copyright: Copyright (c) 2010-2011 Jacob Carlborg.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Jan 26, 2010
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module orange.util.CTFE;
8 
9 import orange.util.Traits;
10 
11 /// Compile time string converter. Converts the given arguments to a string.
12 template format (ARGS...)
13 {
14     static if (ARGS.length == 0)
15         enum format = "";
16 
17     else
18     {
19         static if (is(typeof(ARGS[0]) : string))
20             enum format = ARGS[0] ~ format!(ARGS[1 .. $]);
21 
22         else
23             enum format = toString_!(ARGS[0]) ~ format!(ARGS[1 .. $]);
24     }
25 }
26 
27 private
28 {
29     template toString_ (T)
30     {
31         enum toString_ = T.stringof;
32     }
33 
34     template toString_ (int i)
35     {
36         enum toString_ = itoa!(i);
37     }
38 
39     template toString_ (long l)
40     {
41         enum toString_ = itoa!(l);
42     }
43 
44     template toString_ (bool b)
45     {
46         enum toString_ = b ? "true" : "false";
47     }
48 
49     template toString_ (float f)
50     {
51         enum toString_ = "";
52     }
53 
54     template toString_ (alias a)
55     {
56         enum toString_ = a.stringof;
57     }
58 }
59 
60 /**
61  * Compile-time function to get the index of the give element.
62  *
63  * Performs a linear scan, returning the index of the first occurrence
64  * of the specified element in the array, or U.max if the array does
65  * not contain the element.
66  *
67  * Params:
68  *     arr = the array to get the index of the element from
69  *     element = the element to find
70  *
71  * Returns: the index of the element or size_t.max if the element was not found.
72  */
73 size_t indexOf (T) (T[] arr, T element)
74 {
75     static if (is(T == char) || is(T == wchar) || is(T == dchar))
76     {
77         foreach (i, e ; arr)
78             if (e == element)
79                 return i;
80     }
81 
82     else
83     {
84         foreach (i, e ; arr)
85             if (e == element)
86                 return i;
87     }
88 
89     return size_t.max;
90 }
91 
92 /**
93  * Returns true if the given array contains the given element,
94  * otherwise false.
95  *
96  * Params:
97  *     arr = the array to search in for the element
98  *     element = the element to search for
99  *
100  * Returns: true if the array contains the element, otherwise false
101  */
102 bool contains (T) (T[] arr, T element)
103 {
104     return indexOf(arr, element) != size_t.max;
105 }
106 
107 private:
108 
109 template decimalDigit (int n)    // [3]
110 {
111     enum decimalDigit = "0123456789"[n .. n + 1];
112 }
113 
114 template itoa (long n)
115 {
116     static if (n < 0)
117         enum itoa = "-" ~ itoa!(-n);
118 
119     else static if (n < 10)
120         enum itoa = decimalDigit!(n);
121 
122     else
123         enum itoa = itoa!(n / 10L) ~ decimalDigit!(n % 10L);
124 }