2016-12-02 12:46:39 +01:00
|
|
|
module substitution;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
void loadSubstitutionFile(string fileName)
|
|
|
|
{
|
2017-04-10 21:07:53 +02:00
|
|
|
import std.file : slurp;
|
2016-12-02 12:46:39 +01:00
|
|
|
import std.algorithm.iteration : each;
|
2017-04-10 21:07:53 +02:00
|
|
|
auto data = slurp!(string, string)(fileName, `"%s" = "%s"`);
|
|
|
|
map = (string[string]).init;
|
|
|
|
data.each!(pair => map[pair[0]] = pair[1]);
|
2016-12-02 12:46:39 +01:00
|
|
|
}
|
|
|
|
|
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;
|