mirror of
https://github.com/polybar/polybar.git
synced 2024-11-25 13:55:47 -05:00
fix(config): Proper dereference of ${self.key}
This commit is contained in:
parent
88c8bbd940
commit
40fcabd644
4 changed files with 39 additions and 17 deletions
|
@ -129,7 +129,7 @@ class config {
|
||||||
} else if (path.find("xrdb:") == 0) {
|
} else if (path.find("xrdb:") == 0) {
|
||||||
return dereference_xrdb<T>(path.substr(5), value);
|
return dereference_xrdb<T>(path.substr(5), value);
|
||||||
} else if ((pos = path.find(".")) != string::npos) {
|
} else if ((pos = path.find(".")) != string::npos) {
|
||||||
return dereference_local<T>(path.substr(0, pos), path.substr(pos + 1));
|
return dereference_local<T>(path.substr(0, pos), path.substr(pos + 1), section);
|
||||||
} else {
|
} else {
|
||||||
throw value_error("Invalid reference defined at [" + build_path(section, key) + "]");
|
throw value_error("Invalid reference defined at [" + build_path(section, key) + "]");
|
||||||
}
|
}
|
||||||
|
@ -142,13 +142,13 @@ class config {
|
||||||
* ${section.key}
|
* ${section.key}
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T dereference_local(string section, string key) const {
|
T dereference_local(string section, string key, string current_section) const {
|
||||||
if (section == "BAR")
|
if (section == "BAR")
|
||||||
m_logger.warn("${BAR.key} is deprecated. Use ${root.key} instead");
|
m_logger.warn("${BAR.key} is deprecated. Use ${root.key} instead");
|
||||||
|
|
||||||
section = string_util::replace(section, "BAR", bar_section());
|
section = string_util::replace(section, "BAR", bar_section(), 0, 3);
|
||||||
section = string_util::replace(section, "root", bar_section());
|
section = string_util::replace(section, "root", bar_section(), 0, 4);
|
||||||
section = string_util::replace(section, "self", section);
|
section = string_util::replace(section, "self", current_section, 0, 4);
|
||||||
|
|
||||||
auto path = build_path(section, key);
|
auto path = build_path(section, key);
|
||||||
auto result = m_ptree.get_optional<T>(path);
|
auto result = m_ptree.get_optional<T>(path);
|
||||||
|
|
|
@ -16,8 +16,8 @@ namespace string_util {
|
||||||
string upper(const string& s);
|
string upper(const string& s);
|
||||||
string lower(const string& s);
|
string lower(const string& s);
|
||||||
bool compare(const string& s1, const string& s2);
|
bool compare(const string& s1, const string& s2);
|
||||||
string replace(const string& haystack, string needle, string replacement);
|
string replace(const string& haystack, string needle, string repl, size_t start = 0, size_t end = string::npos);
|
||||||
string replace_all(const string& haystack, string needle, string replacement);
|
string replace_all(const string& haystack, string needle, string repl, size_t start = 0, size_t end = string::npos);
|
||||||
string squeeze(const string& haystack, char needle);
|
string squeeze(const string& haystack, char needle);
|
||||||
string strip(const string& haystack, char needle);
|
string strip(const string& haystack, char needle);
|
||||||
string strip_trailing_newline(const string& haystack);
|
string strip_trailing_newline(const string& haystack);
|
||||||
|
|
|
@ -41,27 +41,40 @@ namespace string_util {
|
||||||
/**
|
/**
|
||||||
* Replace first occurence of needle in haystack
|
* 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 str(haystack);
|
||||||
string::size_type pos;
|
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;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace all occurences of needle in haystack
|
* 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;
|
string replaced;
|
||||||
|
|
||||||
|
if (end == string::npos)
|
||||||
|
end = haystack.length();
|
||||||
|
|
||||||
for (size_t i = 0; i < haystack.length(); i++) {
|
for (size_t i = 0; i < haystack.length(); i++) {
|
||||||
if (haystack.compare(i, needle.length(), needle) == 0) {
|
if (i < start) {
|
||||||
replaced += replacement;
|
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;
|
i += needle.length() - 1;
|
||||||
} else {
|
} else {
|
||||||
replaced += haystack[i];
|
replaced += haystack[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return replaced;
|
return replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,8 +83,7 @@ namespace string_util {
|
||||||
*/
|
*/
|
||||||
string squeeze(const string& haystack, char needle) {
|
string squeeze(const string& haystack, char needle) {
|
||||||
string result = haystack;
|
string result = haystack;
|
||||||
while (result.find({needle, needle}) != string::npos)
|
while (result.find({needle, needle}) != string::npos) result = replace_all(result, {needle, needle}, {needle});
|
||||||
result = replace_all(result, {needle, needle}, {needle});
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,21 @@ int main() {
|
||||||
expect(!string_util::compare("foo", "bar"));
|
expect(!string_util::compare("foo", "bar"));
|
||||||
};
|
};
|
||||||
|
|
||||||
"replace"_test = [] { expect(string_util::replace("Foo bar baz", "a", "x") == "Foo bxr baz"); };
|
"replace"_test = [] {
|
||||||
|
expect(string_util::replace("abc", "b", ".") == "a.c");
|
||||||
|
expect(string_util::replace("aaa", "a", ".", 1, 2) == "a.a");
|
||||||
|
expect(string_util::replace("aaa", "a", ".", 0, 2) == ".aa");
|
||||||
|
expect(string_util::replace("Foo bar baz", "a", "x") == "Foo bxr baz");
|
||||||
|
expect(string_util::replace("foooobar", "o", "x", 2, 3) == "foxoobar");
|
||||||
|
expect(string_util::replace("foooobar", "o", "x", 0, 1) == "foooobar");
|
||||||
|
};
|
||||||
|
|
||||||
"replace_all"_test = [] {
|
"replace_all"_test = [] {
|
||||||
expect(string_util::replace_all("Foo bar baz", "a", "x") == "Foo bxr bxz");
|
expect(string_util::replace_all("Foo bar baza", "a", "x") == "Foo bxr bxzx");
|
||||||
expect(string_util::replace_all("hehehe", "he", "hoo") == "hoohoohoo");
|
expect(string_util::replace_all("hehehe", "he", "hoo") == "hoohoohoo");
|
||||||
|
expect(string_util::replace_all("hehehe", "he", "hoo", 0, 2) == "hoohehe");
|
||||||
|
expect(string_util::replace_all("hehehe", "he", "hoo", 4) == "hehehoo");
|
||||||
|
expect(string_util::replace_all("hehehe", "he", "hoo", 0, 1) == "hehehe");
|
||||||
expect(string_util::replace_all("131313", "3", "13") == "113113113");
|
expect(string_util::replace_all("131313", "3", "13") == "113113113");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue