Buffer: remove line_length and replace line_content with operator[]

This commit is contained in:
Maxime Coste 2013-06-05 18:41:02 +02:00
parent 6b667bc803
commit 580749a91d
4 changed files with 9 additions and 15 deletions

View File

@ -91,19 +91,13 @@ BufferIterator Buffer::iterator_at(const BufferCoord& coord) const
return is_end(coord) ? end() : BufferIterator(*this, clamp(coord)); 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 BufferCoord Buffer::clamp(BufferCoord coord) const
{ {
if (m_lines.empty()) if (m_lines.empty())
return BufferCoord{}; return BufferCoord{};
coord.line = Kakoune::clamp(coord.line, 0_line, line_count() - 1); 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); coord.column = Kakoune::clamp(coord.column, 0_byte, max_col);
return coord; return coord;
} }

View File

@ -134,8 +134,8 @@ public:
BufferIterator end() const; BufferIterator end() const;
ByteCount byte_count() const; ByteCount byte_count() const;
LineCount line_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; } { return m_lines[line].content; }
// returns an iterator at given coordinates. clamp line_and_column // returns an iterator at given coordinates. clamp line_and_column

View File

@ -23,7 +23,7 @@ Editor::Editor(Buffer& buffer)
static void avoid_eol(const Buffer& buffer, BufferCoord& coord) static void avoid_eol(const Buffer& buffer, BufferCoord& coord)
{ {
const auto column = coord.column; 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); 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 // special case for end of lines, append to current line instead
auto pos = std::max(sel.first(), sel.last()); 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; return pos;
else else
return buffer.char_next(pos); 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()); 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); 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); BufferCoord last = m_buffer->char_advance(line, column);
sel.first() = mode == SelectMode::Extend ? sel.first() : last; sel.first() = mode == SelectMode::Extend ? sel.first() : last;
sel.last() = last; sel.last() = last;
@ -215,7 +215,7 @@ void Editor::clear_selections()
auto& sel = m_selections[m_main_sel]; auto& sel = m_selections[m_main_sel];
auto& pos = sel.last(); 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); pos = m_buffer->char_prev(pos);
sel.first() = pos; sel.first() = pos;
@ -471,7 +471,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
first = sel.min(); first = sel.min();
last = sel.max(); last = sel.max();
// special case for end of lines, append to current line instead // 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); last = buffer.char_next(last);
break; break;
} }

View File

@ -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 // end of lines are written according to eolformat but always
// stored as \n // stored as \n
memoryview<char> linedata = buffer.line_content(i).data(); memoryview<char> linedata = buffer[i].data();
write(fd, linedata.subrange(0, linedata.size()-1), filename); write(fd, linedata.subrange(0, linedata.size()-1), filename);
write(fd, eoldata, filename); write(fd, eoldata, filename);
} }