2014-01-12 21:19:05 +04:00
|
|
|
#ifndef buffer_inl_h_INCLUDED
|
|
|
|
#define buffer_inl_h_INCLUDED
|
2011-10-18 02:05:06 +04:00
|
|
|
|
|
|
|
#include "assert.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline char Buffer::byte_at(ByteCoord c) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
|
|
|
kak_assert(c.line < line_count() and c.column < m_lines[c.line].length());
|
|
|
|
return m_lines[c.line].content[c.column];
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline ByteCoord Buffer::next(ByteCoord coord) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline ByteCoord Buffer::prev(ByteCoord coord) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
|
|
|
if (coord.column == 0)
|
|
|
|
{
|
|
|
|
if (coord.line > 0)
|
|
|
|
coord.column = m_lines[--coord.line].length() - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
--coord.column;
|
|
|
|
return coord;
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline ByteCount Buffer::distance(ByteCoord begin, ByteCoord end) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
|
|
|
return offset(end) - offset(begin);
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline ByteCount Buffer::offset(ByteCoord c) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
|
|
|
if (c.line == line_count())
|
|
|
|
return m_lines.back().start + m_lines.back().length();
|
|
|
|
return m_lines[c.line].start + c.column;
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline bool Buffer::is_valid(ByteCoord c) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
|
|
|
return (c.line < line_count() and c.column < m_lines[c.line].length()) or
|
|
|
|
(c.line == line_count() - 1 and c.column == m_lines.back().length()) or
|
|
|
|
(c.line == line_count() and c.column == 0);
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline bool Buffer::is_end(ByteCoord c) const
|
2014-01-12 21:19:05 +04:00
|
|
|
{
|
2014-05-07 22:51:01 +04:00
|
|
|
return c >= ByteCoord{line_count() - 1, m_lines.back().length()};
|
2014-01-12 21:19:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator Buffer::begin() const
|
|
|
|
{
|
|
|
|
return BufferIterator(*this, { 0_line, 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator Buffer::end() const
|
|
|
|
{
|
|
|
|
if (m_lines.empty())
|
|
|
|
return BufferIterator(*this, { 0_line, 0 });
|
2014-04-01 21:54:46 +04:00
|
|
|
return BufferIterator(*this, { line_count() - 1, m_lines.back().length() });
|
2014-01-12 21:19:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ByteCount Buffer::byte_count() const
|
|
|
|
{
|
|
|
|
if (m_lines.empty())
|
|
|
|
return 0;
|
|
|
|
return m_lines.back().start + m_lines.back().length();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LineCount Buffer::line_count() const
|
|
|
|
{
|
|
|
|
return LineCount(m_lines.size());
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:54:46 +04:00
|
|
|
inline size_t Buffer::timestamp() const
|
|
|
|
{
|
2014-05-11 15:20:59 +04:00
|
|
|
return m_changes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline memoryview<Buffer::Change> Buffer::changes_since(size_t timestamp) const
|
|
|
|
{
|
|
|
|
return { m_changes.data() + timestamp,
|
|
|
|
m_changes.data() + m_changes.size() };
|
2014-04-01 21:54:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t Buffer::line_timestamp(LineCount line) const
|
|
|
|
{
|
|
|
|
return m_lines[line].timestamp;
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline ByteCoord Buffer::back_coord() const
|
2014-04-01 21:54:46 +04:00
|
|
|
{
|
|
|
|
return { line_count() - 1, m_lines.back().length() - 1 };
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline ByteCoord Buffer::end_coord() const
|
2014-04-01 21:54:46 +04:00
|
|
|
{
|
|
|
|
return { line_count() - 1, m_lines.back().length() };
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
inline BufferIterator::BufferIterator(const Buffer& buffer, ByteCoord coord)
|
2012-03-30 15:37:18 +04:00
|
|
|
: m_buffer(&buffer), m_coord(coord)
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2013-06-04 20:56:28 +04:00
|
|
|
kak_assert(m_buffer and m_buffer->is_valid(m_coord));
|
2011-10-24 18:26:21 +04:00
|
|
|
}
|
|
|
|
|
2011-10-18 02:05:06 +04:00
|
|
|
inline bool BufferIterator::operator==(const BufferIterator& iterator) const
|
|
|
|
{
|
2013-04-09 16:28:03 +04:00
|
|
|
return (m_buffer == iterator.m_buffer and m_coord == iterator.m_coord);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator!=(const BufferIterator& iterator) const
|
|
|
|
{
|
2013-04-09 16:28:03 +04:00
|
|
|
return (m_buffer != iterator.m_buffer or m_coord != iterator.m_coord);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator<(const BufferIterator& iterator) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_buffer == iterator.m_buffer);
|
2012-03-30 15:37:18 +04:00
|
|
|
return (m_coord < iterator.m_coord);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator<=(const BufferIterator& iterator) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_buffer == iterator.m_buffer);
|
2012-03-30 15:37:18 +04:00
|
|
|
return (m_coord <= iterator.m_coord);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator>(const BufferIterator& iterator) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_buffer == iterator.m_buffer);
|
2012-03-30 15:37:18 +04:00
|
|
|
return (m_coord > iterator.m_coord);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BufferIterator::operator>=(const BufferIterator& iterator) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_buffer == iterator.m_buffer);
|
2012-03-30 15:37:18 +04:00
|
|
|
return (m_coord >= iterator.m_coord);
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:20:08 +04:00
|
|
|
inline char BufferIterator::operator*() const
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2013-06-03 20:56:48 +04:00
|
|
|
return m_buffer->byte_at(m_coord);
|
2012-03-30 15:37:18 +04:00
|
|
|
}
|
|
|
|
|
2013-05-30 16:05:05 +04:00
|
|
|
inline char BufferIterator::operator[](size_t n) const
|
|
|
|
{
|
2013-06-03 20:56:48 +04:00
|
|
|
return m_buffer->byte_at(m_buffer->advance(m_coord, n));
|
2013-05-30 16:05:05 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:56:35 +04:00
|
|
|
inline size_t BufferIterator::operator-(const BufferIterator& iterator) const
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_buffer == iterator.m_buffer);
|
2013-05-22 20:59:55 +04:00
|
|
|
return (size_t)(int)m_buffer->distance(iterator.m_coord, m_coord);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
inline BufferIterator BufferIterator::operator+(ByteCount size) const
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_buffer);
|
2013-05-22 20:59:55 +04:00
|
|
|
return { *m_buffer, m_buffer->advance(m_coord, size) };
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
inline BufferIterator BufferIterator::operator-(ByteCount size) const
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2013-05-22 20:59:55 +04:00
|
|
|
return { *m_buffer, m_buffer->advance(m_coord, -size) };
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
inline BufferIterator& BufferIterator::operator+=(ByteCount size)
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2012-03-30 15:37:18 +04:00
|
|
|
return *this = (*this + size);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
inline BufferIterator& BufferIterator::operator-=(ByteCount size)
|
2011-10-18 02:05:06 +04:00
|
|
|
{
|
2012-03-30 15:37:18 +04:00
|
|
|
return *this = (*this - size);
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator& BufferIterator::operator++()
|
|
|
|
{
|
2013-05-23 21:39:27 +04:00
|
|
|
m_coord = m_buffer->next(m_coord);
|
2012-03-31 19:21:14 +04:00
|
|
|
return *this;
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator& BufferIterator::operator--()
|
|
|
|
{
|
2013-05-23 21:39:27 +04:00
|
|
|
m_coord = m_buffer->prev(m_coord);
|
2012-03-31 19:21:14 +04:00
|
|
|
return *this;
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 16:09:06 +04:00
|
|
|
inline BufferIterator BufferIterator::operator++(int)
|
|
|
|
{
|
|
|
|
BufferIterator save = *this;
|
|
|
|
++*this;
|
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline BufferIterator BufferIterator::operator--(int)
|
|
|
|
{
|
|
|
|
BufferIterator save = *this;
|
|
|
|
--*this;
|
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
2011-10-18 02:05:06 +04:00
|
|
|
}
|
2014-01-12 21:19:05 +04:00
|
|
|
#endif // buffer_inl_h_INCLUDED
|