bayernfahrplan/source/substitution.d

41 lines
929 B
D
Raw Normal View History

2016-12-02 12:46:39 +01:00
module substitution;
public:
void loadSubstitutionFile(string fileName)
{
import std.file : slurp;
2016-12-02 12:46:39 +01:00
import std.algorithm.iteration : each;
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
}
auto substitute(string s) @safe nothrow
2016-12-02 12:46:39 +01:00
{
return s in map ? map[s] : s;
}
@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;