aboutsummaryrefslogtreecommitdiff
path: root/source/util.d
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-05-31 21:32:12 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-05-31 21:32:12 -0400
commitf5f481d3932c66a1fddcfdc74a5c8bfe253a4a0e (patch)
treefbde9fde0c31cf3d84e2aeb5543fe5eba4f38d83 /source/util.d
parent277be91a65eaa2f7c2b23280b73ed696cc377cb4 (diff)
downloaddm-f5f481d3932c66a1fddcfdc74a5c8bfe253a4a0e.tar.gz
Add utility functions:
* indexOfAll * expander * short_version
Diffstat (limited to 'source/util.d')
-rw-r--r--source/util.d49
1 files changed, 49 insertions, 0 deletions
diff --git a/source/util.d b/source/util.d
index a33751e..8f2ad8d 100644
--- a/source/util.d
+++ b/source/util.d
@@ -1,4 +1,6 @@
module util;
+import std.ascii;
+import std.array;
import std.stdio;
import std.string;
import std.process;
@@ -154,3 +156,50 @@ string pytest_xunit2(string filename) {
return result;
}
+
+
+ulong[] indexOfAll(string s, char ch) {
+ ulong[] result;
+ for (ulong i = 0; i < s.length; i++) {
+ if (s[i] == ch) {
+ result ~= i;
+ }
+ }
+ return result;
+}
+
+
+string expander(string[string] aa, string name, char delim = '$') {
+ string s = aa[name].dup;
+ ulong[] needles = indexOfAll(s, delim);
+ string[string] found;
+
+ foreach (needle; needles) {
+ string tmp = "";
+ for (ulong i = needle; i < s.length; i++) {
+ if (s[i] == delim) continue;
+ else if (s[i] == '{' || s[i] == '}') continue;
+ else if (!s[i].isAlphaNum && s[i] != '_' ) break;
+ tmp ~= s[i];
+ }
+ writeln(tmp);
+ found[tmp] = aa.get(tmp, "");
+ }
+
+ foreach (pair; found.byPair) {
+ s = s.replace(delim ~ pair.key, pair.value)
+ .replace(format("%c{%s}", delim, pair.key), pair.value);
+ }
+
+ return s;
+}
+
+
+string short_version(string vrs) {
+ string tmp = vrs.dup;
+ tmp = tmp.replace(".", "");
+ if (tmp.length > 2) {
+ tmp = tmp[0 .. 2];
+ }
+ return tmp;
+}