1 
2 
3 //          Copyright Ferdinand Majerech 2011.
4 // Distributed under the Boost Software License, Version 1.0.
5 //    (See accompanying file LICENSE_1_0.txt or copy at
6 //          http://www.boost.org/LICENSE_1_0.txt)
7 
8 module dyaml.escapes;
9 
10 
11 package:
12 
13 ///Translation table from YAML escapes to dchars.
14 // immutable dchar[dchar] fromEscapes;
15 ///Translation table from dchars to YAML escapes.
16 immutable dchar[dchar] toEscapes;
17 // ///Translation table from prefixes of escaped hexadecimal format characters to their lengths.
18 // immutable uint[dchar]  escapeHexCodes;
19 
20 /// All YAML escapes.
21 immutable dchar[] escapes = ['0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ',
22                              '\"', '\\', 'N', '_', 'L', 'P'];
23 
24 /// YAML hex codes specifying the length of the hex number.
25 immutable dchar[] escapeHexCodeList = ['x', 'u', 'U'];
26 
27 /// Covert a YAML escape to a dchar.
28 ///
29 /// Need a function as associative arrays don't work with @nogc.
30 /// (And this may be even faster with a function.)
31 dchar fromEscape(dchar escape) @safe pure nothrow @nogc
32 {
33     switch(escape)
34     {
35         case '0':  return '\0';
36         case 'a':  return '\x07';
37         case 'b':  return '\x08';
38         case 't':  return '\x09';
39         case '\t': return '\x09';
40         case 'n':  return '\x0A';
41         case 'v':  return '\x0B';
42         case 'f':  return '\x0C';
43         case 'r':  return '\x0D';
44         case 'e':  return '\x1B';
45         case ' ':  return '\x20';
46         case '\"': return '\"';
47         case '\\': return '\\';
48         case 'N':  return '\x85'; //'\u0085';
49         case '_':  return '\xA0';
50         case 'L':  return '\u2028';
51         case 'P':  return '\u2029';
52         default:   assert(false, "No such YAML escape");
53     }
54 }
55 
56 /// Get the length of a hexadecimal number determined by its hex code.
57 ///
58 /// Need a function as associative arrays don't work with @nogc.
59 /// (And this may be even faster with a function.)
60 uint escapeHexLength(dchar hexCode) @safe pure nothrow @nogc
61 {
62     switch(hexCode)
63     {
64         case 'x': return 2;
65         case 'u': return 4;
66         case 'U': return 8;
67         default:  assert(false, "No such YAML hex code");
68     }
69 }
70 
71 
72 static this()
73 {
74     toEscapes =
75         ['\0':     '0',
76          '\x07':   'a',
77          '\x08':   'b',
78          '\x09':   't',
79          '\x0A':   'n',
80          '\x0B':   'v',
81          '\x0C':   'f',
82          '\x0D':   'r',
83          '\x1B':   'e',
84          '\"':     '\"',
85          '\\':     '\\',
86          '\u0085': 'N',
87          '\xA0':   '_',
88          '\u2028': 'L',
89          '\u2029': 'P'];
90 }
91