summaryrefslogtreecommitdiff
path: root/src/buffer.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-05-23 19:39:27 +0200
committerMaxime Coste <frrrwww@gmail.com>2013-05-30 13:59:38 +0200
commita07fde181ab6b3c52837fb280d3633c255f7208a (patch)
tree9721faf3b51253cf51842e80878f46f766f2f9cd /src/buffer.cc
parent9a80a58ff45449c589669a6a5ae0887cd83434f5 (diff)
Add Buffer::{next,prev,at}(BufferCoord) methods and use them in iterators
Diffstat (limited to 'src/buffer.cc')
-rw-r--r--src/buffer.cc35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/buffer.cc b/src/buffer.cc
index 7a654a1d..cefc8f9f 100644
--- a/src/buffer.cc
+++ b/src/buffer.cc
@@ -637,6 +637,35 @@ BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const
return { LineCount{ (int)(it - m_lines.begin()) }, off - it->start };
}
+BufferCoord Buffer::next(BufferCoord coord) const
+{
+ if (coord.column < m_lines[coord.line].length() - 1)
+ ++coord.column;
+ else if (coord.line == m_lines.size() - 1)
+ coord.column = m_lines.back().length();
+ else
+ {
+ ++coord.line;
+ coord.column = 0;
+ }
+ return coord;
+}
+
+BufferCoord Buffer::prev(BufferCoord coord) const
+{
+ if (coord.column == 0)
+ {
+ if (coord.line > 0)
+ {
+ --coord.line;
+ coord.column = m_lines[coord.line].length() - 1;
+ }
+ }
+ else
+ --coord.column;
+ return coord;
+}
+
ByteCount Buffer::distance(const BufferCoord& begin, const BufferCoord& end) const
{
return offset(end) - offset(begin);
@@ -662,4 +691,10 @@ bool Buffer::is_end(const BufferCoord& c) const
(c.line == line_count() - 1 and c.column == m_lines.back().length());
}
+char Buffer::at(const BufferCoord& c) const
+{
+ kak_assert(c.line < line_count() and c.column < m_lines[c.line].length());
+ return m_lines[c.line].content[c.column];
+}
+
}