From fc4142178f2619a9ba0cac62ce1081590a56ed79 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 20 Oct 2014 19:18:38 +0100 Subject: [PATCH] Port more code to StringView instead of const String& --- src/assert.cc | 4 ++-- src/buffer.cc | 2 +- src/buffer.hh | 2 +- src/buffer_utils.cc | 2 +- src/client_manager.cc | 8 ++++---- src/client_manager.hh | 8 ++++---- src/commands.cc | 4 ++-- src/function_registry.hh | 2 +- src/hook_manager.cc | 3 +-- src/hook_manager.hh | 4 ++-- src/input_handler.cc | 6 +++--- src/normal.cc | 4 ++-- src/parameters_parser.hh | 4 ++-- src/remote.cc | 13 +++++++------ src/remote.hh | 10 +++++----- 15 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/assert.cc b/src/assert.cc index e93319a4..018567da 100644 --- a/src/assert.cc +++ b/src/assert.cc @@ -15,8 +15,8 @@ namespace Kakoune struct assert_failed : logic_error { - assert_failed(const String& message) - : m_message(message) {} + assert_failed(String message) + : m_message(std::move(message)) {} const char* what() const override { return m_message.c_str(); } private: diff --git a/src/buffer.cc b/src/buffer.cc index 2f80cc9c..4e5b1f8e 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -507,7 +507,7 @@ void Buffer::on_option_changed(const Option& option) option.name() + "=" + option.get_as_string()); } -void Buffer::run_hook_in_own_context(const String& hook_name, const String& param) +void Buffer::run_hook_in_own_context(const String& hook_name, StringView param) { InputHandler hook_handler({ *this, Selection{} }); m_hooks.run_hook(hook_name, param, hook_handler.context()); diff --git a/src/buffer.hh b/src/buffer.hh index 303123d3..4f0da060 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -157,7 +157,7 @@ public: ValueMap& values() const { return m_values; } - void run_hook_in_own_context(const String& hook_name, const String& param); + void run_hook_in_own_context(const String& hook_name, StringView param); void reload(std::vector lines, time_t fs_timestamp = InvalidTime); diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index 1d7e7d25..c3dd755c 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -133,7 +133,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) }); buffer->hooks().add_hook("BufClose", "", - [buffer, watcher](const String&, const Context&) { + [buffer, watcher](StringView, const Context&) { // Check if fifo is still alive, else watcher is already dead if (buffer->flags() & Buffer::Flags::Fifo) { diff --git a/src/client_manager.cc b/src/client_manager.cc index bc833c62..87abcf3e 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -26,7 +26,7 @@ String ClientManager::generate_name() const Client* ClientManager::create_client(std::unique_ptr&& ui, EnvVarMap env_vars, - const String& init_commands) + StringView init_commands) { Buffer& buffer = **BufferManager::instance().begin(); WindowAndSelections ws = get_free_window(buffer); @@ -139,14 +139,14 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) m_free_windows.erase(end, m_free_windows.end()); } -bool ClientManager::validate_client_name(const String& name) const +bool ClientManager::validate_client_name(StringView name) const { auto it = find_if(m_clients, [&](const std::unique_ptr& client) { return client->context().name() == name; }); return it == m_clients.end(); } -Client* ClientManager::get_client_ifp(const String& name) +Client* ClientManager::get_client_ifp(StringView name) { for (auto& client : m_clients) { @@ -156,7 +156,7 @@ Client* ClientManager::get_client_ifp(const String& name) return nullptr; } -Client& ClientManager::get_client(const String& name) +Client& ClientManager::get_client(StringView name) { Client* client = get_client_ifp(name); if (not client) diff --git a/src/client_manager.hh b/src/client_manager.hh index e4d6da43..2c2e01bc 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -23,7 +23,7 @@ public: ~ClientManager(); Client* create_client(std::unique_ptr&& ui, - EnvVarMap env_vars, const String& init_cmd); + EnvVarMap env_vars, StringView init_cmd); bool empty() const { return m_clients.empty(); } size_t count() const { return m_clients.size(); } @@ -36,9 +36,9 @@ public: void redraw_clients() const; void clear_mode_trashes() const; - Client* get_client_ifp(const String& name); - Client& get_client(const String& name); - bool validate_client_name(const String& name) const; + Client* get_client_ifp(StringView name); + Client& get_client(StringView name); + bool validate_client_name(StringView name) const; void remove_client(Client& client); CandidateList complete_client_name(StringView name, diff --git a/src/commands.cc b/src/commands.cc index cdab704f..1770df1e 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -546,13 +546,13 @@ const CommandDesc add_hook_cmd = { // copy so that the lambda gets a copy as well Regex regex(parser[2].begin(), parser[2].end()); String command = parser[3]; - auto hook_func = [=](const String& param, Context& context) { + auto hook_func = [=](StringView param, Context& context) { if (context.are_user_hooks_disabled()) return; if (regex_match(param.begin(), param.end(), regex)) CommandManager::instance().execute(command, context, {}, - { { "hook_param", param } }); + { { "hook_param", param.str() } }); }; StringView group; if (parser.has_option("group")) diff --git a/src/function_registry.hh b/src/function_registry.hh index b7296eac..5f3732ba 100644 --- a/src/function_registry.hh +++ b/src/function_registry.hh @@ -11,7 +11,7 @@ namespace Kakoune struct function_not_found : runtime_error { - function_not_found(const String& name) + function_not_found(StringView name) : runtime_error("'" + name + "' not found") {} }; diff --git a/src/hook_manager.cc b/src/hook_manager.cc index cfade4d7..70e1c8eb 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -37,8 +37,7 @@ CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_ } void HookManager::run_hook(const String& hook_name, - const String& param, - Context& context) const + StringView param, Context& context) const { if (m_parent) m_parent->run_hook(hook_name, param, context); diff --git a/src/hook_manager.hh b/src/hook_manager.hh index e5c94850..dd1c6eeb 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -10,7 +10,7 @@ namespace Kakoune { class Context; -using HookFunc = std::function; +using HookFunc = std::function; class HookManager { @@ -20,7 +20,7 @@ public: void add_hook(const String& hook_name, String group, HookFunc hook); void remove_hooks(StringView group); CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token); - void run_hook(const String& hook_name, const String& param, + void run_hook(const String& hook_name, StringView param, Context& context) const; private: diff --git a/src/input_handler.cc b/src/input_handler.cc index e4368e7f..badac088 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -274,12 +274,12 @@ public: } } - void insert(const String& str) + void insert(StringView str) { insert_from(m_cursor_pos, str); } - void insert_from(CharCount start, const String& str) + void insert_from(CharCount start, StringView str) { kak_assert(start <= m_cursor_pos); m_line = m_line.substr(0, start) + str @@ -459,7 +459,7 @@ String common_prefix(memoryview strings) return res; } -void history_push(std::vector& history, const String& entry) +void history_push(std::vector& history, StringView entry) { if(entry.empty()) { diff --git a/src/normal.cc b/src/normal.cc index 49eae94d..67640b7d 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -107,7 +107,7 @@ void repeat_last_insert(Context& context, int) context.input_handler().repeat_last_insert(); } -bool show_auto_info_ifn(const String& title, const String& info, +bool show_auto_info_ifn(StringView title, StringView info, const Context& context) { if (context.options()["autoinfo"].get() < 1 or not context.has_ui()) @@ -121,7 +121,7 @@ bool show_auto_info_ifn(const String& title, const String& info, template void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, Cmd cmd, - const String& title, const String& info) + StringView title, StringView info) { const bool hide = show_auto_info_ifn(title, info, context); context.input_handler().on_next_key( diff --git a/src/parameters_parser.hh b/src/parameters_parser.hh index 5b177bb3..219c9454 100644 --- a/src/parameters_parser.hh +++ b/src/parameters_parser.hh @@ -18,13 +18,13 @@ struct parameter_error : public runtime_error struct unknown_option : public parameter_error { - unknown_option(const String& name) + unknown_option(StringView name) : parameter_error("unknown option '" + name + "'") {} }; struct missing_option_value: public parameter_error { - missing_option_value(const String& name) + missing_option_value(StringView name) : parameter_error("missing value for option '" + name + "'") {} }; diff --git a/src/remote.cc b/src/remote.cc index 12bdf580..58dd7233 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -409,12 +409,13 @@ void RemoteUI::set_input_callback(InputCallback callback) RemoteClient::RemoteClient(int socket, std::unique_ptr&& ui, const EnvVarMap& env_vars, - const String& init_command) + StringView init_command) : m_ui(std::move(ui)), m_dimensions(m_ui->dimensions()), m_socket_watcher{socket, [this](FDWatcher&){ process_available_messages(); }} { Message msg(socket); - msg.write(init_command.c_str(), (int)init_command.length()+1); + msg.write(init_command.data(), (int)init_command.length()); + msg.write((char)0); msg.write(env_vars); Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) | @@ -504,10 +505,10 @@ void RemoteClient::write_next_key() } } -std::unique_ptr connect_to(const String& session, +std::unique_ptr connect_to(StringView session, std::unique_ptr&& ui, const EnvVarMap& env_vars, - const String& init_command) + StringView init_command) { auto filename = "/tmp/kak-" + session; @@ -524,7 +525,7 @@ std::unique_ptr connect_to(const String& session, init_command}}; } -void send_command(const String& session, const String& command) +void send_command(StringView session, StringView command) { auto filename = "/tmp/kak-" + session; @@ -538,7 +539,7 @@ void send_command(const String& session, const String& command) { Message msg(sock); - msg.write(command.c_str(), (int)command.length()); + msg.write(command.data(), (int)command.length()); } close(sock); } diff --git a/src/remote.hh b/src/remote.hh index aa2af7b1..2d56011c 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -13,7 +13,7 @@ struct peer_disconnected {}; struct connection_failed : runtime_error { - connection_failed(const String& filename) + connection_failed(StringView filename) : runtime_error{"connect to " + filename + " failed"} {} }; @@ -24,7 +24,7 @@ class RemoteClient { public: RemoteClient(int socket, std::unique_ptr&& ui, - const EnvVarMap& env_vars, const String& init_command); + const EnvVarMap& env_vars, StringView init_command); private: void process_available_messages(); @@ -35,12 +35,12 @@ private: CharCoord m_dimensions; FDWatcher m_socket_watcher; }; -std::unique_ptr connect_to(const String& session, +std::unique_ptr connect_to(StringView session, std::unique_ptr&& ui, const EnvVarMap& env_vars, - const String& init_command); + StringView init_command); -void send_command(const String& session, const String& command); +void send_command(StringView session, StringView command); struct Server : public Singleton {