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 package: 11 12 import std.meta : AliasSeq; 13 alias escapes = AliasSeq!('0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ', 14 '\"', '\\', 'N', '_', 'L', 'P'); 15 16 /// YAML hex codes specifying the length of the hex number. 17 alias escapeHexCodeList = AliasSeq!('x', 'u', 'U'); 18 19 /// Convert a YAML escape to a dchar. 20 dchar fromEscape(dchar escape) @safe pure nothrow @nogc 21 { 22 switch(escape) 23 { 24 case '0': return '\0'; 25 case 'a': return '\x07'; 26 case 'b': return '\x08'; 27 case 't': return '\x09'; 28 case '\t': return '\x09'; 29 case 'n': return '\x0A'; 30 case 'v': return '\x0B'; 31 case 'f': return '\x0C'; 32 case 'r': return '\x0D'; 33 case 'e': return '\x1B'; 34 case ' ': return '\x20'; 35 case '\"': return '\"'; 36 case '\\': return '\\'; 37 case 'N': return '\x85'; //'\u0085'; 38 case '_': return '\xA0'; 39 case 'L': return '\u2028'; 40 case 'P': return '\u2029'; 41 default: assert(false, "No such YAML escape"); 42 } 43 } 44 45 /** 46 * Convert a dchar to a YAML escape. 47 * 48 * Params: 49 * value = The possibly escapable character. 50 * 51 * Returns: 52 * If the character passed as parameter can be escaped, returns the matching 53 * escape, otherwise returns a null character. 54 */ 55 dchar toEscape(dchar value) @safe pure nothrow @nogc 56 { 57 switch(value) 58 { 59 case '\0': return '0'; 60 case '\x07': return 'a'; 61 case '\x08': return 'b'; 62 case '\x09': return 't'; 63 case '\x0A': return 'n'; 64 case '\x0B': return 'v'; 65 case '\x0C': return 'f'; 66 case '\x0D': return 'r'; 67 case '\x1B': return 'e'; 68 case '\"': return '\"'; 69 case '\\': return '\\'; 70 case '\xA0': return '_'; 71 case '\x85': return 'N'; 72 case '\u2028': return 'L'; 73 case '\u2029': return 'P'; 74 default: return 0; 75 } 76 } 77 78 /// Get the length of a hexadecimal number determined by its hex code. 79 /// 80 /// Need a function as associative arrays don't work with @nogc. 81 /// (And this may be even faster with a function.) 82 uint escapeHexLength(dchar hexCode) @safe pure nothrow @nogc 83 { 84 switch(hexCode) 85 { 86 case 'x': return 2; 87 case 'u': return 4; 88 case 'U': return 8; 89 default: assert(false, "No such YAML hex code"); 90 } 91 } 92