2016-12-02 12:46:39 +01:00
|
|
|
module substitution;
|
|
|
|
|
2017-04-10 22:49:20 +02:00
|
|
|
import std.file : slurp;
|
|
|
|
import std.meta : AliasSeq;
|
|
|
|
import std.traits : Parameters;
|
2017-04-10 22:08:46 +02:00
|
|
|
|
2017-08-04 02:21:59 +02:00
|
|
|
version (unittest)
|
|
|
|
{
|
|
|
|
import unit_threaded;
|
|
|
|
}
|
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
public:
|
|
|
|
|
2017-05-16 14:34:42 +02:00
|
|
|
/***********************************
|
|
|
|
* Loads a substitution dictonary from a file.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 22:08:46 +02:00
|
|
|
void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
|
2017-04-10 22:49:20 +02:00
|
|
|
if (is(Parameters!(slurpFun!(string, string)) == AliasSeq!(string, const char[])))
|
2016-12-02 12:46:39 +01:00
|
|
|
{
|
|
|
|
import std.algorithm.iteration : each;
|
2017-04-10 22:49:20 +02:00
|
|
|
|
2017-04-10 21:07:53 +02:00
|
|
|
map = (string[string]).init;
|
2017-04-14 14:33:50 +02:00
|
|
|
slurpFun!(string, string)(fileName, `"%s" = "%s"`).each!(pair => map[pair[0]] = pair[1]);
|
2016-12-02 12:46:39 +01:00
|
|
|
}
|
|
|
|
|
2017-05-16 14:34:42 +02:00
|
|
|
///
|
2017-04-10 22:08:46 +02:00
|
|
|
@safe unittest
|
|
|
|
{
|
2017-04-10 22:49:20 +02:00
|
|
|
import std.typecons : Tuple, tuple;
|
2017-04-10 22:08:46 +02:00
|
|
|
|
|
|
|
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);
|
2017-04-10 22:08:46 +02:00
|
|
|
|
2017-04-10 22:49:20 +02:00
|
|
|
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename,
|
|
|
|
in char[] format)
|
2017-04-10 22:08:46 +02:00
|
|
|
{
|
|
|
|
return [tuple("", "")];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSubstitutionFile!mockSlurpEmptyEntry("");
|
2017-08-04 02:21:59 +02:00
|
|
|
"".shouldBeIn(map);
|
|
|
|
map.length.shouldEqual(1);
|
|
|
|
map[""].shouldEqual("");
|
2017-04-10 22:08:46 +02:00
|
|
|
|
2017-04-10 22:49:20 +02:00
|
|
|
static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename,
|
|
|
|
in char[] format)
|
2017-04-10 22:08:46 +02:00
|
|
|
{
|
|
|
|
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");
|
2017-04-10 22:08:46 +02:00
|
|
|
|
2017-04-10 22:49:20 +02:00
|
|
|
static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(
|
|
|
|
string filename, in char[] format)
|
2017-04-10 22:08:46 +02:00
|
|
|
{
|
|
|
|
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");
|
2017-04-10 22:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 14:34:42 +02:00
|
|
|
/***********************************
|
|
|
|
* Substitutes a string with its corresponding replacement, if one is available.
|
|
|
|
* Otherwise just returns the original string.
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-16 14:34:42 +02:00
|
|
|
///
|
2016-12-29 13:33:41 +01:00
|
|
|
@safe unittest
|
|
|
|
{
|
|
|
|
map[""] = "";
|
2017-08-04 02:21:59 +02:00
|
|
|
"".substitute.shouldEqual("");
|
2016-12-29 13:33:41 +01:00
|
|
|
|
|
|
|
map["a"] = "b";
|
2017-08-04 02:21:59 +02:00
|
|
|
"a".substitute.shouldEqual("b");
|
2016-12-29 13:33:41 +01:00
|
|
|
|
|
|
|
map["Regensburg Danziger Freiheit"] = "Danziger Freiheit";
|
2017-08-04 02:21:59 +02:00
|
|
|
"Regensburg Danziger Freiheit".substitute.shouldEqual("Danziger Freiheit");
|
2016-12-29 13:33:41 +01:00
|
|
|
|
|
|
|
map["Regensburg Danziger Freiheit"] = "Anderer Test";
|
2017-08-04 02:21:59 +02:00
|
|
|
"Regensburg Danziger Freiheit".substitute.shouldEqual("Anderer Test");
|
2016-12-29 13:33:41 +01:00
|
|
|
|
2017-08-04 02:21:59 +02:00
|
|
|
"z".substitute.shouldEqual("z");
|
2016-12-29 13:33:41 +01:00
|
|
|
|
2017-08-04 02:21:59 +02:00
|
|
|
"Regensburg Hauptbahnhof".substitute.shouldEqual("Regensburg Hauptbahnhof");
|
2016-12-29 13:33:41 +01:00
|
|
|
}
|
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
string[string] map;
|