Mercurial > projects > orange
diff 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 |
line wrap: on
line diff
--- a/orange/util/CTFE.d Wed May 26 17:19:13 2010 +0200 +++ b/orange/util/CTFE.d Mon May 31 16:06:36 2010 +0200 @@ -6,4 +6,157 @@ */ module orange.util.CTFE; -public import mambo.util.CTFE; \ No newline at end of file +import orange.util.string; +import orange.util.Traits; + +template format (ARGS...) +{ + static if (ARGS.length == 0) + const format = ""; + + else + { + static if (is(typeof(ARGS[0]) : string)) + const format = ARGS[0] ~ format!(ARGS[1 .. $]); + + else + { + pragma(msg, typeof(ARGS[0].stringof)); + const format = toString_!(ARGS[0]) ~ format!(ARGS[1 .. $]); + } + + } +} + +private +{ + template toString_ (T) + { + const toString_ = T.stringof; + } + + template toString_ (int i) + { + const toString_ = itoa!(i); + } + + template toString_ (long l) + { + const toString_ = itoa!(l); + } + + template toString_ (bool b) + { + const toString_ = b ? "true" : "false"; + } + + template toString_ (float f) + { + const toString_ = ""; + } + + template toString_ (alias a) + { + const toString_ = a.stringof; + } +} + +/** + * Compile-time function to get the index of the give element. + * + * Performs a linear scan, returning the index of the first occurrence + * of the specified element in the array, or U.max if the array does + * not contain the element. + * + * Params: + * arr = the array to get the index of the element from + * element = the element to find + * + * Returns: the index of the element or size_t.max if the element was not found. + */ +size_t indexOf (T) (T[] arr, T element) +{ + static if (is(T == char) || is(T == wchar) || is(T == dchar)) + { + foreach (i, e ; arr) + if (e == element) + return i; + } + + else + { + foreach (i, e ; arr) + if (e == element) + return i; + } + + return size_t.max; +} + +/** + * Returns true if the given array contains the given element, + * otherwise false. + * + * Params: + * arr = the array to search in for the element + * element = the element to search for + * + * Returns: true if the array contains the element, otherwise false + */ +bool contains (T) (T[] arr, T element) +{ + return arr.indexOf(element) != size_t.max; +} + +/** + * CTFE, splits the given string on the given pattern + * + * Params: + * str = the string to split + * splitChar = the character to split on + * + * Returns: an array of strings containing the splited string + */ +T[][] split (T) (T[] str, T splitChar = ',') +{ + T[][] arr; + size_t x; + + foreach (i, c ; str) + { + if (splitChar == c) + { + if (str[x] == splitChar) + x++; + + arr ~= str[x .. i]; + x = i; + } + } + + if (str[x] == splitChar) + x++; + + arr ~= str[x .. $]; + + return arr; +} + +private: + +template decimalDigit (int n) // [3] +{ + const decimalDigit = "0123456789"[n .. n + 1]; +} + +template itoa (long n) +{ + static if (n < 0) + const itoa = "-" ~ itoa!(-n); + + else static if (n < 10) + const itoa = decimalDigit!(n); + + else + const itoa = itoa!(n / 10L) ~ decimalDigit!(n % 10L); +} \ No newline at end of file