2011-10-18 00:05:06 +02:00
|
|
|
#ifndef buffer_iterator_inl_h_INCLUDED
|
|
|
|
#define buffer_iterator_inl_h_INCLUDED
|
|
|
|
|
|
|
|
#include "assert.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-03-30 13:37:18 +02:00
|
|
|
inline BufferIterator::BufferIterator(const Buffer& buffer, BufferCoord coord)
|
|
|
|
: m_buffer(&buffer), m_coord(coord)
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
2012-03-30 13:37:18 +02:00
|
|
|
assert(is_valid());
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const Buffer& BufferIterator::buffer() const
|
|
|
|
{
|
|
|
|
assert(m_buffer);
|
|
|
|
return *m_buffer;
|
|
|
|
}
|
|
|
|
|
2011-10-24 16:26:21 +02:00
|
|
|
inline bool BufferIterator::is_valid() const
|
|
|
|
{
|
2012-03-30 13:37:18 +02:00
|
|
|
return m_buffer and
|
|
|
|
((line() < m_buffer->line_count() and
|
2012-03-30 14:00:40 +02:00
|
|
|
column() < m_buffer->m_lines[line()].length()) or
|
2012-03-30 13:37:18 +02:00
|
|
|
((line() == m_buffer->line_count() and column() == 0)) or
|
|
|
|
(line() == m_buffer->line_count() - 1 and
|
2012-03-30 14:00:40 +02:00
|
|
|
column() == m_buffer->m_lines.back().length()));
|
2011-10-24 16:26:21 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 18:18:12 +02:00
|
|
|
inline void BufferIterator::clamp(bool avoid_eol)
|
|
|
|
{
|
|
|
|
assert(m_buffer);
|
|
|
|
m_coord = m_buffer->clamp(m_coord, avoid_eol);
|
|
|
|
}
|
|
|
|
|
2011-10-18 00:05:06 +02:00
|
|
|
inline bool BufferIterator::operator==(const BufferIterator& iterator) const
|
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return (m_coord == iterator.m_coord);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator!=(const BufferIterator& iterator) const
|
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return (m_coord != iterator.m_coord);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator<(const BufferIterator& iterator) const
|
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return (m_coord < iterator.m_coord);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator<=(const BufferIterator& iterator) const
|
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return (m_coord <= iterator.m_coord);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator>(const BufferIterator& iterator) const
|
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return (m_coord > iterator.m_coord);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator>=(const BufferIterator& iterator) const
|
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return (m_coord >= iterator.m_coord);
|
|
|
|
}
|
|
|
|
|
2012-04-04 15:56:19 +02:00
|
|
|
inline void BufferIterator::on_insert(const BufferCoord& begin,
|
|
|
|
const BufferCoord& end)
|
2012-03-30 13:37:18 +02:00
|
|
|
{
|
2012-04-04 15:56:19 +02:00
|
|
|
if (m_coord < begin)
|
|
|
|
return;
|
2012-03-30 13:37:18 +02:00
|
|
|
|
2012-04-04 15:56:19 +02:00
|
|
|
if (begin.line == line())
|
|
|
|
m_coord.column = end.column + m_coord.column - begin.column;
|
|
|
|
m_coord.line += end.line - begin.line;
|
|
|
|
|
|
|
|
assert(is_valid());
|
2012-03-30 13:37:18 +02:00
|
|
|
}
|
|
|
|
|
2012-04-04 15:56:19 +02:00
|
|
|
inline void BufferIterator::on_erase(const BufferCoord& begin,
|
|
|
|
const BufferCoord& end)
|
2012-03-30 13:37:18 +02:00
|
|
|
{
|
2012-04-04 15:56:19 +02:00
|
|
|
if (m_coord < begin)
|
2012-03-30 13:37:18 +02:00
|
|
|
return;
|
|
|
|
|
2012-04-04 15:56:19 +02:00
|
|
|
if (m_coord <= end)
|
|
|
|
m_coord = begin;
|
2012-03-30 13:37:18 +02:00
|
|
|
else
|
|
|
|
{
|
2012-04-04 15:56:19 +02:00
|
|
|
if (end.line == m_coord.line)
|
2012-06-28 14:26:01 +02:00
|
|
|
{
|
|
|
|
m_coord.line = begin.line;
|
|
|
|
m_coord.column = begin.column + m_coord.column - end.column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_coord.line -= end.line - begin.line;
|
2012-03-30 13:37:18 +02:00
|
|
|
}
|
2012-04-04 15:56:19 +02:00
|
|
|
|
|
|
|
if (is_end())
|
|
|
|
operator--();
|
2012-03-30 13:37:18 +02:00
|
|
|
assert(is_valid());
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2012-04-04 15:56:19 +02:00
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
inline Character BufferIterator::operator*() const
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
|
|
|
assert(m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return m_buffer->m_lines[line()].content[column()];
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
inline CharCount BufferIterator::offset() const
|
2012-03-30 13:37:18 +02:00
|
|
|
{
|
|
|
|
assert(m_buffer);
|
2012-08-22 23:33:52 +02:00
|
|
|
return line() == 0 ? column()
|
2012-08-23 23:56:35 +02:00
|
|
|
: m_buffer->m_lines[line()].start + column();
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
inline size_t BufferIterator::operator-(const BufferIterator& iterator) const
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
|
|
|
assert(m_buffer == iterator.m_buffer);
|
2012-08-23 23:56:35 +02:00
|
|
|
return (size_t)(int)(offset() - iterator.offset());
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
inline BufferIterator BufferIterator::operator+(CharCount size) const
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
|
|
|
assert(m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
if (size >= 0)
|
|
|
|
{
|
2012-08-23 23:56:35 +02:00
|
|
|
CharCount o = std::min(m_buffer->character_count(), offset() + size);
|
2012-08-22 23:33:52 +02:00
|
|
|
for (LineCount i = line() + 1; i < m_buffer->line_count(); ++i)
|
2012-03-30 13:37:18 +02:00
|
|
|
{
|
|
|
|
if (m_buffer->m_lines[i].start > o)
|
|
|
|
return BufferIterator(*m_buffer, { i-1, o - m_buffer->m_lines[i-1].start });
|
|
|
|
}
|
2012-08-22 23:33:52 +02:00
|
|
|
LineCount last_line = std::max(0_line, m_buffer->line_count() - 1);
|
2012-03-30 13:37:18 +02:00
|
|
|
return BufferIterator(*m_buffer, { last_line, o - m_buffer->m_lines[last_line].start });
|
|
|
|
}
|
|
|
|
return operator-(-size);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
inline BufferIterator BufferIterator::operator-(CharCount size) const
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
|
|
|
assert(m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
if (size >= 0)
|
|
|
|
{
|
2012-08-23 23:56:35 +02:00
|
|
|
CharCount o = std::max(0_char, offset() - size);
|
2012-08-22 23:33:52 +02:00
|
|
|
for (LineCount i = line(); i >= 0; --i)
|
2012-03-30 13:37:18 +02:00
|
|
|
{
|
|
|
|
if (m_buffer->m_lines[i].start <= o)
|
|
|
|
return BufferIterator(*m_buffer, { i, o - m_buffer->m_lines[i].start });
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
return operator+(-size);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
inline BufferIterator& BufferIterator::operator+=(CharCount size)
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
2012-03-30 13:37:18 +02:00
|
|
|
return *this = (*this + size);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
inline BufferIterator& BufferIterator::operator-=(CharCount size)
|
2011-10-18 00:05:06 +02:00
|
|
|
{
|
2012-03-30 13:37:18 +02:00
|
|
|
return *this = (*this - size);
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator& BufferIterator::operator++()
|
|
|
|
{
|
2012-03-30 14:01:18 +02:00
|
|
|
if (column() < m_buffer->m_lines[line()].length() - 1)
|
|
|
|
++m_coord.column;
|
|
|
|
else if (line() == m_buffer->line_count() - 1)
|
|
|
|
m_coord.column = m_buffer->m_lines.back().length();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++m_coord.line;
|
|
|
|
m_coord.column = 0;
|
|
|
|
}
|
2012-03-31 17:21:14 +02:00
|
|
|
return *this;
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator& BufferIterator::operator--()
|
|
|
|
{
|
2012-03-31 17:21:14 +02:00
|
|
|
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;
|
|
|
|
return *this;
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::is_begin() const
|
|
|
|
{
|
|
|
|
assert(m_buffer);
|
2012-03-30 13:37:18 +02:00
|
|
|
return m_coord.line == 0 and m_coord.column == 0;
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::is_end() const
|
|
|
|
{
|
|
|
|
assert(m_buffer);
|
2012-06-25 19:05:32 +02:00
|
|
|
return offset() == m_buffer->character_count();
|
2011-10-18 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // buffer_iterator_inl_h_INCLUDED
|