Port more code to StringView instead of const String&

This commit is contained in:
Maxime Coste 2014-10-20 19:18:38 +01:00
parent 69113e2711
commit fc4142178f
15 changed files with 38 additions and 38 deletions

View File

@ -15,8 +15,8 @@ namespace Kakoune
struct assert_failed : logic_error struct assert_failed : logic_error
{ {
assert_failed(const String& message) assert_failed(String message)
: m_message(message) {} : m_message(std::move(message)) {}
const char* what() const override { return m_message.c_str(); } const char* what() const override { return m_message.c_str(); }
private: private:

View File

@ -507,7 +507,7 @@ void Buffer::on_option_changed(const Option& option)
option.name() + "=" + option.get_as_string()); 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{} }); InputHandler hook_handler({ *this, Selection{} });
m_hooks.run_hook(hook_name, param, hook_handler.context()); m_hooks.run_hook(hook_name, param, hook_handler.context());

View File

@ -157,7 +157,7 @@ public:
ValueMap& values() const { return m_values; } 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<String> lines, time_t fs_timestamp = InvalidTime); void reload(std::vector<String> lines, time_t fs_timestamp = InvalidTime);

View File

@ -133,7 +133,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
}); });
buffer->hooks().add_hook("BufClose", "", 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 // Check if fifo is still alive, else watcher is already dead
if (buffer->flags() & Buffer::Flags::Fifo) if (buffer->flags() & Buffer::Flags::Fifo)
{ {

View File

@ -26,7 +26,7 @@ String ClientManager::generate_name() const
Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui, Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
EnvVarMap env_vars, EnvVarMap env_vars,
const String& init_commands) StringView init_commands)
{ {
Buffer& buffer = **BufferManager::instance().begin(); Buffer& buffer = **BufferManager::instance().begin();
WindowAndSelections ws = get_free_window(buffer); 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()); 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>& client) auto it = find_if(m_clients, [&](const std::unique_ptr<Client>& client)
{ return client->context().name() == name; }); { return client->context().name() == name; });
return it == m_clients.end(); 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) for (auto& client : m_clients)
{ {
@ -156,7 +156,7 @@ Client* ClientManager::get_client_ifp(const String& name)
return nullptr; return nullptr;
} }
Client& ClientManager::get_client(const String& name) Client& ClientManager::get_client(StringView name)
{ {
Client* client = get_client_ifp(name); Client* client = get_client_ifp(name);
if (not client) if (not client)

View File

@ -23,7 +23,7 @@ public:
~ClientManager(); ~ClientManager();
Client* create_client(std::unique_ptr<UserInterface>&& ui, Client* create_client(std::unique_ptr<UserInterface>&& ui,
EnvVarMap env_vars, const String& init_cmd); EnvVarMap env_vars, StringView init_cmd);
bool empty() const { return m_clients.empty(); } bool empty() const { return m_clients.empty(); }
size_t count() const { return m_clients.size(); } size_t count() const { return m_clients.size(); }
@ -36,9 +36,9 @@ public:
void redraw_clients() const; void redraw_clients() const;
void clear_mode_trashes() const; void clear_mode_trashes() const;
Client* get_client_ifp(const String& name); Client* get_client_ifp(StringView name);
Client& get_client(const String& name); Client& get_client(StringView name);
bool validate_client_name(const String& name) const; bool validate_client_name(StringView name) const;
void remove_client(Client& client); void remove_client(Client& client);
CandidateList complete_client_name(StringView name, CandidateList complete_client_name(StringView name,

View File

@ -546,13 +546,13 @@ const CommandDesc add_hook_cmd = {
// copy so that the lambda gets a copy as well // copy so that the lambda gets a copy as well
Regex regex(parser[2].begin(), parser[2].end()); Regex regex(parser[2].begin(), parser[2].end());
String command = parser[3]; 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()) if (context.are_user_hooks_disabled())
return; return;
if (regex_match(param.begin(), param.end(), regex)) if (regex_match(param.begin(), param.end(), regex))
CommandManager::instance().execute(command, context, {}, CommandManager::instance().execute(command, context, {},
{ { "hook_param", param } }); { { "hook_param", param.str() } });
}; };
StringView group; StringView group;
if (parser.has_option("group")) if (parser.has_option("group"))

View File

@ -11,7 +11,7 @@ namespace Kakoune
struct function_not_found : runtime_error struct function_not_found : runtime_error
{ {
function_not_found(const String& name) function_not_found(StringView name)
: runtime_error("'" + name + "' not found") {} : runtime_error("'" + name + "' not found") {}
}; };

View File

@ -37,8 +37,7 @@ CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_
} }
void HookManager::run_hook(const String& hook_name, void HookManager::run_hook(const String& hook_name,
const String& param, StringView param, Context& context) const
Context& context) const
{ {
if (m_parent) if (m_parent)
m_parent->run_hook(hook_name, param, context); m_parent->run_hook(hook_name, param, context);

View File

@ -10,7 +10,7 @@ namespace Kakoune
{ {
class Context; class Context;
using HookFunc = std::function<void (const String&, Context&)>; using HookFunc = std::function<void (StringView, Context&)>;
class HookManager class HookManager
{ {
@ -20,7 +20,7 @@ public:
void add_hook(const String& hook_name, String group, HookFunc hook); void add_hook(const String& hook_name, String group, HookFunc hook);
void remove_hooks(StringView group); void remove_hooks(StringView group);
CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token); 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; Context& context) const;
private: private:

View File

@ -274,12 +274,12 @@ public:
} }
} }
void insert(const String& str) void insert(StringView str)
{ {
insert_from(m_cursor_pos, 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); kak_assert(start <= m_cursor_pos);
m_line = m_line.substr(0, start) + str m_line = m_line.substr(0, start) + str
@ -459,7 +459,7 @@ String common_prefix(memoryview<String> strings)
return res; return res;
} }
void history_push(std::vector<String>& history, const String& entry) void history_push(std::vector<String>& history, StringView entry)
{ {
if(entry.empty()) if(entry.empty())
{ {

View File

@ -107,7 +107,7 @@ void repeat_last_insert(Context& context, int)
context.input_handler().repeat_last_insert(); 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) const Context& context)
{ {
if (context.options()["autoinfo"].get<int>() < 1 or not context.has_ui()) if (context.options()["autoinfo"].get<int>() < 1 or not context.has_ui())
@ -121,7 +121,7 @@ bool show_auto_info_ifn(const String& title, const String& info,
template<typename Cmd> template<typename Cmd>
void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, Cmd cmd, 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); const bool hide = show_auto_info_ifn(title, info, context);
context.input_handler().on_next_key( context.input_handler().on_next_key(

View File

@ -18,13 +18,13 @@ struct parameter_error : public runtime_error
struct unknown_option : public parameter_error struct unknown_option : public parameter_error
{ {
unknown_option(const String& name) unknown_option(StringView name)
: parameter_error("unknown option '" + name + "'") {} : parameter_error("unknown option '" + name + "'") {}
}; };
struct missing_option_value: public parameter_error 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 + "'") {} : parameter_error("missing value for option '" + name + "'") {}
}; };

View File

@ -409,12 +409,13 @@ void RemoteUI::set_input_callback(InputCallback callback)
RemoteClient::RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui, RemoteClient::RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, const EnvVarMap& env_vars,
const String& init_command) StringView init_command)
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions()), : m_ui(std::move(ui)), m_dimensions(m_ui->dimensions()),
m_socket_watcher{socket, [this](FDWatcher&){ process_available_messages(); }} m_socket_watcher{socket, [this](FDWatcher&){ process_available_messages(); }}
{ {
Message msg(socket); 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); msg.write(env_vars);
Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) | Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) |
@ -504,10 +505,10 @@ void RemoteClient::write_next_key()
} }
} }
std::unique_ptr<RemoteClient> connect_to(const String& session, std::unique_ptr<RemoteClient> connect_to(StringView session,
std::unique_ptr<UserInterface>&& ui, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, const EnvVarMap& env_vars,
const String& init_command) StringView init_command)
{ {
auto filename = "/tmp/kak-" + session; auto filename = "/tmp/kak-" + session;
@ -524,7 +525,7 @@ std::unique_ptr<RemoteClient> connect_to(const String& session,
init_command}}; init_command}};
} }
void send_command(const String& session, const String& command) void send_command(StringView session, StringView command)
{ {
auto filename = "/tmp/kak-" + session; auto filename = "/tmp/kak-" + session;
@ -538,7 +539,7 @@ void send_command(const String& session, const String& command)
{ {
Message msg(sock); Message msg(sock);
msg.write(command.c_str(), (int)command.length()); msg.write(command.data(), (int)command.length());
} }
close(sock); close(sock);
} }

View File

@ -13,7 +13,7 @@ struct peer_disconnected {};
struct connection_failed : runtime_error struct connection_failed : runtime_error
{ {
connection_failed(const String& filename) connection_failed(StringView filename)
: runtime_error{"connect to " + filename + " failed"} : runtime_error{"connect to " + filename + " failed"}
{} {}
}; };
@ -24,7 +24,7 @@ class RemoteClient
{ {
public: public:
RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui, RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, const String& init_command); const EnvVarMap& env_vars, StringView init_command);
private: private:
void process_available_messages(); void process_available_messages();
@ -35,12 +35,12 @@ private:
CharCoord m_dimensions; CharCoord m_dimensions;
FDWatcher m_socket_watcher; FDWatcher m_socket_watcher;
}; };
std::unique_ptr<RemoteClient> connect_to(const String& session, std::unique_ptr<RemoteClient> connect_to(StringView session,
std::unique_ptr<UserInterface>&& ui, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, 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<Server> struct Server : public Singleton<Server>
{ {