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
{
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:

View File

@ -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());

View File

@ -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<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, 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)
{

View File

@ -26,7 +26,7 @@ String ClientManager::generate_name() const
Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& 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>& 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)

View File

@ -23,7 +23,7 @@ public:
~ClientManager();
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(); }
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,

View File

@ -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"))

View File

@ -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") {}
};

View File

@ -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);

View File

@ -10,7 +10,7 @@ namespace Kakoune
{
class Context;
using HookFunc = std::function<void (const String&, Context&)>;
using HookFunc = std::function<void (StringView, Context&)>;
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:

View File

@ -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<String> strings)
return res;
}
void history_push(std::vector<String>& history, const String& entry)
void history_push(std::vector<String>& history, StringView entry)
{
if(entry.empty())
{

View File

@ -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<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>
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(

View File

@ -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 + "'") {}
};

View File

@ -409,12 +409,13 @@ void RemoteUI::set_input_callback(InputCallback callback)
RemoteClient::RemoteClient(int socket, std::unique_ptr<UserInterface>&& 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<RemoteClient> connect_to(const String& session,
std::unique_ptr<RemoteClient> connect_to(StringView session,
std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars,
const String& init_command)
StringView init_command)
{
auto filename = "/tmp/kak-" + session;
@ -524,7 +525,7 @@ std::unique_ptr<RemoteClient> 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);
}

View File

@ -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<UserInterface>&& 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<RemoteClient> connect_to(const String& session,
std::unique_ptr<RemoteClient> connect_to(StringView session,
std::unique_ptr<UserInterface>&& 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<Server>
{