1 /** Contains common types and constants. 2 */ 3 module diet.defs; 4 5 import diet.dom; 6 import memutils.vector; 7 import std.traits; 8 import memutils.scoped; 9 10 nothrow: 11 /** The name of the output range variable within a Diet template. 12 13 D statements can access the variable with this name to directly write to the 14 output. 15 */ 16 enum dietOutputRangeName = "_diet_output"; 17 18 19 /// Thrown by the parser for malformed input. 20 alias DietParserException = Exception; 21 22 void put(T, S)(ref T dst, S content) { 23 dst.insertBack(content); 24 } 25 26 string formattedWrite(string format, T, Args...)(ref T buffer, Args args) @trusted 27 { 28 import fast.format; 29 import ldc.intrinsics; 30 int sz = format.length; 31 sz -= (args.length*2); 32 static foreach(arg; args) { 33 static if (isIntegral!(typeof(arg)) || isFloatingPoint!(typeof(arg))) { 34 sz += decCharsVal(arg); 35 } 36 else static if (isSomeString!(typeof(arg))) { 37 sz += arg.length; 38 } 39 else static if (isSomeChar!(typeof(arg))) { 40 sz += 1; 41 } 42 43 } 44 45 buffer.length = sz; 46 47 auto buf = fast.format.formattedWrite!format(buffer.ptr, args); 48 return cast(string)buf; 49 } 50 51 string format(string fmt, Args...)(Args args) @trusted { 52 Vector!char ret; 53 formattedWrite!fmt(ret, args); 54 return ret[].copy(); 55 } 56 57 /** Throws an exception if the condition evaluates to `false`. 58 59 This function will generate a proper error message including file and line 60 number when called at compile time. An assertion is used in this case 61 instead of an exception: 62 63 Throws: 64 Throws a `DietParserException` when called with a `false` condition at 65 run time. 66 */ 67 68 void enforcep(bool cond, string text, in Location loc) @trusted 69 { 70 Vector!char err; 71 err.formattedWrite!"%s (%d): %s"(loc.file[], loc.line+1, text); 72 assert(cond, err[]); 73 }