bayernfahrplan/source/substitution.d

111 lines
2.7 KiB
D
Raw Normal View History

2016-12-02 12:46:39 +01:00
module substitution;
import std.file : slurp;
import std.meta : AliasSeq;
import std.traits : Parameters;
2017-08-04 02:21:59 +02:00
version (unittest)
{
import unit_threaded;
}
2016-12-02 12:46:39 +01:00
public:
/***********************************
* Loads a substitution dictonary from a file.
*/
void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
if (is(Parameters!(slurpFun!(string, string)) == AliasSeq!(string, const char[])))
2016-12-02 12:46:39 +01:00
{
import std.algorithm.iteration : each;
map = (string[string]).init;
slurpFun!(string, string)(fileName, `"%s" = "%s"`).each!(pair => map[pair[0]] = pair[1]);
2016-12-02 12:46:39 +01:00
}
///
@safe unittest
{
import std.typecons : Tuple, tuple;
static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format)
{
return [];
}
loadSubstitutionFile!mockSlurpEmpty("");
2017-08-04 02:21:59 +02:00
map.length.shouldEqual(0);
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename,
in char[] format)
{
return [tuple("", "")];
}
loadSubstitutionFile!mockSlurpEmptyEntry("");
2017-08-04 02:21:59 +02:00
"".shouldBeIn(map);
map.length.shouldEqual(1);
map[""].shouldEqual("");
static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename,
in char[] format)
{
return [tuple("foo", "bar")];
}
loadSubstitutionFile!mockSlurpSingleEntry("");
2017-08-04 02:21:59 +02:00
"foo".shouldBeIn(map);
map.length.shouldEqual(1);
map["foo"].shouldEqual("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("");
2017-08-04 02:21:59 +02:00
"".shouldBeIn(map);
"0".shouldBeIn(map);
"Text in".shouldBeIn(map);
map.length.shouldEqual(3);
map[""].shouldEqual("");
map["0"].shouldEqual("1");
map["Text in"].shouldEqual("wird durch diesen ersetzt");
}
/***********************************
* Substitutes a string with its corresponding replacement, if one is available.
* Otherwise just returns the original string.
*/
auto substitute(string s) @safe nothrow
2016-12-02 12:46:39 +01:00
{
return s in map ? map[s] : s;
}
///
@safe unittest
{
map[""] = "";
2017-08-04 02:21:59 +02:00
"".substitute.shouldEqual("");
map["a"] = "b";
2017-08-04 02:21:59 +02:00
"a".substitute.shouldEqual("b");
map["Regensburg Danziger Freiheit"] = "Danziger Freiheit";
2017-08-04 02:21:59 +02:00
"Regensburg Danziger Freiheit".substitute.shouldEqual("Danziger Freiheit");
map["Regensburg Danziger Freiheit"] = "Anderer Test";
2017-08-04 02:21:59 +02:00
"Regensburg Danziger Freiheit".substitute.shouldEqual("Anderer Test");
2017-08-04 02:21:59 +02:00
"z".substitute.shouldEqual("z");
2017-08-04 02:21:59 +02:00
"Regensburg Hauptbahnhof".substitute.shouldEqual("Regensburg Hauptbahnhof");
}
2016-12-02 12:46:39 +01:00
private:
string[string] map;