diff options
| author | Maxime Coste <mawww@kakoune.org> | 2017-06-12 06:10:18 +0100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2017-06-12 06:10:18 +0100 |
| commit | 208b28cbe703176ea30dcfecd870b7cbce7c3bd7 (patch) | |
| tree | 7ecdc21b0d1be7149e593589c2207909556da9f9 /src/buffer.inl.hh | |
| parent | 250886a9e1dce0526e1d1dc20743ecd15907f63f (diff) | |
Simplify a bit buffer iteration functions
Dont try to ensure the returned value is valid, incrementing past
the end/decrementing before begin is the caller's error.
Diffstat (limited to 'src/buffer.inl.hh')
| -rw-r--r-- | src/buffer.inl.hh | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/src/buffer.inl.hh b/src/buffer.inl.hh index 5904b9ff..6816e818 100644 --- a/src/buffer.inl.hh +++ b/src/buffer.inl.hh @@ -16,25 +16,15 @@ inline const char& Buffer::byte_at(BufferCoord c) const inline BufferCoord Buffer::next(BufferCoord coord) const { if (coord.column < m_lines[coord.line].length() - 1) - ++coord.column; - else - { - ++coord.line; - coord.column = 0; - } - return coord; + return {coord.line, coord.column + 1}; + return { coord.line + 1, 0 }; } inline BufferCoord Buffer::prev(BufferCoord coord) const { if (coord.column == 0) - { - if (coord.line > 0) - coord.column = m_lines[--coord.line].length() - 1; - } - else - --coord.column; - return coord; + return { coord.line - 1, m_lines[coord.line - 1].length() - 1 }; + return { coord.line, coord.column - 1 }; } inline ByteCount Buffer::distance(BufferCoord begin, BufferCoord end) const @@ -53,11 +43,9 @@ inline ByteCount Buffer::distance(BufferCoord begin, BufferCoord end) const inline bool Buffer::is_valid(BufferCoord c) const { - if (c.line < 0 or c.column < 0) - return false; - - return (c.line < line_count() and c.column < m_lines[c.line].length()) or - (c.line == line_count() and c.column == 0); + return (c.line >= 0 and c.column >= 0) and + ((c.line < line_count() and c.column < m_lines[c.line].length()) or + (c.line == line_count() and c.column == 0)); } inline bool Buffer::is_end(BufferCoord c) const @@ -67,7 +55,7 @@ inline bool Buffer::is_end(BufferCoord c) const inline BufferIterator Buffer::begin() const { - return {*this, { 0_line, 0 }}; + return {*this, { 0, 0 }}; } inline BufferIterator Buffer::end() const @@ -78,7 +66,7 @@ inline BufferIterator Buffer::end() const [[gnu::always_inline]] inline LineCount Buffer::line_count() const { - return LineCount(m_lines.size()); + return LineCount{(int)m_lines.size()}; } inline size_t Buffer::timestamp() const |
