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