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.cc | |
| 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.cc')
| -rw-r--r-- | src/buffer.cc | 33 |
1 files changed, 10 insertions, 23 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 |
