Minor fixes in examples and the 'custom types' tutorial.
This commit is contained in:
parent
278b60e896
commit
6ccda33467
|
@ -202,6 +202,7 @@ Add this to your code to add implicit resolution of ``!color``.
|
|||
//code from the previous example...
|
||||
|
||||
auto resolver = new Resolver;
|
||||
import std.regex;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
|
@ -302,6 +303,7 @@ such as the color being white.
|
|||
representer.addRepresenter!Color(&representColor);
|
||||
|
||||
auto resolver = new Resolver;
|
||||
import std.regex;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import std.ascii;
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import dyaml.all;
|
||||
|
@ -32,6 +31,7 @@ Color constructColorScalar(ref Node node)
|
|||
//Get value of a hex digit.
|
||||
uint hex(char c)
|
||||
{
|
||||
import std.ascii;
|
||||
if(!std.ascii.isHexDigit(c))
|
||||
{
|
||||
throw new Exception("Invalid color: " ~ value);
|
||||
|
@ -83,9 +83,9 @@ void main()
|
|||
|
||||
auto root = loader.load();
|
||||
|
||||
if(root["scalar-red"].as!Color == red &&
|
||||
root["mapping-red"].as!Color == red &&
|
||||
root["scalar-orange"].as!Color == orange &&
|
||||
if(root["scalar-red"].as!Color == red &&
|
||||
root["mapping-red"].as!Color == red &&
|
||||
root["scalar-orange"].as!Color == orange &&
|
||||
root["mapping-orange"].as!Color == orange)
|
||||
{
|
||||
writeln("SUCCESS");
|
||||
|
|
|
@ -1,4 +1,2 @@
|
|||
Hello World :
|
||||
- Hello
|
||||
- World
|
||||
Hello World : [Hello, World]
|
||||
Answer : 42
|
||||
|
|
|
@ -27,7 +27,7 @@ Node representColor(ref Node node, Representer representer)
|
|||
string scalar;
|
||||
foreach(channel; [color.red, color.green, color.blue])
|
||||
{
|
||||
scalar ~= hex[channel / 16];
|
||||
scalar ~= hex[channel / 16];
|
||||
scalar ~= hex[channel % 16];
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@ void main()
|
|||
representer.addRepresenter!Color(&representColor);
|
||||
|
||||
auto resolver = new Resolver;
|
||||
import std.regex;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
|
@ -50,8 +51,8 @@ void main()
|
|||
dumper.representer = representer;
|
||||
dumper.resolver = resolver;
|
||||
|
||||
auto document = Node([Color(255, 0, 0),
|
||||
Color(0, 255, 0),
|
||||
auto document = Node([Color(255, 0, 0),
|
||||
Color(0, 255, 0),
|
||||
Color(0, 0, 255)]);
|
||||
|
||||
dumper.dump(document);
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import std.ascii;
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import dyaml.all;
|
||||
|
@ -32,6 +31,7 @@ Color constructColorScalar(ref Node node)
|
|||
//Get value of a hex digit.
|
||||
uint hex(char c)
|
||||
{
|
||||
import std.ascii;
|
||||
if(!std.ascii.isHexDigit(c))
|
||||
{
|
||||
throw new Exception("Invalid color: " ~ value);
|
||||
|
@ -79,18 +79,19 @@ void main()
|
|||
constructor.addConstructorMapping("!color-mapping", &constructColorMapping);
|
||||
|
||||
auto resolver = new Resolver;
|
||||
import std.regex;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
|
||||
auto loader = Loader("input.yaml");
|
||||
loader.constructor = constructor;
|
||||
loader.resolver = resolver;
|
||||
|
||||
auto root = loader.load();
|
||||
|
||||
if(root["scalar-red"].as!Color == red &&
|
||||
root["mapping-red"].as!Color == red &&
|
||||
root["scalar-orange"].as!Color == orange &&
|
||||
if(root["scalar-red"].as!Color == red &&
|
||||
root["mapping-red"].as!Color == red &&
|
||||
root["scalar-orange"].as!Color == orange &&
|
||||
root["mapping-orange"].as!Color == orange)
|
||||
{
|
||||
writeln("SUCCESS");
|
||||
|
|
|
@ -12,14 +12,14 @@ void help()
|
|||
{
|
||||
string help =
|
||||
"D:YAML benchmark\n"
|
||||
"Copyright (C) 2011 Ferdinand Majerech\n"
|
||||
"Copyright (C) 2011-2014 Ferdinand Majerech\n"
|
||||
"Usage: yaml_bench [OPTION ...] [YAML_FILE]\n"
|
||||
"\n"
|
||||
"Loads and optionally extracts data and/or dumps a YAML file.\n"
|
||||
"\n"
|
||||
"Available options:\n"
|
||||
" -h --help Show this help information.\n"
|
||||
" -g --get Extract data from the file (using Node.get()).\n"
|
||||
" -g --get Extract data from the file (using Node.as()).\n"
|
||||
" -d --dump Dump the loaded data (to YAML_FILE.dump).\n"
|
||||
" -r --runs=NUM Repeat the benchmark NUM times.\n";
|
||||
writeln(help);
|
||||
|
|
|
@ -36,7 +36,7 @@ static this()
|
|||
generators["set"] = &genSet;
|
||||
}
|
||||
|
||||
real randomNormalized(in string distribution = "linear")
|
||||
real randomNormalized(const string distribution = "linear")
|
||||
{
|
||||
auto generator = Random(unpredictableSeed());
|
||||
const r = uniform!"[]"(0.0L, 1.0L, generator);
|
||||
|
@ -49,23 +49,23 @@ real randomNormalized(in string distribution = "linear")
|
|||
case "cubic":
|
||||
return r * r * r;
|
||||
default:
|
||||
writeln("Unknown random distribution: ", distribution,
|
||||
writeln("Unknown random distribution: ", distribution,
|
||||
", falling back to linear");
|
||||
return randomNormalized("linear");
|
||||
}
|
||||
}
|
||||
|
||||
long randomLong(in long min, in long max, in string distribution = "linear")
|
||||
long randomLong(const long min, const long max, const string distribution = "linear")
|
||||
{
|
||||
return min + cast(long)round((max - min) * randomNormalized(distribution));
|
||||
}
|
||||
|
||||
real randomReal(in real min, in real max, in string distribution = "linear")
|
||||
real randomReal(const real min, const real max, const string distribution = "linear")
|
||||
{
|
||||
return min + (max - min) * randomNormalized(distribution);
|
||||
}
|
||||
|
||||
char randomChar(in string chars)
|
||||
char randomChar(const string chars)
|
||||
{
|
||||
return chars[randomLong(0, chars.length - 1)];
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ Node genString(bool root = false)
|
|||
{
|
||||
auto range = config["string"]["range"];
|
||||
|
||||
const chars = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
const chars = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
range["dist"].as!string);
|
||||
|
||||
char[] result = new char[chars];
|
||||
|
@ -101,7 +101,7 @@ Node genInt(bool root = false)
|
|||
{
|
||||
auto range = config["int"]["range"];
|
||||
|
||||
const result = randomLong(range["min"].as!int, range["max"].as!int,
|
||||
const result = randomLong(range["min"].as!int, range["max"].as!int,
|
||||
range["dist"].as!string);
|
||||
|
||||
return Node(result);
|
||||
|
@ -111,7 +111,7 @@ Node genFloat(bool root = false)
|
|||
{
|
||||
auto range = config["float"]["range"];
|
||||
|
||||
const result = randomReal(range["min"].as!real, range["max"].as!real,
|
||||
const result = randomReal(range["min"].as!real, range["max"].as!real,
|
||||
range["dist"].as!string);
|
||||
|
||||
return Node(result);
|
||||
|
@ -126,7 +126,7 @@ Node genTimestamp(bool root = false)
|
|||
{
|
||||
auto range = config["timestamp"]["range"];
|
||||
|
||||
auto hnsecs = randomLong(range["min"].as!ulong, range["max"].as!ulong,
|
||||
auto hnsecs = randomLong(range["min"].as!ulong, range["max"].as!ulong,
|
||||
range["dist"].as!string);
|
||||
|
||||
if(randomNormalized() <= config["timestamp"]["round-chance"].as!real)
|
||||
|
@ -141,7 +141,7 @@ Node genBinary(bool root = false)
|
|||
{
|
||||
auto range = config["binary"]["range"];
|
||||
|
||||
const bytes = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
const bytes = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
range["dist"].as!string);
|
||||
|
||||
ubyte[] result = new ubyte[bytes];
|
||||
|
@ -153,7 +153,7 @@ Node genBinary(bool root = false)
|
|||
return Node(result);
|
||||
}
|
||||
|
||||
Node nodes(in bool root, Node range, in string tag, in bool set = false)
|
||||
Node nodes(const bool root, Node range, const string tag, const bool set = false)
|
||||
{
|
||||
auto types = config["collection-keys"].as!bool ? typesCollection : [];
|
||||
types ~= (set ? typesScalarKey : typesScalar);
|
||||
|
@ -168,7 +168,7 @@ Node nodes(in bool root, Node range, in string tag, in bool set = false)
|
|||
}
|
||||
else
|
||||
{
|
||||
const elems = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
const elems = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
range["dist"].as!string);
|
||||
|
||||
nodes = new Node[elems];
|
||||
|
@ -205,7 +205,7 @@ Node pairs(bool root, bool complex, Node range, string tag)
|
|||
}
|
||||
else
|
||||
{
|
||||
const pairs = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
const pairs = randomLong(range["min"].as!uint, range["max"].as!uint,
|
||||
range["dist"].as!string);
|
||||
|
||||
keys = new Node[pairs];
|
||||
|
@ -244,13 +244,13 @@ Node genPairs(bool root = false)
|
|||
return pairs(root, complex, range, "tag:yaml.org,2002:pairs");
|
||||
}
|
||||
|
||||
Node generateNode(in string type, bool root = false)
|
||||
Node generateNode(const string type, bool root = false)
|
||||
{
|
||||
++totalNodes;
|
||||
return generators[type](root);
|
||||
}
|
||||
|
||||
Node[] generate(in string configFileName)
|
||||
Node[] generate(const string configFileName)
|
||||
{
|
||||
config = Loader(configFileName).load();
|
||||
|
||||
|
@ -285,12 +285,12 @@ void main(string[] args)
|
|||
//Generate and dump the nodes.
|
||||
Node[] generated = generate(configFile);
|
||||
|
||||
auto dumper = Dumper(args[1]);
|
||||
auto encoding = config["encoding"];
|
||||
auto dumper = Dumper(args[1]);
|
||||
auto encoding = config["encoding"];
|
||||
dumper.encoding = encoding == "utf-16" ? Encoding.UTF_16:
|
||||
encoding == "utf-32" ? Encoding.UTF_32:
|
||||
encoding == "utf-32" ? Encoding.UTF_32:
|
||||
Encoding.UTF_8;
|
||||
|
||||
|
||||
dumper.indent = config["indent"].as!uint;
|
||||
dumper.textWidth = config["text-width"].as!uint;
|
||||
dumper.dump(generated);
|
||||
|
|
Loading…
Reference in a new issue