From e69db0f671a6f7cdbc10fe3e7099f0c2fc4aa211 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 3 Sep 2015 13:21:35 +0100 Subject: [PATCH] Regex are overkill for shell manager env vars, we just need exact match or prefix match --- src/main.cc | 41 +++++++++++++++++++++-------------------- src/shell_manager.cc | 12 +++++++----- src/shell_manager.hh | 5 +++-- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main.cc b/src/main.cc index 906326bc..d11fa828 100644 --- a/src/main.cc +++ b/src/main.cc @@ -55,92 +55,93 @@ void register_env_vars() { static const struct { const char* name; + bool prefix; String (*func)(StringView, const Context&); } env_vars[] = { { - "bufname", + "bufname", false, [](StringView name, const Context& context) -> String { return context.buffer().display_name(); } }, { - "buffile", + "buffile", false, [](StringView name, const Context& context) -> String { return context.buffer().name(); } }, { - "buflist", + "buflist", false, [](StringView name, const Context& context) { return join(transformed(BufferManager::instance(), [](const SafePtr& b) { return b->display_name(); }), ':'); } }, { - "timestamp", + "timestamp", false, [](StringView name, const Context& context) -> String { return to_string(context.buffer().timestamp()); } }, { - "selection", + "selection", false, [](StringView name, const Context& context) { const Selection& sel = context.selections().main(); return content(context.buffer(), sel); } }, { - "selections", + "selections", false, [](StringView name, const Context& context) { return join(context.selections_content(), ':'); } }, { - "runtime", + "runtime", false, [](StringView name, const Context& context) { return runtime_directory(); } }, { - "opt_.+", + "opt_", true, [](StringView name, const Context& context) { return context.options()[name.substr(4_byte)].get_as_string(); } }, { - "reg_.+", + "reg_", true, [](StringView name, const Context& context) { return context.main_sel_register_value(name.substr(4_byte)).str(); } }, { - "client_env_.+", + "client_env_", true, [](StringView name, const Context& context) { return context.client().get_env_var(name.substr(11_byte)).str(); } }, { - "session", + "session", false, [](StringView name, const Context& context) -> String { return Server::instance().session(); } }, { - "client", + "client", false, [](StringView name, const Context& context) -> String { return context.name(); } }, { - "cursor_line", + "cursor_line", false, [](StringView name, const Context& context) -> String { return to_string(context.selections().main().cursor().line + 1); } }, { - "cursor_column", + "cursor_column", false, [](StringView name, const Context& context) -> String { return to_string(context.selections().main().cursor().column + 1); } }, { - "cursor_char_column", + "cursor_char_column", false, [](StringView name, const Context& context) -> String { auto coord = context.selections().main().cursor(); return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); } }, { - "selection_desc", + "selection_desc", false, [](StringView name, const Context& context) { return selection_to_string(context.selections().main()); } }, { - "selections_desc", + "selections_desc", false, [](StringView name, const Context& context) { return selection_list_to_string(context.selections()); } }, { - "window_width", + "window_width", false, [](StringView name, const Context& context) -> String { return to_string(context.window().dimensions().column); } }, { - "window_height", + "window_height", false, [](StringView name, const Context& context) -> String { return to_string(context.window().dimensions().line); } } }; ShellManager& shell_manager = ShellManager::instance(); for (auto& env_var : env_vars) - shell_manager.register_env_var(env_var.name, env_var.func); + shell_manager.register_env_var(env_var.name, env_var.prefix, env_var.func); } void register_registers() diff --git a/src/shell_manager.cc b/src/shell_manager.cc index 8aacee35..19d55bb3 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -135,22 +135,24 @@ std::pair ShellManager::eval( return {}; } -void ShellManager::register_env_var(StringView regex, +void ShellManager::register_env_var(StringView str, bool prefix, EnvVarRetriever retriever) { - m_env_vars.push_back({ Regex{regex}, std::move(retriever) }); + m_env_vars.push_back({ str.str(), prefix, std::move(retriever) }); } String ShellManager::get_val(StringView name, const Context& context) const { auto env_var = std::find_if( m_env_vars.begin(), m_env_vars.end(), - [name](const std::pair& pair) - { return regex_match(name.begin(), name.end(), pair.first); }); + [name](const EnvVarDesc& desc) { + return desc.prefix ? prefix_match(name, desc.str) : name == desc.str; + }); if (env_var == m_env_vars.end()) throw runtime_error("no such env var: " + name); - return env_var->second(name, context); + + return env_var->func(name, context); } } diff --git a/src/shell_manager.hh b/src/shell_manager.hh index 586cf59b..d4fb23a1 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -33,11 +33,12 @@ public: ConstArrayView params = {}, const EnvVarMap& env_vars = EnvVarMap{}); - void register_env_var(StringView regex, EnvVarRetriever retriever); + void register_env_var(StringView str, bool prefix, EnvVarRetriever retriever); String get_val(StringView name, const Context& context) const; private: - Vector> m_env_vars; + struct EnvVarDesc { String str; bool prefix; EnvVarRetriever func; }; + Vector m_env_vars; }; template<> struct WithBitOps : std::true_type {};