From 13822c77b8c06bba522a40fdccd9e0c8e6f7d547 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sat, 1 Jun 2019 18:50:04 -0400 Subject: Interpolation of environment variables --- source/util.d | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'source/util.d') diff --git a/source/util.d b/source/util.d index 101645a..1d0abf1 100644 --- a/source/util.d +++ b/source/util.d @@ -202,6 +202,38 @@ string expander(string[string] aa, string name, char delim = '$') { } +string interpolate(string[string]aa, string str, char delim = '$') { + import std.ascii; + string s = str.dup; + ulong[] needles = indexOfAll(s, delim); + 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]; + } + found ~= tmp; + } + + foreach (match; found) { + foreach (pair; aa.byPair) { + if (pair.key != match) + continue; + 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(".", ""); -- cgit