Remove unneeded string copies

This commit is contained in:
Maxime Coste 2014-11-17 19:38:30 +00:00
parent 7619fcb198
commit 0c3acb74c2
3 changed files with 9 additions and 8 deletions

View File

@ -121,7 +121,7 @@ void Client::redraw_ifn()
context().ui().refresh(); context().ui().refresh();
} }
static void reload_buffer(Context& context, const String& filename) static void reload_buffer(Context& context, StringView filename)
{ {
CharCoord view_pos = context.window().position(); CharCoord view_pos = context.window().position();
ByteCoord cursor_pos = context.selections().main().cursor(); ByteCoord cursor_pos = context.selections().main().cursor();

View File

@ -132,29 +132,30 @@ String read_file(StringView filename)
return read_fd(fd); return read_fd(fd);
} }
Buffer* create_buffer_from_file(String filename) Buffer* create_buffer_from_file(StringView filename)
{ {
filename = real_path(parse_filename(filename)); String real_filename = real_path(parse_filename(filename));
int fd = open(filename.c_str(), O_RDONLY); int fd = open(real_filename.c_str(), O_RDONLY);
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
return nullptr; return nullptr;
throw file_access_error(filename, strerror(errno)); throw file_access_error(real_filename, strerror(errno));
} }
auto close_fd = on_scope_end([&]{ close(fd); }); auto close_fd = on_scope_end([&]{ close(fd); });
struct stat st; struct stat st;
fstat(fd, &st); fstat(fd, &st);
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
throw file_access_error(filename, "is a directory"); throw file_access_error(real_filename, "is a directory");
const char* data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); const char* data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
auto unmap = on_scope_end([&]{ munmap((void*)data, st.st_size); }); auto unmap = on_scope_end([&]{ munmap((void*)data, st.st_size); });
return create_buffer_from_data({data, data + st.st_size}, filename, Buffer::Flags::File, st.st_mtime); return create_buffer_from_data({data, data + st.st_size}, real_filename,
Buffer::Flags::File, st.st_mtime);
} }
static void write(int fd, StringView data) static void write(int fd, StringView data)

View File

@ -36,7 +36,7 @@ String get_kak_binary_path();
String read_fd(int fd); String read_fd(int fd);
String read_file(StringView filename); String read_file(StringView filename);
Buffer* create_buffer_from_file(String filename); Buffer* create_buffer_from_file(StringView filename);
void write_buffer_to_file(Buffer& buffer, StringView filename); void write_buffer_to_file(Buffer& buffer, StringView filename);
void write_buffer_to_fd(Buffer& buffer, int fd); void write_buffer_to_fd(Buffer& buffer, int fd);