Merge pull request #172 from BBasile/esc-sw

convert toEscapes AA to a switch for faster convertion
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2018-06-22 05:34:58 +02:00 committed by GitHub
commit 8f9dafdef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 34 deletions

View file

@ -1384,10 +1384,10 @@ struct ScalarWriter
if(c != dcharNone) if(c != dcharNone)
{ {
auto appender = appender!string(); auto appender = appender!string();
if((c in dyaml.escapes.toEscapes) !is null) if(const dchar es = toEscape(c))
{ {
appender.put('\\'); appender.put('\\');
appender.put(dyaml.escapes.toEscapes[c]); appender.put(es);
} }
else else
{ {

View file

@ -7,16 +7,8 @@
module dyaml.escapes; module dyaml.escapes;
package: package:
///Translation table from YAML escapes to dchars.
// immutable dchar[dchar] fromEscapes;
///Translation table from dchars to YAML escapes.
immutable dchar[dchar] toEscapes;
// ///Translation table from prefixes of escaped hexadecimal format characters to their lengths.
// immutable uint[dchar] escapeHexCodes;
/// All YAML escapes. /// All YAML escapes.
immutable dchar[] escapes = ['0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ', immutable dchar[] escapes = ['0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ',
'\"', '\\', 'N', '_', 'L', 'P']; '\"', '\\', 'N', '_', 'L', 'P'];
@ -24,10 +16,7 @@ immutable dchar[] escapes = ['0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e',
/// YAML hex codes specifying the length of the hex number. /// YAML hex codes specifying the length of the hex number.
immutable dchar[] escapeHexCodeList = ['x', 'u', 'U']; immutable dchar[] escapeHexCodeList = ['x', 'u', 'U'];
/// Covert a YAML escape to a dchar. /// Convert a YAML escape to a dchar.
///
/// Need a function as associative arrays don't work with @nogc.
/// (And this may be even faster with a function.)
dchar fromEscape(dchar escape) @safe pure nothrow @nogc dchar fromEscape(dchar escape) @safe pure nothrow @nogc
{ {
switch(escape) switch(escape)
@ -53,6 +42,39 @@ dchar fromEscape(dchar escape) @safe pure nothrow @nogc
} }
} }
/**
* Convert a dchar to a YAML escape.
*
* Params:
* value = The possibly escapable character.
*
* Returns:
* If the character passed as parameter can be escaped, returns the matching
* escape, otherwise returns a null character.
*/
dchar toEscape(dchar value) @safe pure nothrow @nogc
{
switch(value)
{
case '\0': return '0';
case '\x07': return 'a';
case '\x08': return 'b';
case '\x09': return 't';
case '\x0A': return 'n';
case '\x0B': return 'v';
case '\x0C': return 'f';
case '\x0D': return 'r';
case '\x1B': return 'e';
case '\"': return '\"';
case '\\': return '\\';
case '\xA0': return '_';
case '\x85': return 'N';
case '\u2028': return 'L';
case '\u2029': return 'P';
default: return 0;
}
}
/// Get the length of a hexadecimal number determined by its hex code. /// Get the length of a hexadecimal number determined by its hex code.
/// ///
/// Need a function as associative arrays don't work with @nogc. /// Need a function as associative arrays don't work with @nogc.
@ -68,23 +90,3 @@ uint escapeHexLength(dchar hexCode) @safe pure nothrow @nogc
} }
} }
static this()
{
toEscapes =
['\0': '0',
'\x07': 'a',
'\x08': 'b',
'\x09': 't',
'\x0A': 'n',
'\x0B': 'v',
'\x0C': 'f',
'\x0D': 'r',
'\x1B': 'e',
'\"': '\"',
'\\': '\\',
'\u0085': 'N',
'\xA0': '_',
'\u2028': 'L',
'\u2029': 'P'];
}