annotate mde/text/format.d @ 6:dcb24afa0dce

Some fixes from mde/text/format.d unittests plus a few more fixes. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 10 Jan 2008 18:33:24 +0000
parents 9a990644948c
children b544c3a7c9ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /**************************************************************************************************
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 * This contains templates for converting various data-types to a char[].
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 *
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
4 * Copyright: Copyright © 2007 Diggory Hardy.
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
5 * Authors: Diggory Hardy, diggory.hardy@gmail.com
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
6 * License: Licensed under the Academic Free License version 3.0
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 *
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8 * This module basically implements the following templated function for $(B most) basic D types:
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 * bool, byte, short, int, long, ubyte, ushort, uint, ulong, float, double, real, char.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 * It also supports arrays of any supported type (including of other arrays) and has special
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 * handling for strings (char[]) and binary (ubyte[]) data-types.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12 * -----------------------------
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 * char[] format(T) (T value);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 * -----------------------------
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15 *
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 * There are also a few utility functions defined; the public ones have their own documentation.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 *
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 * On errors, a warning is logged and an TextConvertException is thrown. No other exceptions should
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 * be thrown and none thrown from functions used outside this module.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 *************************************************************************************************/
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 module mde.text.format;
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
22 // TODO: write unittests; check strings generate quotes.
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 // package imports
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 import mde.text.exception;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27 // tango imports
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 import cInt = tango.text.convert.Integer;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 import cFloat = tango.text.convert.Float;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 import Utf = tango.text.convert.Utf;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 import Util = tango.text.Util;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32 import tango.util.log.Log : Log, Logger;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 private Logger logger;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 static this () {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 logger = Log.getLogger ("mde.text.format");
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 //BEGIN Convert templates
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 /* Idea: could extend format with a second parameter, containing flags for things like base to output.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 * Unnecessary for mergetag though.
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
42 */
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
43
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
44 // Associative arrays
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
45 char[] format(T : T[S], S) (T[S] val) {
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
46 char[] ret;
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
47 ret.length = val.length * (defLength!(T) + defLength!(S)) + 2;
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
48 ret[0] = '[';
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
49 uint i = 1;
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
50 foreach (S k, T v; val) {
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
51 char[] s = format!(S) (k) ~ ":" ~ format!(T) (v);
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
52 i += s.length;
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
53 if (i+1 >= ret.length) ret.length = ret.length * 2; // check.
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
54 ret[i-s.length .. i] = s;
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
55 ret[i++] = ',';
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
56 }
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
57 if (i == 1) ++i; // special case - not overwriting a comma
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
58 ret[i-1] = ']'; // replaces last comma
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
59 return ret[0..i];
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
60 }
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
61 unittest {
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
62 char[] X = format!(char[][char]) (['a':cast(char[])"animal", 'b':['b','u','s']]);
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
63 char[] Y = `['a':"animal",'b':"bus"]`;
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
64 assert (X == Y);
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
65 }
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
66
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67 // Arrays
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 char[] format(T : T[]) (T[] val) {
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
69 char[] ret;
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
70 ret.length = val.length * defLength!(T) + 2;
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71 ret[0] = '[';
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
72 uint i = 1;
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 foreach (T x; val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 char[] s = format!(T) (x);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 i += s.length;
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
76 if (i+1 >= ret.length) ret.length = ret.length * 2; // check.
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 ret[i-s.length .. i] = s;
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
78 ret[i++] = ',';
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
80 if (i == 1) ++i; // special case - not overwriting a comma
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
81 ret[i-1] = ']'; // replaces last comma
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 return ret[0..i];
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
84 char[] format(T : dchar[]) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 return format (toUtf8 (val));
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87 char[] format(T : wchar[]) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 return format (toUtf8 (val));
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90 char[] format(T : char[]) (T val) {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
91 char[] ret = new char[val.length * 2 + 2]; // Initial storage. This should ALWAYS be enough.
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92 ret[0] = '"';
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
93 uint i = 1;
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 for (uint t = 0; t < val.length;) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 // process a block of non-escapable characters
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96 uint s = t;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
97 while (t < val.length && !isEscapableChar(val[t]))
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 ++t; // skip all non-escapable chars
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 uint j = i + t - s;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 ret[i..j] = val[s..t]; // copy a block
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101 i = j;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 // process a block of escapable charaters
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 while (t < val.length && isEscapableChar(val[t])) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 ret[i++] = '\\'; // backslash; increment i
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
105 ret[i++] = replaceEscapableChar(val[t++]); // character; increment i and t
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
108 ret[i++] = '"';
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 return ret[0..i];
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
111 char[] format(T : ubyte[]) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
112 static const char[16] digits = "0123456789abcdef";
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
113
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
114 char[] ret = new char[val.length * 2]; // exact length
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
115 uint i = 0;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
116 foreach (ubyte x; val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
117 ret[i++] = digits[x >> 4];
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
118 ret[i++] = digits[x & 0x0F];
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
120 return ret;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
121 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
122 unittest {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
123 // generic array stuff:
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
124 assert (format!(double[]) ([1.0, 1.0e-10]) == `[1.00000000000000000,0.10000000000000000e-09]`);
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
125 assert (format!(double[]) (cast(double[]) []) == `[]`); // empty array
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
126
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
127 // char[] conversions, with commas, escape sequences and multichar UTF8 characters:
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
128 assert (format!(char[][]) ([ ".\""[], [',','\''] ,"!\b€" ]) == `[".\"",",\'","!\b€"]`);
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
129
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
130 assert (format!(ubyte[]) (cast(ubyte[]) [0x01, 0xF2, 0xAC]) == `01f2ac`); // ubyte[] special notation
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
131 }
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
132
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
133 // Support for outputting a wide char... I reccomend against trying to output these though.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
134 const char[] WIDE_CHAR_ERROR = "Error: unicode non-ascii character cannot be converted to a single UTF-8 char";
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
135 char[] format(T : dchar) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
136 if (val <= 127u) return format (cast(char) val); // this char can be converted
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
137 throwException (WIDE_CHAR_ERROR);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
138 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
139 char[] format(T : wchar) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
140 if (val <= 127u) return format (cast(char) val); // this char can be converted
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
141 throwException (WIDE_CHAR_ERROR);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
142 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
143 char[] format(T : char) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
144 // Note: if (val > 127) "is invalid UTF-8 single char"
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
145 // However we don't know what this is for, in particular if it will be recombined with other chars later
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
146
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
147 // Can't return reference to static array; making dynamic is cheaper than copying.
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
148 char[] ret = new char[4]; // max length for an escaped char
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
149 ret[0] = '\'';
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
150
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
151 if (!isEscapableChar (val)) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
152 ret[1] = val;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
153 ret[2] = '\'';
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
154 return ret[0..3];
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
155 } else {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
156 ret[1] = '\\';
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
157 ret[2] = replaceEscapableChar (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
158 ret[3] = '\'';
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
159 return ret;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
160 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
161 assert (false);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
162 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
163 unittest {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
164 assert (format!(char) ('\'') == "\'\\\'\'");
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
165 }
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
166
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
167 char[] format(T : bool) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
168 if (T) return "true";
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
169 else return "false";
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
170 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
171 // too simple to need a unittest
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
172
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
173 char[] format(T : byte) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
174 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
175 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
176 char[] format(T : short) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
177 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
178 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
179 char[] format(T : int) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
180 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
181 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
182 char[] format(T : long) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
183 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
184 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
185 char[] format(T : ubyte) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
186 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
187 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
188 char[] format(T : ushort) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
189 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
190 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
191 char[] format(T : uint) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
192 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
193 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
194 char[] format(T : ulong) (T val) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
195 if (val > cast(ulong) long.max) throwException ("No handling available for ulong where value > long.max");
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
196 return formatLong (val);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
197 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
198 unittest {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
199 assert (format!(byte) (cast(byte) -5) == "-5");
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
200 // annoyingly, octal syntax differs from D (blame tango):
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
201 assert (format!(uint[]) ([0b0100u,0724,0xFa59c,0xFFFFFFFF,0]) == "[4,468,1025436,4294967295,0]");
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
202 }
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
203
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
204 // Old calculation (not used):
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
205 // t.dig+2+4+3 // should be sufficient length (mant + (neg, dot, e, exp neg) + exp (3,4,5 for float,double,real resp.))
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
206 char[] format(T : float) (T val) {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
207 char[] ret = new char[32]; // minimum allowed by assert in format
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
208 return cFloat.format (ret, val, T.dig+2, 1); // from old C++ tests, T.dig+2 gives best(?) accuracy
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
209 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
210 char[] format(T : double) (T val) {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
211 char[] ret = new char[32];
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
212 return cFloat.format (ret, val, T.dig+2, 1);
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
213 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
214 char[] format(T : real) (T val) {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
215 char[] ret = new char[32];
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
216 return cFloat.format (ret, val, T.dig+2, 1);
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
217 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
218 unittest {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
219 // NOTE: these numbers are not particularly meaningful.
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
220 assert (format!(float) (0.0f) == "0.00000000");
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
221 assert (format!(double) (-1e25) == "-1.00000000000000000e+25");
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
222 assert (format!(real) (cast(real) 4.918e300) == "4.91800000000000000000e+300");
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
223 }
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
224 //END Convert templates
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
225
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
226 //BEGIN Length templates
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
227 /* This template provides the initial length for strings for formatting various types. These strings
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
228 * can be expanded; this value should cover 90% of cases or so.
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
229 * FIXME: provide more specialisations (or not?)
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
230 */
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
231 private {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
232 template defLength(T) { const uint defLength = 20; }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
233 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
234 //END Length templates
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
235
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
236 //BEGIN Utility funcs
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
237 private char[] formatLong (long val) {
6
dcb24afa0dce Some fixes from mde/text/format.d unittests plus a few more fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 4
diff changeset
238 try return cInt.toString (val, cInt.Style.Signed, cInt.Flags.Throw);
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
239 catch (Exception e) throwException (e.msg);
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
240 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
241 private bool isEscapableChar (char c) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
242 return ((c <= '\r' && c >= '\a') || c == '\"' || c == '\'' || c == '\\');
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
243 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
244 // Warning: this DOES NOT check c is escapable
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
245 private char replaceEscapableChar (char c) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
246 static char[char] escCharsRev; // reversed escChars
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
247 static bool escCharsRevFilled; // will be initialised false
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
248
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
249 if (!escCharsRevFilled) { // only do this once
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
250 // map of all supported escape sequences
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
251 escCharsRev = ['"' : '"', '\'' : '\'',
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
252 '\\' : '\\', '\a' : 'a',
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
253 '\b' : 'b', '\f' : 'f',
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
254 '\n' : 'n', '\r' : 'r',
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
255 '\t' : 't', '\v' : 'v'];
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
256 escCharsRevFilled = true;
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
257 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
258
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
259 return escCharsRev[c];
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
260 }
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
261
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
262 private void throwException (char[] msg) {
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
263 logger.warn (msg); // only small errors are trapped here
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
264 throw new TextFormatException ();
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
265 }
4
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
266
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
267 unittest {
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
268 // all utility functions should be well-enough used not to need testing
9a990644948c Many changes: upgraded to tango 0.99.4, reorganised mde/input, large changes to mde/mergetag and mde/init, separated off test/MTTest.d and more.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 1
diff changeset
269 }
1
18491334a525 Finished format.d and parse.d modules; moved to mde/text. Partway implementing mde.mergetag.write.TextWriter.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
270 //END Utility funcs