5 <title>CodeMirror: D mode</title>
6 <link rel="stylesheet" href="../../lib/codemirror.css">
7 <script src="../../lib/codemirror.js"></script>
8 <script src="../../addon/edit/matchbrackets.js"></script>
9 <script src="d.js"></script>
10 <link rel="stylesheet" href="../../doc/docs.css">
11 <style>.CodeMirror {border: 2px inset #dee;}</style>
14 <h1>CodeMirror: D mode</h1>
16 <form><textarea id="code" name="code">
17 /* D demo code // copied from phobos/sd/metastrings.d */
18 // Written in the D programming language.
21 Templates with which to do compile-time manipulation of strings.
24 WIKI = Phobos/StdMetastrings
26 Copyright: Copyright Digital Mars 2007 - 2009.
27 License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
28 Authors: $(WEB digitalmars.com, Walter Bright),
30 Source: $(PHOBOSSRC std/_metastrings.d)
33 Copyright Digital Mars 2007 - 2009.
34 Distributed under the Boost Software License, Version 1.0.
35 (See accompanying file LICENSE_1_0.txt or copy at
36 http://www.boost.org/LICENSE_1_0.txt)
38 module std.metastrings;
41 Formats constants into a string at compile time. Analogous to $(XREF
46 A = tuple of constants, which can be strings, characters, or integral
50 * The formats supported are %s for strings, and %%
51 * for the % character.
54 import std.metastrings;
59 string s = Format!("Arg %s = %s", "foo", 27);
60 writefln(s); // "Arg foo = 27"
67 static if (A.length == 0)
69 else static if (is(typeof(A[0]) : const(char)[]))
70 enum Format = FormatString!(A[0], A[1..$]);
72 enum Format = toStringNow!(A[0]) ~ Format!(A[1..$]);
75 template FormatString(const(char)[] F, A...)
77 static if (F.length == 0)
78 enum FormatString = Format!(A);
79 else static if (F.length == 1)
80 enum FormatString = F[0] ~ Format!(A);
81 else static if (F[0..2] == "%s")
83 = toStringNow!(A[0]) ~ FormatString!(F[2..$],A[1..$]);
84 else static if (F[0..2] == "%%")
85 enum FormatString = "%" ~ FormatString!(F[2..$],A);
88 static assert(F[0] != '%', "unrecognized format %" ~ F[1]);
89 enum FormatString = F[0] ~ FormatString!(F[1..$],A);
95 auto s = Format!("hel%slo", "world", -138, 'c', true);
96 assert(s == "helworldlo-138ctrue", "[" ~ s ~ "]");
100 * Convert constant argument to a string.
103 template toStringNow(ulong v)
106 enum toStringNow = "" ~ cast(char)(v + '0');
108 enum toStringNow = toStringNow!(v / 10) ~ toStringNow!(v % 10);
113 static assert(toStringNow!(1uL << 62) == "4611686018427387904");
117 template toStringNow(long v)
120 enum toStringNow = "-" ~ toStringNow!(cast(ulong) -v);
122 enum toStringNow = toStringNow!(cast(ulong) v);
127 static assert(toStringNow!(0x100000000) == "4294967296");
128 static assert(toStringNow!(-138L) == "-138");
132 template toStringNow(uint U)
134 enum toStringNow = toStringNow!(cast(ulong)U);
138 template toStringNow(int I)
140 enum toStringNow = toStringNow!(cast(long)I);
144 template toStringNow(bool B)
146 enum toStringNow = B ? "true" : "false";
150 template toStringNow(string S)
152 enum toStringNow = S;
156 template toStringNow(char C)
158 enum toStringNow = "" ~ C;
163 * Parse unsigned integer literal from the start of string s.
165 * .value = the integer literal as a string,
166 * .rest = the string following the integer literal
172 template parseUinteger(const(char)[] s)
174 static if (s.length == 0)
179 else static if (s[0] >= '0' && s[0] <= '9')
181 enum value = s[0] ~ parseUinteger!(s[1..$]).value;
182 enum rest = parseUinteger!(s[1..$]).rest;
192 Parse integer literal optionally preceded by $(D '-') from the start
196 .value = the integer literal as a string,
197 .rest = the string following the integer literal
204 template parseInteger(const(char)[] s)
206 static if (s.length == 0)
211 else static if (s[0] >= '0' && s[0] <= '9')
213 enum value = s[0] ~ parseUinteger!(s[1..$]).value;
214 enum rest = parseUinteger!(s[1..$]).rest;
216 else static if (s.length >= 2 &&
217 s[0] == '-' && s[1] >= '0' && s[1] <= '9')
219 enum value = s[0..2] ~ parseUinteger!(s[2..$]).value;
220 enum rest = parseUinteger!(s[2..$]).rest;
231 assert(parseUinteger!("1234abc").value == "1234");
232 assert(parseUinteger!("1234abc").rest == "abc");
233 assert(parseInteger!("-1234abc").value == "-1234");
234 assert(parseInteger!("-1234abc").rest == "abc");
238 Deprecated aliases held for backward compatibility.
240 deprecated alias toStringNow ToString;
242 deprecated alias parseUinteger ParseUinteger;
244 deprecated alias parseUinteger ParseInteger;
249 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
257 <p>Simple mode that handle D-Syntax (<a href="http://www.dlang.org">DLang Homepage</a>).</p>
259 <p><strong>MIME types defined:</strong> <code>text/x-d</code>