From 7cdb94d5c7c4acb5febceabab3a163496504e476 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 20 Apr 2014 12:15:31 +0100 Subject: [PATCH] Use StringView in shell manager --- src/main.cc | 36 ++++++++++++++++++------------------ src/shell_manager.cc | 32 ++++++++++++++++---------------- src/shell_manager.hh | 10 +++++----- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/main.cc b/src/main.cc index 43b9675ab..3cdbf997c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -59,27 +59,27 @@ void register_env_vars() { static const struct { const char* name; - String (*func)(const String&, const Context&); + String (*func)(StringView, const Context&); } env_vars[] = { { "bufname", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return context.buffer().display_name(); } }, { "buffile", - [](const String& name, const Context& context) -> String + [](StringView name, const Context& context) -> String { return context.buffer().name(); } }, { "timestamp", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return to_string(context.buffer().timestamp()); } }, { "selection", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { const Selection& sel = context.selections().main(); return content(context.buffer(), sel); } }, { "selections", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { auto sels = context.selections_content(); String res; for (size_t i = 0; i < sels.size(); ++i) @@ -91,55 +91,55 @@ void register_env_vars() return res; } }, { "runtime", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return runtime_directory(); } }, { "opt_.+", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return context.options()[name.substr(4_byte)].get_as_string(); } }, { "reg_.+", - [](const String& name, const Context& context) -> String + [](StringView name, const Context& context) -> String { return RegisterManager::instance()[name[4]].values(context)[0]; } }, { "client_env_.+", - [](const String& name, const Context& context) -> String + [](StringView name, const Context& context) -> String { return context.client().get_env_var(name.substr(11_byte)); } }, { "session", - [](const String& name, const Context& context) -> String + [](StringView name, const Context& context) -> String { return Server::instance().session(); } }, { "client", - [](const String& name, const Context& context) -> String + [](StringView name, const Context& context) -> String { return context.name(); } }, { "cursor_line", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return to_string(context.selections().main().cursor().line + 1); } }, { "cursor_column", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return to_string(context.selections().main().cursor().column + 1); } }, { "cursor_char_column", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { auto coord = context.selections().main().cursor(); return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); } }, { "selection_desc", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { auto& sel = context.selections().main(); auto beg = sel.min(); return to_string(beg.line + 1) + ':' + to_string(beg.column + 1) + '+' + to_string((int)context.buffer().distance(beg, sel.max())+1); } }, { "window_width", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return to_string(context.window().dimensions().column); } }, { "window_height", - [](const String& name, const Context& context) + [](StringView name, const Context& context) { return to_string(context.window().dimensions().line); } } }; diff --git a/src/shell_manager.cc b/src/shell_manager.cc index bdda6c05f..448b3ddce 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -16,15 +16,15 @@ ShellManager::ShellManager() { } -String ShellManager::eval(const String& cmdline, const Context& context, +String ShellManager::eval(StringView cmdline, const Context& context, memoryview params, const EnvVarMap& env_vars) { return pipe("", cmdline, context, params, env_vars); } -String ShellManager::pipe(const String& input, - const String& cmdline, const Context& context, +String ShellManager::pipe(StringView input, + StringView cmdline, const Context& context, memoryview params, const EnvVarMap& env_vars) { @@ -43,8 +43,7 @@ String ShellManager::pipe(const String& input, close(read_pipe[1]); close(error_pipe[1]); - auto data = input.data(); - write(write_pipe[1], data.pointer(), data.size()); + write(write_pipe[1], input.data(), (int)input.length()); close(write_pipe[1]); char buffer[1024]; @@ -79,18 +78,18 @@ String ShellManager::pipe(const String& input, dup2(error_pipe[1], 2); close(error_pipe[1]); dup2(write_pipe[0], 0); close(write_pipe[0]); - boost::regex_iterator it(cmdline.begin(), cmdline.end(), env_var_regex); - boost::regex_iterator end; + boost::regex_iterator it(cmdline.begin(), cmdline.end(), env_var_regex); + boost::regex_iterator end; while (it != end) { auto& match = *it; - String name; + StringView name; if (match[1].matched) - name = String(match[1].first, match[1].second); + name = StringView(match[1].first, match[1].second); else if (match[2].matched) - name = String(match[2].first, match[2].second); + name = StringView(match[2].first, match[2].second); else kak_assert(false); kak_assert(name.length() > 0); @@ -111,7 +110,7 @@ String ShellManager::pipe(const String& input, try { String value = env_var->second(name, context); - setenv(("kak_" + name).c_str(), value.c_str(), 1); + setenv(("kak_"_str + name).c_str(), value.c_str(), 1); } catch (runtime_error&) {} } @@ -119,22 +118,23 @@ String ShellManager::pipe(const String& input, ++it; } - String shell = "/bin/sh"; - std::vector execparams = { shell.c_str(), "-c", cmdline.c_str() }; + const char* shell = "/bin/sh"; + auto cmdlinezstr = cmdline.zstr(); + std::vector execparams = { shell, "-c", cmdlinezstr }; if (not params.empty()) - execparams.push_back(shell.c_str()); + execparams.push_back(shell); for (auto& param : params) execparams.push_back(param.c_str()); execparams.push_back(nullptr); - execvp(shell.c_str(), (char* const*)execparams.data()); + execvp(shell, (char* const*)execparams.data()); exit(-1); } catch (...) { exit(-1); } return output; } -void ShellManager::register_env_var(const String& regex, +void ShellManager::register_env_var(StringView regex, EnvVarRetriever retriever) { m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) }); diff --git a/src/shell_manager.hh b/src/shell_manager.hh index a6fe38fde..742ba1cd8 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -9,23 +9,23 @@ namespace Kakoune { class Context; -using EnvVarRetriever = std::function; +using EnvVarRetriever = std::function; class ShellManager : public Singleton { public: ShellManager(); - String eval(const String& cmdline, const Context& context, + String eval(StringView cmdline, const Context& context, memoryview params, const EnvVarMap& env_vars); - String pipe(const String& input, - const String& cmdline, const Context& context, + String pipe(StringView input, + StringView cmdline, const Context& context, memoryview params, const EnvVarMap& env_vars); - void register_env_var(const String& regex, EnvVarRetriever retriever); + void register_env_var(StringView regex, EnvVarRetriever retriever); private: std::vector> m_env_vars;