module DPrint; /// Convert an integer of type T to string. /// Note: this function is CTFE-compatible, but not very efficient at runtime char [] ct_itoa(T)(T x) { char [] s=""; static if (is(T==byte)||is(T==short)||is(T==int)||is(T==long)) { if (x<0) { s = "-"; x = -x; } } do { s = cast(char)('0' + (x%10)) ~ s; x/=10; } while (x>0); return s; } // Templates to return string representation of floating-point constant. // This works, but only for floating-point constants -- should really create a // CTFE-compatible ftoa(). char [] mixin_ftoa(real x)() { return `"` ~ x.stringof ~ `"`; } char [] mixin_ftoa(ireal x)() { return `"` ~ x.stringof ~ `"`; } char [] mixin_ftoa(creal x)() { return `"` ~ x.stringof ~ `"`; } /** Evaluate a textual expression of type T, and return it as a textual literal. * */ char [] dollar_convert(T)(char [] x) { static if (is(T ==long)) return `"%Ld"`; else static if (is(T==ulong)) return `"%Lu"`; else static if (is(T:int)) return `"%d"`; else static if (is(T:uint)) return `"%u"`; else static if (is(T==real)) return `"%Lg"`; else static if (is(T:double)) return `"%g"`; else static if (is(T==char[])) return `"%.*s"`; else return x; } private { // BUGS: Many other UTF chars should be allowed inside identifiers. bool isIdentifierChar(char c) { return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || (c=='_'); } } /** Evaluate a string containing embedded 'dollar expressions'. * * Given a string containing embedded expression marked with 'dollar' indicators, * returns a string which, when mixed in, will evaluate to a new string with all * of the expressions evaluated. * The expressions can be strings, or any integral type, or a floating-point * constant (real, imaginary or complex) expression. * Dollar expressions consist of a dollar sign, followed by a variable name * (the first non-identifier character marks the end), or a dollar sign followed * by an expression in parentheses. Two consecutive dollar signs become a dollar * character. */ char [] dprint(char [] s) { char [] result=`printf(("`; char [] vars=""; int i=0; while (i