Avoid copying token content strings in expand_token

We can move that data instead of copying
This commit is contained in:
Maxime Coste 2021-07-09 17:03:22 +10:00
parent cac946b434
commit cd67f2cf11
2 changed files with 14 additions and 6 deletions

View File

@ -323,9 +323,9 @@ Token parse_percent_token(Reader& reader, bool throw_on_unterminated)
template<bool single> template<bool single>
std::conditional_t<single, String, Vector<String>> std::conditional_t<single, String, Vector<String>>
expand_token(const Token& token, const Context& context, const ShellContext& shell_context) expand_token(Token&& token, const Context& context, const ShellContext& shell_context)
{ {
auto& content = token.content; auto&& content = token.content;
switch (token.type) switch (token.type)
{ {
case Token::Type::ShellExpand: case Token::Type::ShellExpand:
@ -386,7 +386,7 @@ expand_token(const Token& token, const Context& context, const ShellContext& she
return {expand(content, context, shell_context)}; return {expand(content, context, shell_context)};
case Token::Type::Raw: case Token::Type::Raw:
case Token::Type::RawQuoted: case Token::Type::RawQuoted:
return {content}; return {std::move(content)};
default: kak_assert(false); default: kak_assert(false);
} }
return {}; return {};
@ -569,7 +569,7 @@ void CommandManager::execute(StringView command_line,
shell_context.params.end()); shell_context.params.end());
else else
{ {
auto tokens = expand_token<false>(*token, context, shell_context); auto tokens = expand_token<false>(*std::move(token), context, shell_context);
params.insert(params.end(), params.insert(params.end(),
std::make_move_iterator(tokens.begin()), std::make_move_iterator(tokens.begin()),
std::make_move_iterator(tokens.end())); std::make_move_iterator(tokens.end()));

View File

@ -68,12 +68,20 @@ public:
return m_value; return m_value;
} }
T& operator*() T& operator*() &
{ {
kak_assert(m_valid); kak_assert(m_valid);
return m_value; return m_value;
} }
const T& operator*() const { return *const_cast<Optional&>(*this); }
T&& operator*() &&
{
kak_assert(m_valid);
return std::move(m_value);
}
const T& operator*() const & { return *const_cast<Optional&>(*this); }
const T& operator*() const && { return *const_cast<Optional&>(*this); }
T* operator->() T* operator->()
{ {