From 580749a91da1e293c9dc8c5f3a72aa9c5a5e7666 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 5 Jun 2013 18:41:02 +0200 Subject: [PATCH] Buffer: remove line_length and replace line_content with operator[] --- src/buffer.cc | 8 +------- src/buffer.hh | 4 ++-- src/editor.cc | 10 +++++----- src/file.cc | 2 +- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index ae98755b..b9ef6a96 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -91,19 +91,13 @@ BufferIterator Buffer::iterator_at(const BufferCoord& coord) const return is_end(coord) ? end() : BufferIterator(*this, clamp(coord)); } -ByteCount Buffer::line_length(LineCount line) const -{ - kak_assert(line < line_count()); - return m_lines[line].length(); -} - BufferCoord Buffer::clamp(BufferCoord coord) const { if (m_lines.empty()) return BufferCoord{}; coord.line = Kakoune::clamp(coord.line, 0_line, line_count() - 1); - ByteCount max_col = std::max(0_byte, line_length(coord.line) - 1); + ByteCount max_col = std::max(0_byte, m_lines[coord.line].length() - 1); coord.column = Kakoune::clamp(coord.column, 0_byte, max_col); return coord; } diff --git a/src/buffer.hh b/src/buffer.hh index 7aacb841..4775f44b 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -134,8 +134,8 @@ public: BufferIterator end() const; ByteCount byte_count() const; LineCount line_count() const; - ByteCount line_length(LineCount line) const; - const String& line_content(LineCount line) const + + const String& operator[](LineCount line) const { return m_lines[line].content; } // returns an iterator at given coordinates. clamp line_and_column diff --git a/src/editor.cc b/src/editor.cc index 7e5c0389..fbd2ae58 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -23,7 +23,7 @@ Editor::Editor(Buffer& buffer) static void avoid_eol(const Buffer& buffer, BufferCoord& coord) { const auto column = coord.column; - if (column != 0 and column == buffer.line_length(coord.line) - 1) + if (column != 0 and column == buffer[coord.line].length() - 1) coord = buffer.char_prev(coord); } @@ -60,7 +60,7 @@ static BufferCoord prepare_insert(Buffer& buffer, const Selection& sel, { // special case for end of lines, append to current line instead auto pos = std::max(sel.first(), sel.last()); - if (pos.column == buffer.line_length(pos.line) - 1) + if (pos.column == buffer[pos.line].length() - 1) return pos; else return buffer.char_next(pos); @@ -201,7 +201,7 @@ void Editor::move_selections(LineCount offset, SelectMode mode) { CharCount column = m_buffer->char_distance(sel.last().line, sel.last()); auto line = clamp(sel.last().line + offset, 0_line, m_buffer->line_count()-1); - column = std::min(column, m_buffer->line_content(line).char_length()-1); + column = std::min(column, (*m_buffer)[line].char_length()-1); BufferCoord last = m_buffer->char_advance(line, column); sel.first() = mode == SelectMode::Extend ? sel.first() : last; sel.last() = last; @@ -215,7 +215,7 @@ void Editor::clear_selections() auto& sel = m_selections[m_main_sel]; auto& pos = sel.last(); - if (pos.column != 0 and pos.column == m_buffer->line_length(pos.line) - 1) + if (pos.column != 0 and pos.column == (*m_buffer)[pos.line].length() - 1) pos = m_buffer->char_prev(pos); sel.first() = pos; @@ -471,7 +471,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode) first = sel.min(); last = sel.max(); // special case for end of lines, append to current line instead - if (last.column != buffer.line_length(last.line) - 1) + if (last.column != buffer[last.line].length() - 1) last = buffer.char_next(last); break; } diff --git a/src/file.cc b/src/file.cc index b4c793ed..d0ed0404 100644 --- a/src/file.cc +++ b/src/file.cc @@ -221,7 +221,7 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename) { // end of lines are written according to eolformat but always // stored as \n - memoryview linedata = buffer.line_content(i).data(); + memoryview linedata = buffer[i].data(); write(fd, linedata.subrange(0, linedata.size()-1), filename); write(fd, eoldata, filename); }