2016-12-02 12:46:39 +01:00
|
|
|
module substitution;
|
|
|
|
|
2017-04-10 22:08:46 +02:00
|
|
|
import std.file: slurp;
|
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
public:
|
|
|
|
|
2017-04-10 22:08:46 +02:00
|
|
|
void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
|
2016-12-02 12:46:39 +01:00
|
|
|
{
|
|
|
|
import std.algorithm.iteration : each;
|
2017-04-10 22:08:46 +02:00
|
|
|
auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`);
|
2017-04-10 21:07:53 +02:00
|
|
|
map = (string[string]).init;
|
|
|
|
data.each!(pair => map[pair[0]] = pair[1]);
|
2016-12-02 12:46:39 +01:00
|
|
|
}
|
|
|
|
|
2017-04-10 22:08:46 +02:00
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
import std.algorithm: canFind;
|
|
|
|
import std.typecons: Tuple, tuple;
|
|
|
|
|
|
|
|
static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSubstitutionFile!mockSlurpEmpty("");
|
|
|
|
assert(map.length == 0);
|
|
|
|
|
|
|
|
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, in char[] format)
|
|
|
|
{
|
|
|
|
return [tuple("", "")];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSubstitutionFile!mockSlurpEmptyEntry("");
|
|
|
|
assert("" in map);
|
|
|
|
assert(map.length == 1);
|
|
|
|
assert(map[""] == "");
|
|
|
|
|
|
|
|
static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, in char[] format)
|
|
|
|
{
|
|
|
|
return [tuple("foo", "bar")];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSubstitutionFile!mockSlurpSingleEntry("");
|
|
|
|
assert("foo" in map);
|
|
|
|
assert(map.length == 1);
|
|
|
|
assert(map["foo"] == "bar");
|
|
|
|
|
|
|
|
static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(string filename, in char[] format)
|
|
|
|
{
|
|
|
|
return [tuple("", ""), tuple("0", "1"), tuple("Text in", "wird durch diesen ersetzt")];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSubstitutionFile!mockSlurpMultipleEntries("");
|
|
|
|
assert("" in map);
|
|
|
|
assert("0" in map);
|
|
|
|
assert("Text in" in map);
|
|
|
|
assert(map.length == 3);
|
|
|
|
assert(map[""] == "");
|
|
|
|
assert(map["0"] == "1");
|
|
|
|
assert(map["Text in"] == "wird durch diesen ersetzt");
|
|
|
|
}
|
|
|
|
|
2016-12-29 13:33:41 +01:00
|
|
|
auto substitute(string s) @safe nothrow
|
2016-12-02 12:46:39 +01:00
|
|
|
{
|
|
|
|
return s in map ? map[s] : s;
|
|
|
|
}
|
|
|
|
|
2016-12-29 13:33:41 +01:00
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
map[""] = "";
|
|
|
|
assert(substitute("") == "");
|
|
|
|
|
|
|
|
map["a"] = "b";
|
|
|
|
assert(substitute("a") == "b");
|
|
|
|
|
|
|
|
map["Regensburg Danziger Freiheit"] = "Danziger Freiheit";
|
|
|
|
assert(substitute("Regensburg Danziger Freiheit") == "Danziger Freiheit");
|
|
|
|
|
|
|
|
map["Regensburg Danziger Freiheit"] = "Anderer Test";
|
|
|
|
assert(substitute("Regensburg Danziger Freiheit") == "Anderer Test");
|
|
|
|
|
|
|
|
assert(substitute("z") == "z");
|
|
|
|
|
|
|
|
assert(substitute("Regensburg Hauptbahnhof") == "Regensburg Hauptbahnhof");
|
|
|
|
}
|
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
string[string] map;
|