From 0fba373d1d1b852b00db54ad344c69629ef4e0da Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 1 Jun 2020 20:46:48 +1000 Subject: [PATCH] Replace some tag dispatch with if constexpr --- src/command_manager.cc | 57 ++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index cbe29779..3a8ffaea 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -300,41 +300,10 @@ Token parse_percent_token(Reader& reader, bool throw_on_unterminated) } } -auto expand_option(const Option& opt, std::true_type) -{ - return opt.get_as_string(Quoting::Raw); -} - -auto expand_option(const Option& opt, std::false_type) -{ - return opt.get_as_strings(); -} - -auto expand_register(StringView reg, const Context& context, std::true_type) -{ - return context.main_sel_register_value(reg).str(); -} - -auto expand_register(StringView reg, const Context& context, std::false_type) -{ - return RegisterManager::instance()[reg].get(context) | gather>(); -} - -String expand_arobase(ConstArrayView params, std::true_type) -{ - return join(params, ' ', false); -} - -Vector expand_arobase(ConstArrayView params, std::false_type) -{ - return {params.begin(), params.end()}; -} - template std::conditional_t> expand_token(const Token& token, const Context& context, const ShellContext& shell_context) { - using IsSingle = std::integral_constant; auto& content = token.content; switch (token.type) { @@ -355,24 +324,40 @@ expand_token(const Token& token, const Context& context, const ShellContext& she return {str}; } case Token::Type::RegisterExpand: - return expand_register(content, context, IsSingle{}); + if constexpr (single) + return context.main_sel_register_value(content).str(); + else + return RegisterManager::instance()[content].get(context) | gather>(); case Token::Type::OptionExpand: - return expand_option(context.options()[content], IsSingle{}); + { + auto& opt = context.options()[content]; + if constexpr (single) + return opt.get_as_string(Quoting::Raw); + else + return opt.get_as_strings(); + } case Token::Type::ValExpand: { auto it = shell_context.env_vars.find(content); if (it != shell_context.env_vars.end()) return {it->value}; + + auto val = ShellManager::instance().get_val(content, context); if constexpr (single) - return join(ShellManager::instance().get_val(content, context), false, ' '); + return join(val, false, ' '); else - return ShellManager::instance().get_val(content, context); + return val; } case Token::Type::ArgExpand: { auto& params = shell_context.params; if (content == '@') - return expand_arobase(params, IsSingle{}); + { + if constexpr (single) + return join(params, ' ', false); + else + return Vector{params.begin(), params.end()}; + } const int arg = str_to_int(content)-1; if (arg < 0)