Optimize lookup of electric variables

This commit is contained in:
Mahmoud Al-Qudsi 2020-06-24 22:46:02 -05:00
parent 9bddd78239
commit a5be15da69

View File

@ -81,27 +81,33 @@ struct electric_var_t {
static const electric_var_t *for_name(const wcstring &name);
};
static const electric_var_t electric_variables[] = {
// Keep sorted alphabetically
static const std::vector<electric_var_t> electric_variables {
{L"FISH_VERSION", electric_var_t::freadonly},
{L"PWD", electric_var_t::freadonly | electric_var_t::fcomputed | electric_var_t::fexports},
{L"SHLVL", electric_var_t::freadonly | electric_var_t::fexports},
{L"_", electric_var_t::freadonly},
{L"fish_kill_signal", electric_var_t::freadonly | electric_var_t::fcomputed},
{L"fish_pid", electric_var_t::freadonly},
{L"fish_private_mode", electric_var_t::freadonly},
{L"history", electric_var_t::freadonly | electric_var_t::fcomputed},
{L"hostname", electric_var_t::freadonly},
{L"pipestatus", electric_var_t::freadonly | electric_var_t::fcomputed},
{L"status", electric_var_t::freadonly | electric_var_t::fcomputed},
{L"version", electric_var_t::freadonly},
{L"FISH_VERSION", electric_var_t::freadonly},
{L"fish_pid", electric_var_t::freadonly},
{L"hostname", electric_var_t::freadonly},
{L"_", electric_var_t::freadonly},
{L"fish_private_mode", electric_var_t::freadonly},
{L"umask", electric_var_t::fcomputed},
{L"fish_kill_signal", electric_var_t::freadonly | electric_var_t::fcomputed},
{L"version", electric_var_t::freadonly},
};
const electric_var_t *electric_var_t::for_name(const wcstring &name) {
for (const auto &var : electric_variables) {
if (name == var.name) {
return &var;
}
static auto first = electric_variables.begin();
static auto last = electric_variables.end();
electric_var_t search { name.c_str(), 0 };
auto binsearch = std::lower_bound(first, last, search,
[&](const electric_var_t &v1, const electric_var_t &v2) {
return wcscmp(v1.name, v2.name) < 0;
});
if (binsearch != last && wcscmp(name.c_str(), binsearch->name) == 0) {
return &*binsearch;
}
return nullptr;
}