Add Buffer::{next,prev,at}(BufferCoord) methods and use them in iterators

This commit is contained in:
Maxime Coste 2013-05-23 19:39:27 +02:00
parent 9a80a58ff4
commit a07fde181a
3 changed files with 47 additions and 25 deletions

View File

@ -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];
}
}

View File

@ -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;

View File

@ -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;
}