fix(config): Proper dereference of ${self.key}

This commit is contained in:
Michael Carlberg
2016-11-19 19:18:28 +01:00
parent 88c8bbd940
commit 40fcabd644
4 changed files with 39 additions and 17 deletions

View File

@ -41,27 +41,40 @@ namespace string_util {
/**
* Replace first occurence of needle in haystack
*/
string replace(const string& haystack, string needle, string replacement) {
string replace(const string& haystack, string needle, string reply, size_t start, size_t end) {
string str(haystack);
string::size_type pos;
if (needle != replacement && (pos = str.find(needle)) != string::npos)
str = str.replace(pos, needle.length(), replacement);
if (needle != reply && (pos = str.find(needle, start)) != string::npos) {
if (end == string::npos || pos < end)
str = str.replace(pos, needle.length(), reply);
}
return str;
}
/**
* Replace all occurences of needle in haystack
*/
string replace_all(const string& haystack, string needle, string replacement) {
string replace_all(const string& haystack, string needle, string reply, size_t start, size_t end) {
string replaced;
if (end == string::npos)
end = haystack.length();
for (size_t i = 0; i < haystack.length(); i++) {
if (haystack.compare(i, needle.length(), needle) == 0) {
replaced += replacement;
if (i < start) {
replaced += haystack[i];
} else if (i + needle.length() > end) {
replaced += haystack[i];
} else if (haystack.compare(i, needle.length(), needle) == 0) {
replaced += reply;
i += needle.length() - 1;
} else {
replaced += haystack[i];
}
}
return replaced;
}
@ -70,8 +83,7 @@ namespace string_util {
*/
string squeeze(const string& haystack, char needle) {
string result = haystack;
while (result.find({needle, needle}) != string::npos)
result = replace_all(result, {needle, needle}, {needle});
while (result.find({needle, needle}) != string::npos) result = replace_all(result, {needle, needle}, {needle});
return result;
}