From 99d36745ebc841311ccc7a51c03855b76ec84ed0 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 10 Apr 2017 21:07:53 +0200 Subject: [PATCH 1/4] Moved check for the existance of the substitution file from loadSubstitutionFile to main --- source/app.d | 8 ++++++-- source/substitution.d | 16 ++++------------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/source/app.d b/source/app.d index 1c4b745..604f62d 100644 --- a/source/app.d +++ b/source/app.d @@ -1,5 +1,6 @@ import std.array : array, replace; import std.datetime : Clock; +import std.file : exists, isFile; import std.format : format; import std.getopt : defaultGetoptPrinter, getopt; import std.json : JSONValue; @@ -35,8 +36,11 @@ void main(string[] args) "mode" : "direct", "type_dm" : "any", "itdLPxx_bcl" : "true"]); - - loadSubstitutionFile(substitutionFileName); + + if (substitutionFileName.exists && substitutionFileName.isFile) + { + loadSubstitutionFile(substitutionFileName); + } auto currentTime = Clock.currTime; JSONValue j = ["time" : "%02s:%02s".format(currentTime.hour, currentTime.minute)]; diff --git a/source/substitution.d b/source/substitution.d index d743fe9..9275705 100644 --- a/source/substitution.d +++ b/source/substitution.d @@ -4,19 +4,11 @@ public: void loadSubstitutionFile(string fileName) { - import std.file : slurp, exists, isFile; + import std.file : slurp; import std.algorithm.iteration : each; - - if (fileName.exists && fileName.isFile) - { - auto data = slurp!(string, string)(fileName, `"%s" = "%s"`); - map = (string[string]).init; - data.each!(pair => map[pair[0]] = pair[1]); - } - else - { - map = (string[string]).init; - } + auto data = slurp!(string, string)(fileName, `"%s" = "%s"`); + map = (string[string]).init; + data.each!(pair => map[pair[0]] = pair[1]); } auto substitute(string s) @safe nothrow From 86d88607670e373088eb9df06861a7f9db008a75 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 10 Apr 2017 22:08:46 +0200 Subject: [PATCH 2/4] made slurp a template parameter of loadSubstitutionFile and created tests for loadSubstitutionFile --- source/app.d | 2 +- source/substitution.d | 55 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/source/app.d b/source/app.d index 604f62d..db49108 100644 --- a/source/app.d +++ b/source/app.d @@ -36,7 +36,7 @@ void main(string[] args) "mode" : "direct", "type_dm" : "any", "itdLPxx_bcl" : "true"]); - + if (substitutionFileName.exists && substitutionFileName.isFile) { loadSubstitutionFile(substitutionFileName); diff --git a/source/substitution.d b/source/substitution.d index 9275705..deb90b6 100644 --- a/source/substitution.d +++ b/source/substitution.d @@ -1,16 +1,65 @@ module substitution; +import std.file: slurp; + public: -void loadSubstitutionFile(string fileName) +void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) { - import std.file : slurp; import std.algorithm.iteration : each; - auto data = slurp!(string, string)(fileName, `"%s" = "%s"`); + auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`); map = (string[string]).init; data.each!(pair => map[pair[0]] = pair[1]); } +@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"); +} + auto substitute(string s) @safe nothrow { return s in map ? map[s] : s; From 446f9a096b227ed99d1e54038d0a31efe16224a9 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 10 Apr 2017 22:49:20 +0200 Subject: [PATCH 3/4] added template constraint to loadSubstitutionFile --- source/substitution.d | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/source/substitution.d b/source/substitution.d index deb90b6..315b868 100644 --- a/source/substitution.d +++ b/source/substitution.d @@ -1,12 +1,16 @@ module substitution; -import std.file: slurp; +import std.file : slurp; +import std.meta : AliasSeq; +import std.traits : Parameters; public: void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) + if (is(Parameters!(slurpFun!(string, string)) == AliasSeq!(string, const char[]))) { import std.algorithm.iteration : each; + auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`); map = (string[string]).init; data.each!(pair => map[pair[0]] = pair[1]); @@ -14,8 +18,7 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) @safe unittest { - import std.algorithm: canFind; - import std.typecons: Tuple, tuple; + import std.typecons : Tuple, tuple; static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format) { @@ -25,7 +28,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) loadSubstitutionFile!mockSlurpEmpty(""); assert(map.length == 0); - static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, in char[] format) + static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, + in char[] format) { return [tuple("", "")]; } @@ -35,7 +39,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) assert(map.length == 1); assert(map[""] == ""); - static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, in char[] format) + static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, + in char[] format) { return [tuple("foo", "bar")]; } @@ -45,7 +50,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) assert(map.length == 1); assert(map["foo"] == "bar"); - static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(string filename, in char[] format) + static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)( + string filename, in char[] format) { return [tuple("", ""), tuple("0", "1"), tuple("Text in", "wird durch diesen ersetzt")]; } From ff4c72628fdbe750e7e290c1a3acdd4150d57b24 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Fri, 14 Apr 2017 14:33:50 +0200 Subject: [PATCH 4/4] got rid of unnecessary temporary variable --- source/substitution.d | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/substitution.d b/source/substitution.d index 315b868..66e7b9d 100644 --- a/source/substitution.d +++ b/source/substitution.d @@ -11,9 +11,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) { import std.algorithm.iteration : each; - auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`); map = (string[string]).init; - data.each!(pair => map[pair[0]] = pair[1]); + slurpFun!(string, string)(fileName, `"%s" = "%s"`).each!(pair => map[pair[0]] = pair[1]); } @safe unittest