summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2017-06-12 06:10:18 +0100
committerMaxime Coste <mawww@kakoune.org>2017-06-12 06:10:18 +0100
commit208b28cbe703176ea30dcfecd870b7cbce7c3bd7 (patch)
tree7ecdc21b0d1be7149e593589c2207909556da9f9 /src
parent250886a9e1dce0526e1d1dc20743ecd15907f63f (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')
-rw-r--r--src/buffer.cc33
-rw-r--r--src/buffer.inl.hh30
2 files changed, 19 insertions, 44 deletions
diff --git a/src/buffer.cc b/src/buffer.cc
index dc0dd93b..d7882b63 100644
--- a/src/buffer.cc
+++ b/src/buffer.cc
@@ -639,36 +639,23 @@ BufferCoord Buffer::char_next(BufferCoord coord) const
if (coord.column < m_lines[coord.line].length() - 1)
{
auto line = m_lines[coord.line];
- coord.column += utf8::codepoint_size(line[coord.column]);
- // Handle invalid utf-8
- if (coord.column >= line.length())
- {
- ++coord.line;
- coord.column = 0;
- }
+ auto column = coord.column + utf8::codepoint_size(line[coord.column]);
+ if (column >= line.length()) // Handle invalid utf-8
+ return { coord.line + 1, 0 };
+ return { coord.line, column };
}
- else
- {
- ++coord.line;
- coord.column = 0;
- }
- return coord;
+ return { coord.line + 1, 0 };
}
BufferCoord Buffer::char_prev(BufferCoord coord) const
{
kak_assert(is_valid(coord));
if (coord.column == 0)
- {
- if (coord.line > 0)
- coord.column = m_lines[--coord.line].length() - 1;
- }
- else
- {
- auto line = m_lines[coord.line];
- coord.column = (int)(utf8::character_start(line.begin() + (int)coord.column - 1, line.begin()) - line.begin());
- }
- return coord;
+ return { coord.line - 1, m_lines[coord.line - 1].length() - 1 };
+
+ auto line = m_lines[coord.line];
+ auto column = (int)(utf8::character_start(line.begin() + (int)coord.column - 1, line.begin()) - line.begin());
+ return { coord.line, column };
}
timespec Buffer::fs_timestamp() const
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