From a07fde181ab6b3c52837fb280d3633c255f7208a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 23 May 2013 19:39:27 +0200 Subject: [PATCH] Add Buffer::{next,prev,at}(BufferCoord) methods and use them in iterators --- src/buffer.cc | 35 +++++++++++++++++++++++++++++++++++ src/buffer.hh | 14 +++++++++----- src/buffer_iterator.inl.hh | 23 +++-------------------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 7a654a1d..cefc8f9f 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -637,6 +637,35 @@ BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const return { LineCount{ (int)(it - m_lines.begin()) }, off - it->start }; } +BufferCoord Buffer::next(BufferCoord coord) const +{ + if (coord.column < m_lines[coord.line].length() - 1) + ++coord.column; + else if (coord.line == m_lines.size() - 1) + coord.column = m_lines.back().length(); + else + { + ++coord.line; + coord.column = 0; + } + return coord; +} + +BufferCoord Buffer::prev(BufferCoord coord) const +{ + if (coord.column == 0) + { + if (coord.line > 0) + { + --coord.line; + coord.column = m_lines[coord.line].length() - 1; + } + } + else + --coord.column; + return coord; +} + ByteCount Buffer::distance(const BufferCoord& begin, const BufferCoord& end) const { return offset(end) - offset(begin); @@ -662,4 +691,10 @@ bool Buffer::is_end(const BufferCoord& c) const (c.line == line_count() - 1 and c.column == m_lines.back().length()); } +char Buffer::at(const BufferCoord& c) const +{ + kak_assert(c.line < line_count() and c.column < m_lines[c.line].length()); + return m_lines[c.line].content[c.column]; +} + } diff --git a/src/buffer.hh b/src/buffer.hh index 4b8a3f43..52475ffb 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -123,11 +123,15 @@ public: String string(const BufferCoord& begin, const BufferCoord& end) const; - ByteCount offset(const BufferCoord& c) const; - ByteCount distance(const BufferCoord& begin, const BufferCoord& end) const; - BufferCoord advance(BufferCoord coord, ByteCount count) const; - bool is_valid(const BufferCoord& c) const; - bool is_end(const BufferCoord& c) const; + char at(const BufferCoord& c) const; + ByteCount offset(const BufferCoord& c) const; + ByteCount distance(const BufferCoord& begin, + const BufferCoord& end) const; + BufferCoord advance(BufferCoord coord, ByteCount count) const; + BufferCoord next(BufferCoord coord) const; + BufferCoord prev(BufferCoord coord) const; + bool is_valid(const BufferCoord& c) const; + bool is_end(const BufferCoord& c) const; BufferIterator begin() const; BufferIterator end() const; diff --git a/src/buffer_iterator.inl.hh b/src/buffer_iterator.inl.hh index e8372221..ac02f022 100644 --- a/src/buffer_iterator.inl.hh +++ b/src/buffer_iterator.inl.hh @@ -65,7 +65,7 @@ inline bool BufferIterator::operator>=(const BufferIterator& iterator) const inline char BufferIterator::operator*() const { - return m_buffer->m_lines[m_coord.line].content[m_coord.column]; + return m_buffer->at(m_coord); } inline ByteCount BufferIterator::offset() const @@ -103,30 +103,13 @@ inline BufferIterator& BufferIterator::operator-=(ByteCount size) inline BufferIterator& BufferIterator::operator++() { - if (m_coord.column < m_buffer->m_lines[m_coord.line].length() - 1) - ++m_coord.column; - else if (m_coord.line == m_buffer->m_lines.size() - 1) - m_coord.column = m_buffer->m_lines.back().length(); - else - { - ++m_coord.line; - m_coord.column = 0; - } + m_coord = m_buffer->next(m_coord); return *this; } inline BufferIterator& BufferIterator::operator--() { - if (column() == 0) - { - if (line() > 0) - { - --m_coord.line; - m_coord.column = m_buffer->m_lines[m_coord.line].length() - 1; - } - } - else - --m_coord.column; + m_coord = m_buffer->prev(m_coord); return *this; }