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.
This commit is contained in:
parent
250886a9e1
commit
208b28cbe7
|
@ -639,36 +639,23 @@ BufferCoord Buffer::char_next(BufferCoord coord) const
|
||||||
if (coord.column < m_lines[coord.line].length() - 1)
|
if (coord.column < m_lines[coord.line].length() - 1)
|
||||||
{
|
{
|
||||||
auto line = m_lines[coord.line];
|
auto line = m_lines[coord.line];
|
||||||
coord.column += utf8::codepoint_size(line[coord.column]);
|
auto column = coord.column + utf8::codepoint_size(line[coord.column]);
|
||||||
// Handle invalid utf-8
|
if (column >= line.length()) // Handle invalid utf-8
|
||||||
if (coord.column >= line.length())
|
return { coord.line + 1, 0 };
|
||||||
{
|
return { coord.line, column };
|
||||||
++coord.line;
|
|
||||||
coord.column = 0;
|
|
||||||
}
|
}
|
||||||
}
|
return { coord.line + 1, 0 };
|
||||||
else
|
|
||||||
{
|
|
||||||
++coord.line;
|
|
||||||
coord.column = 0;
|
|
||||||
}
|
|
||||||
return coord;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferCoord Buffer::char_prev(BufferCoord coord) const
|
BufferCoord Buffer::char_prev(BufferCoord coord) const
|
||||||
{
|
{
|
||||||
kak_assert(is_valid(coord));
|
kak_assert(is_valid(coord));
|
||||||
if (coord.column == 0)
|
if (coord.column == 0)
|
||||||
{
|
return { coord.line - 1, m_lines[coord.line - 1].length() - 1 };
|
||||||
if (coord.line > 0)
|
|
||||||
coord.column = m_lines[--coord.line].length() - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto line = m_lines[coord.line];
|
auto line = m_lines[coord.line];
|
||||||
coord.column = (int)(utf8::character_start(line.begin() + (int)coord.column - 1, line.begin()) - line.begin());
|
auto column = (int)(utf8::character_start(line.begin() + (int)coord.column - 1, line.begin()) - line.begin());
|
||||||
}
|
return { coord.line, column };
|
||||||
return coord;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timespec Buffer::fs_timestamp() const
|
timespec Buffer::fs_timestamp() const
|
||||||
|
|
|
@ -16,25 +16,15 @@ inline const char& Buffer::byte_at(BufferCoord c) const
|
||||||
inline BufferCoord Buffer::next(BufferCoord coord) const
|
inline BufferCoord Buffer::next(BufferCoord coord) const
|
||||||
{
|
{
|
||||||
if (coord.column < m_lines[coord.line].length() - 1)
|
if (coord.column < m_lines[coord.line].length() - 1)
|
||||||
++coord.column;
|
return {coord.line, coord.column + 1};
|
||||||
else
|
return { coord.line + 1, 0 };
|
||||||
{
|
|
||||||
++coord.line;
|
|
||||||
coord.column = 0;
|
|
||||||
}
|
|
||||||
return coord;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline BufferCoord Buffer::prev(BufferCoord coord) const
|
inline BufferCoord Buffer::prev(BufferCoord coord) const
|
||||||
{
|
{
|
||||||
if (coord.column == 0)
|
if (coord.column == 0)
|
||||||
{
|
return { coord.line - 1, m_lines[coord.line - 1].length() - 1 };
|
||||||
if (coord.line > 0)
|
return { coord.line, coord.column - 1 };
|
||||||
coord.column = m_lines[--coord.line].length() - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
--coord.column;
|
|
||||||
return coord;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline ByteCount Buffer::distance(BufferCoord begin, BufferCoord end) const
|
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
|
inline bool Buffer::is_valid(BufferCoord c) const
|
||||||
{
|
{
|
||||||
if (c.line < 0 or c.column < 0)
|
return (c.line >= 0 and c.column >= 0) and
|
||||||
return false;
|
((c.line < line_count() and c.column < m_lines[c.line].length()) or
|
||||||
|
(c.line == line_count() and c.column == 0));
|
||||||
return (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
|
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
|
inline BufferIterator Buffer::begin() const
|
||||||
{
|
{
|
||||||
return {*this, { 0_line, 0 }};
|
return {*this, { 0, 0 }};
|
||||||
}
|
}
|
||||||
|
|
||||||
inline BufferIterator Buffer::end() const
|
inline BufferIterator Buffer::end() const
|
||||||
|
@ -78,7 +66,7 @@ inline BufferIterator Buffer::end() const
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
inline LineCount Buffer::line_count() const
|
inline LineCount Buffer::line_count() const
|
||||||
{
|
{
|
||||||
return LineCount(m_lines.size());
|
return LineCount{(int)m_lines.size()};
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t Buffer::timestamp() const
|
inline size_t Buffer::timestamp() const
|
||||||
|
|
Loading…
Reference in New Issue
Block a user