comparison orange/util/CTFE.d @ 1:11a31bd929f9

Removed dependency on private library
author Jacob Carlborg <doob@me.com>
date Mon, 31 May 2010 16:06:36 +0200
parents f7b078e85f7f
children ea37a9470e3e
comparison
equal deleted inserted replaced
0:f7b078e85f7f 1:11a31bd929f9
4 * Version: Initial created: Jan 26, 2010 4 * Version: Initial created: Jan 26, 2010
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6 */ 6 */
7 module orange.util.CTFE; 7 module orange.util.CTFE;
8 8
9 public import mambo.util.CTFE; 9 import orange.util.string;
10 import orange.util.Traits;
11
12 template format (ARGS...)
13 {
14 static if (ARGS.length == 0)
15 const format = "";
16
17 else
18 {
19 static if (is(typeof(ARGS[0]) : string))
20 const format = ARGS[0] ~ format!(ARGS[1 .. $]);
21
22 else
23 {
24 pragma(msg, typeof(ARGS[0].stringof));
25 const format = toString_!(ARGS[0]) ~ format!(ARGS[1 .. $]);
26 }
27
28 }
29 }
30
31 private
32 {
33 template toString_ (T)
34 {
35 const toString_ = T.stringof;
36 }
37
38 template toString_ (int i)
39 {
40 const toString_ = itoa!(i);
41 }
42
43 template toString_ (long l)
44 {
45 const toString_ = itoa!(l);
46 }
47
48 template toString_ (bool b)
49 {
50 const toString_ = b ? "true" : "false";
51 }
52
53 template toString_ (float f)
54 {
55 const toString_ = "";
56 }
57
58 template toString_ (alias a)
59 {
60 const toString_ = a.stringof;
61 }
62 }
63
64 /**
65 * Compile-time function to get the index of the give element.
66 *
67 * Performs a linear scan, returning the index of the first occurrence
68 * of the specified element in the array, or U.max if the array does
69 * not contain the element.
70 *
71 * Params:
72 * arr = the array to get the index of the element from
73 * element = the element to find
74 *
75 * Returns: the index of the element or size_t.max if the element was not found.
76 */
77 size_t indexOf (T) (T[] arr, T element)
78 {
79 static if (is(T == char) || is(T == wchar) || is(T == dchar))
80 {
81 foreach (i, e ; arr)
82 if (e == element)
83 return i;
84 }
85
86 else
87 {
88 foreach (i, e ; arr)
89 if (e == element)
90 return i;
91 }
92
93 return size_t.max;
94 }
95
96 /**
97 * Returns true if the given array contains the given element,
98 * otherwise false.
99 *
100 * Params:
101 * arr = the array to search in for the element
102 * element = the element to search for
103 *
104 * Returns: true if the array contains the element, otherwise false
105 */
106 bool contains (T) (T[] arr, T element)
107 {
108 return arr.indexOf(element) != size_t.max;
109 }
110
111 /**
112 * CTFE, splits the given string on the given pattern
113 *
114 * Params:
115 * str = the string to split
116 * splitChar = the character to split on
117 *
118 * Returns: an array of strings containing the splited string
119 */
120 T[][] split (T) (T[] str, T splitChar = ',')
121 {
122 T[][] arr;
123 size_t x;
124
125 foreach (i, c ; str)
126 {
127 if (splitChar == c)
128 {
129 if (str[x] == splitChar)
130 x++;
131
132 arr ~= str[x .. i];
133 x = i;
134 }
135 }
136
137 if (str[x] == splitChar)
138 x++;
139
140 arr ~= str[x .. $];
141
142 return arr;
143 }
144
145 private:
146
147 template decimalDigit (int n) // [3]
148 {
149 const decimalDigit = "0123456789"[n .. n + 1];
150 }
151
152 template itoa (long n)
153 {
154 static if (n < 0)
155 const itoa = "-" ~ itoa!(-n);
156
157 else static if (n < 10)
158 const itoa = decimalDigit!(n);
159
160 else
161 const itoa = itoa!(n / 10L) ~ decimalDigit!(n % 10L);
162 }