mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-26 10:43:47 +08:00
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:
parent
cb3ab80cab
commit
2d39568ec4
|
@ -1738,7 +1738,7 @@ static int string_upper(parser_t &parser, io_streams_t &streams, int argc, wchar
|
|||
}
|
||||
|
||||
// Keep sorted alphabetically
|
||||
static const struct string_subcommand {
|
||||
static constexpr const struct string_subcommand {
|
||||
const wchar_t *name;
|
||||
int (*handler)(parser_t &, io_streams_t &, int argc, //!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"upper", &string_upper},
|
||||
};
|
||||
ASSERT_SORT_ORDER(string_subcommands, .name);
|
||||
|
||||
/// The string builtin, for manipulating strings.
|
||||
maybe_t<int> builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
||||
|
|
13
src/env.cpp
13
src/env.cpp
|
@ -83,7 +83,7 @@ struct electric_var_t {
|
|||
};
|
||||
|
||||
// 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"PWD", electric_var_t::freadonly | electric_var_t::fcomputed | 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"version", electric_var_t::freadonly},
|
||||
};
|
||||
ASSERT_SORT_ORDER(electric_variables, .name);
|
||||
|
||||
const electric_var_t *electric_var_t::for_name(const wcstring &name) {
|
||||
static auto first = electric_variables.begin();
|
||||
static auto last = electric_variables.end();
|
||||
constexpr auto electric_var_count = sizeof(electric_variables) / sizeof(electric_variables[0]);
|
||||
constexpr auto begin = &electric_variables[0];
|
||||
constexpr auto end = &electric_variables[0] + electric_var_count;
|
||||
|
||||
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) {
|
||||
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 nullptr;
|
||||
|
|
Loading…
Reference in New Issue
Block a user