kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

buffer.inl.hh (5647B)


      1 #ifndef buffer_inl_h_INCLUDED
      2 #define buffer_inl_h_INCLUDED
      3 
      4 #include "assert.hh"
      5 
      6 namespace Kakoune
      7 {
      8 
      9 [[gnu::always_inline]]
     10 inline const char& Buffer::byte_at(BufferCoord c) const
     11 {
     12     kak_assert(c.line < line_count() and c.column < m_lines[c.line].length());
     13     return m_lines[c.line][c.column];
     14 }
     15 
     16 inline BufferCoord Buffer::next(BufferCoord coord) const
     17 {
     18     if (coord.column < m_lines[coord.line].length() - 1)
     19         return {coord.line, coord.column + 1};
     20     return { coord.line + 1, 0 };
     21 }
     22 
     23 inline BufferCoord Buffer::prev(BufferCoord coord) const
     24 {
     25     if (coord.column == 0)
     26         return { coord.line - 1, m_lines[coord.line - 1].length() - 1 };
     27     return { coord.line, coord.column - 1 };
     28 }
     29 
     30 inline ByteCount Buffer::distance(ArrayView<const StringDataPtr> lines, BufferCoord begin, BufferCoord end)
     31 {
     32     if (begin > end)
     33         return -distance(lines, end, begin);
     34     if (begin.line == end.line)
     35         return end.column - begin.column;
     36 
     37     ByteCount res = lines[(size_t)begin.line]->length - begin.column;
     38     for (LineCount l = begin.line+1; l < end.line; ++l)
     39         res += lines[(size_t)l]->length;
     40     res += end.column;
     41     return res;
     42 }
     43 
     44 inline bool Buffer::is_valid(BufferCoord c) const
     45 {
     46     return (c.line >= 0 and c.column >= 0) and
     47            ((c.line < line_count() and c.column < m_lines[c.line].length()) or
     48             (c.line == line_count() and c.column == 0));
     49 }
     50 
     51 inline bool Buffer::is_end(BufferCoord c) const
     52 {
     53     return c >= end_coord();
     54 }
     55 
     56 inline BufferIterator Buffer::begin() const
     57 {
     58     return {*this, { 0, 0 }};
     59 }
     60 
     61 inline BufferIterator Buffer::end() const
     62 {
     63     return {*this, end_coord()};
     64 }
     65 
     66 [[gnu::always_inline]]
     67 inline LineCount Buffer::line_count() const
     68 {
     69     return LineCount{(int)m_lines.size()};
     70 }
     71 
     72 inline size_t Buffer::timestamp() const
     73 {
     74     return m_changes.size();
     75 }
     76 
     77 inline StringView Buffer::substr(BufferCoord begin, BufferCoord end) const
     78 {
     79     kak_assert(begin.line == end.line);
     80     return m_lines[begin.line].substr(begin.column, end.column - begin.column);
     81 }
     82 
     83 inline ConstArrayView<Buffer::Change> Buffer::changes_since(size_t timestamp) const
     84 {
     85     if (timestamp < m_changes.size())
     86         return { m_changes.data() + timestamp,
     87                  m_changes.data() + m_changes.size() };
     88     return {};
     89 }
     90 
     91 inline BufferCoord Buffer::back_coord() const
     92 {
     93     return { line_count() - 1, m_lines.back().length() - 1 };
     94 }
     95 
     96 inline BufferCoord Buffer::end_coord() const
     97 {
     98     return line_count();
     99 }
    100 
    101 inline BufferIterator::BufferIterator(const Buffer& buffer, BufferCoord coord) noexcept
    102     : BufferIterator{buffer.m_lines.data(), buffer.line_count(), coord} {}
    103 
    104 inline BufferIterator::BufferIterator(const StringDataPtr* lines, LineCount line_count, BufferCoord coord) noexcept
    105     : m_lines{lines},
    106       m_line{coord.line < line_count ? m_lines[(size_t)coord.line]->strview() : StringView{}},
    107       m_line_count{line_count},
    108       m_coord{coord} {}
    109 
    110 inline bool BufferIterator::operator==(const BufferIterator& iterator) const noexcept
    111 {
    112     kak_assert(m_lines == iterator.m_lines);
    113     return m_coord == iterator.m_coord;
    114 }
    115 
    116 inline auto BufferIterator::operator<=>(const BufferIterator& iterator) const noexcept
    117 {
    118     kak_assert(m_lines == iterator.m_lines);
    119     return (m_coord <=> iterator.m_coord);
    120 }
    121 
    122 inline bool BufferIterator::operator==(const BufferCoord& coord) const noexcept
    123 {
    124     return m_coord == coord;
    125 }
    126 
    127 [[gnu::always_inline]]
    128 inline const char& BufferIterator::operator*() const noexcept
    129 {
    130     return m_line[m_coord.column];
    131 }
    132 
    133 inline const char& BufferIterator::operator[](size_t n) const noexcept
    134 {
    135     auto coord = Buffer::advance({m_lines, (size_t)(int)m_line_count}, m_coord, n);
    136     return m_lines[(size_t)coord.line]->strview()[coord.column];
    137 }
    138 
    139 inline size_t BufferIterator::operator-(const BufferIterator& iterator) const
    140 {
    141     kak_assert(m_lines == iterator.m_lines);
    142     return (size_t)Buffer::distance({m_lines, (size_t)(int)m_line_count}, iterator.m_coord, m_coord);
    143 }
    144 
    145 inline BufferIterator BufferIterator::operator+(ByteCount size) const
    146 {
    147     kak_assert(*this);
    148     return { m_lines, m_line_count, Buffer::advance({m_lines, (size_t)(int)m_line_count}, m_coord, size) };
    149 }
    150 
    151 inline BufferIterator BufferIterator::operator-(ByteCount size) const
    152 {
    153     return { m_lines, m_line_count, Buffer::advance({m_lines, (size_t)(int)m_line_count}, m_coord, -size) };
    154 }
    155 
    156 inline BufferIterator& BufferIterator::operator+=(ByteCount size)
    157 {
    158     m_coord = Buffer::advance({m_lines, (size_t)(int)m_line_count}, m_coord, size);
    159     m_line = m_lines[(size_t)m_coord.line]->strview();
    160     return *this;
    161 }
    162 
    163 inline BufferIterator& BufferIterator::operator-=(ByteCount size)
    164 {
    165     m_coord = Buffer::advance({m_lines, (size_t)(int)m_line_count}, m_coord, -size);
    166     m_line = m_lines[(size_t)m_coord.line]->strview();
    167     return *this;
    168 }
    169 
    170 inline BufferIterator& BufferIterator::operator++()
    171 {
    172     if (++m_coord.column == m_line.length())
    173     {
    174         m_line = ((size_t)++m_coord.line < m_line_count) ?
    175             m_lines[(size_t)m_coord.line]->strview() : StringView{};
    176         m_coord.column = 0;
    177     }
    178     return *this;
    179 }
    180 
    181 inline BufferIterator& BufferIterator::operator--()
    182 {
    183     if (m_coord.column == 0)
    184     {
    185         m_line = m_lines[(size_t)--m_coord.line]->strview();
    186         m_coord.column = m_line.length() - 1;
    187     }
    188     else
    189        --m_coord.column;
    190     return *this;
    191 }
    192 
    193 inline BufferIterator BufferIterator::operator++(int)
    194 {
    195     BufferIterator save = *this;
    196     ++*this;
    197     return save;
    198 }
    199 
    200 inline BufferIterator BufferIterator::operator--(int)
    201 {
    202     BufferIterator save = *this;
    203     --*this;
    204     return save;
    205 }
    206 
    207 }
    208 #endif // buffer_inl_h_INCLUDED