Statically assert the sort order of more lists

Add compile-time checks to ensure list of string subcommands, builtins,
and electric variables are kept in asciibetical order to facilitate
binary search lookups.
This commit is contained in:
Mahmoud Al-Qudsi 2021-02-08 15:31:49 -06:00
parent cb3ab80cab
commit 2d39568ec4
2 changed files with 10 additions and 6 deletions

View file

@ -1738,7 +1738,7 @@ static int string_upper(parser_t &parser, io_streams_t &streams, int argc, wchar
} }
// Keep sorted alphabetically // Keep sorted alphabetically
static const struct string_subcommand { static constexpr const struct string_subcommand {
const wchar_t *name; const wchar_t *name;
int (*handler)(parser_t &, io_streams_t &, int argc, //!OCLINT(unused param) int (*handler)(parser_t &, io_streams_t &, int argc, //!OCLINT(unused param)
wchar_t **argv); //!OCLINT(unused param) wchar_t **argv); //!OCLINT(unused param)
@ -1750,6 +1750,7 @@ static const struct string_subcommand {
{L"sub", &string_sub}, {L"trim", &string_trim}, {L"unescape", &string_unescape}, {L"sub", &string_sub}, {L"trim", &string_trim}, {L"unescape", &string_unescape},
{L"upper", &string_upper}, {L"upper", &string_upper},
}; };
ASSERT_SORT_ORDER(string_subcommands, .name);
/// The string builtin, for manipulating strings. /// The string builtin, for manipulating strings.
maybe_t<int> builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv) { maybe_t<int> builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv) {

View file

@ -83,7 +83,7 @@ struct electric_var_t {
}; };
// Keep sorted alphabetically // Keep sorted alphabetically
static const std::vector<electric_var_t> electric_variables{ static constexpr const electric_var_t electric_variables[] = {
{L"FISH_VERSION", electric_var_t::freadonly}, {L"FISH_VERSION", electric_var_t::freadonly},
{L"PWD", electric_var_t::freadonly | electric_var_t::fcomputed | electric_var_t::fexports}, {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"SHLVL", electric_var_t::freadonly | electric_var_t::fexports},
@ -98,16 +98,19 @@ static const std::vector<electric_var_t> electric_variables{
{L"umask", electric_var_t::fcomputed}, {L"umask", electric_var_t::fcomputed},
{L"version", electric_var_t::freadonly}, {L"version", electric_var_t::freadonly},
}; };
ASSERT_SORT_ORDER(electric_variables, .name);
const electric_var_t *electric_var_t::for_name(const wcstring &name) { const electric_var_t *electric_var_t::for_name(const wcstring &name) {
static auto first = electric_variables.begin(); constexpr auto electric_var_count = sizeof(electric_variables) / sizeof(electric_variables[0]);
static auto last = electric_variables.end(); constexpr auto begin = &electric_variables[0];
constexpr auto end = &electric_variables[0] + electric_var_count;
electric_var_t search{name.c_str(), 0}; electric_var_t search{name.c_str(), 0};
auto binsearch = std::lower_bound(first, last, search, auto binsearch = std::lower_bound(begin, end, search,
[&](const electric_var_t &v1, const electric_var_t &v2) { [&](const electric_var_t &v1, const electric_var_t &v2) {
return wcscmp(v1.name, v2.name) < 0; return wcscmp(v1.name, v2.name) < 0;
}); });
if (binsearch != last && wcscmp(name.c_str(), binsearch->name) == 0) { if (binsearch != end && wcscmp(name.c_str(), binsearch->name) == 0) {
return &*binsearch; return &*binsearch;
} }
return nullptr; return nullptr;