diff --git a/src/buffer.cc b/src/buffer.cc index aa6314d6..3c402905 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -273,31 +273,28 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content) } else { - String prefix = m_lines[pos.line].substr(0, pos.column); - String suffix = m_lines[pos.line].substr(pos.column); + StringView prefix = m_lines[pos.line].substr(0, pos.column); + StringView suffix = m_lines[pos.line].substr(pos.column); - std::vector new_lines; + std::vector new_lines; ByteCount start = 0; for (ByteCount i = 0; i < content.length(); ++i) { if (content[i] == '\n') { - String line_content = content.substr(start, i + 1 - start); + StringView line_content = content.substr(start, i + 1 - start); if (start == 0) - { - line_content = prefix + line_content; - new_lines.push_back(std::move(line_content)); - } + new_lines.emplace_back(prefix + line_content); else - new_lines.push_back(std::move(line_content)); + new_lines.push_back(line_content); start = i + 1; } } if (start == 0) - new_lines.push_back(prefix + content + suffix); + new_lines.emplace_back(prefix + content + suffix); else if (start != content.length() or not suffix.empty()) - new_lines.push_back(content.substr(start) + suffix); + new_lines.emplace_back(content.substr(start) + suffix); LineCount last_line = pos.line + new_lines.size() - 1; @@ -327,7 +324,7 @@ ByteCoord Buffer::do_erase(ByteCoord begin, ByteCoord end) if (new_line.length() != 0) { m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line); - m_lines[begin.line] = std::move(new_line); + m_lines[begin.line] = InternedString(new_line); next = begin; } else @@ -368,21 +365,24 @@ void Buffer::apply_modification(const Modification& modification) } } -BufferIterator Buffer::insert(const BufferIterator& pos, String content) +BufferIterator Buffer::insert(const BufferIterator& pos, StringView content) { kak_assert(is_valid(pos.coord())); if (content.empty()) return pos; + InternedString real_content; if (pos == end() and content.back() != '\n') - content += '\n'; + real_content = InternedString(content + "\n"); + else + real_content = content; // for undo and redo purpose it is better to use one past last line rather // than one past last char coord. auto coord = pos == end() ? ByteCoord{line_count()} : pos.coord(); if (not (m_flags & Flags::NoUndo)) - m_current_undo_group.emplace_back(Modification::Insert, coord, content); - return {*this, do_insert(pos.coord(), content)}; + m_current_undo_group.emplace_back(Modification::Insert, coord, real_content); + return {*this, do_insert(pos.coord(), real_content)}; } BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end) @@ -396,7 +396,7 @@ BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end) if (not (m_flags & Flags::NoUndo)) m_current_undo_group.emplace_back(Modification::Erase, begin.coord(), - string(begin.coord(), end.coord())); + InternedString(string(begin.coord(), end.coord()))); return {*this, do_erase(begin.coord(), end.coord())}; } @@ -452,7 +452,7 @@ ByteCoord Buffer::char_next(ByteCoord coord) const { if (coord.column < m_lines[coord.line].length() - 1) { - auto& line = m_lines[coord.line]; + auto line = m_lines[coord.line]; coord.column += utf8::codepoint_size(line[(int)coord.column]); // Handle invalid utf-8 if (coord.column >= line.length()) @@ -483,7 +483,7 @@ ByteCoord Buffer::char_prev(ByteCoord coord) const } else { - auto& line = m_lines[coord.line]; + auto line = m_lines[coord.line]; coord.column = (int)(utf8::character_start(line.begin() + (int)coord.column - 1, line.begin()) - line.begin()); } return coord; diff --git a/src/buffer.hh b/src/buffer.hh index e24bc786..01504170 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -6,7 +6,7 @@ #include "option_manager.hh" #include "keymap_manager.hh" #include "safe_ptr.hh" -#include "string.hh" +#include "interned_string.hh" #include "value.hh" #include @@ -92,7 +92,7 @@ public: bool set_name(String name); - BufferIterator insert(const BufferIterator& pos, String content); + BufferIterator insert(const BufferIterator& pos, StringView content); BufferIterator erase(BufferIterator begin, BufferIterator end); size_t timestamp() const; @@ -126,7 +126,7 @@ public: BufferIterator end() const; LineCount line_count() const; - const String& operator[](LineCount line) const + const StringView& operator[](LineCount line) const { return m_lines[line]; } // returns an iterator at given coordinates. clamp line_and_column @@ -178,15 +178,15 @@ private: void on_option_changed(const Option& option) override; - struct LineList : std::vector + struct LineList : std::vector { [[gnu::always_inline]] - String& operator[](LineCount line) - { return std::vector::operator[]((int)line); } + InternedString& operator[](LineCount line) + { return std::vector::operator[]((int)line); } [[gnu::always_inline]] - const String& operator[](LineCount line) const - { return std::vector::operator[]((int)line); } + const InternedString& operator[](LineCount line) const + { return std::vector::operator[]((int)line); } }; LineList m_lines; diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index 19e35552..eadf6415 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -11,7 +11,7 @@ namespace Kakoune CharCount get_column(const Buffer& buffer, CharCount tabstop, ByteCoord coord) { - auto& line = buffer[coord.line]; + auto line = buffer[coord.line]; auto col = 0_char; for (auto it = line.begin(); it != line.end() and coord.column > (int)(it - line.begin()); diff --git a/src/display_buffer.hh b/src/display_buffer.hh index e8451753..8ad01661 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -31,7 +31,7 @@ public: { case BufferRange: { - auto& line = (*m_buffer)[m_begin.line]; + auto line = (*m_buffer)[m_begin.line]; if (m_begin.line == m_end.line) return line.substr(m_begin.column, m_end.column - m_begin.column); else if (m_begin.line+1 == m_end.line and m_end.column == 0) diff --git a/src/highlighters.cc b/src/highlighters.cc index 310ab223..a42b6c94 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -724,8 +724,8 @@ void find_matches(const Buffer& buffer, RegexMatchList& matches, const Regex& re const size_t buf_timestamp = buffer.timestamp(); for (auto line = 0_line, end = buffer.line_count(); line < end; ++line) { - auto& l = buffer[line]; - for (boost::regex_iterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) + auto l = buffer[line]; + for (boost::regex_iterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) { ByteCount b = (int)((*it)[0].first - l.begin()); ByteCount e = (int)((*it)[0].second - l.begin()); @@ -778,8 +778,8 @@ void update_matches(const Buffer& buffer, memoryview modifs, line < modif.new_line + modif.num_added+1 and line < buffer.line_count(); ++line) { - auto& l = buffer[line]; - for (boost::regex_iterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) + auto l = buffer[line]; + for (boost::regex_iterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) { ByteCount b = (int)((*it)[0].first - l.begin()); ByteCount e = (int)((*it)[0].second - l.begin()); diff --git a/src/interned_string.hh b/src/interned_string.hh index 5d81afd3..23984a5d 100644 --- a/src/interned_string.hh +++ b/src/interned_string.hh @@ -39,7 +39,7 @@ public: InternedString(const char* str) : StringView() { acquire_ifn(str); } InternedString(StringView str) : StringView() { acquire_ifn(str); } - InternedString(const String& str) : StringView() { acquire_ifn(str); } + //InternedString(const String& str) : StringView() { acquire_ifn(str); } InternedString& operator=(const InternedString& str) { diff --git a/src/normal.cc b/src/normal.cc index 9b6ea48e..fbd23ae8 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -804,7 +804,7 @@ void deindent(Context& context, int) for (auto line = sel.min().line; line < sel.max().line+1; ++line) { CharCount width = 0; - auto& content = buffer[line]; + auto content = buffer[line]; for (auto column = 0_byte; column < content.length(); ++column) { const char c = content[column]; @@ -1105,7 +1105,7 @@ void copy_indent(Context& context, int selection) selection = context.selections().main_index() + 1; auto ref_line = selections[selection-1].min().line; - const String& line = buffer[ref_line]; + auto line = buffer[ref_line]; auto it = line.begin(); while (it != line.end() and is_horizontal_blank(*it)) ++it; @@ -1117,7 +1117,7 @@ void copy_indent(Context& context, int selection) if (l == ref_line) continue; - auto& line = buffer[l]; + auto line = buffer[l]; ByteCount i = 0; while (i < line.length() and is_horizontal_blank(line[i])) ++i; diff --git a/src/remote.cc b/src/remote.cc index a5643387..12bdf580 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -35,7 +35,7 @@ class Message { public: Message(int sock) : m_socket(sock) {} - ~Message() + ~Message() noexcept(false) { if (m_stream.size() == 0) return; diff --git a/src/selection.cc b/src/selection.cc index f4640e5e..b188a2fe 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -410,8 +410,8 @@ namespace inline void _avoid_eol(const Buffer& buffer, ByteCoord& coord) { - const auto column = coord.column; - const auto& line = buffer[coord.line]; + auto column = coord.column; + auto line = buffer[coord.line]; if (column != 0 and column == line.length() - 1) coord.column = line.byte_count_to(line.char_length() - 2); }