mirror of
https://github.com/polybar/polybar.git
synced 2026-02-21 01:35:47 +00:00
fix(config): Proper dereference of ${self.key}
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user