summaryrefslogtreecommitdiff
path: root/src/buffer.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-05-22 18:59:55 +0200
committerMaxime Coste <frrrwww@gmail.com>2013-05-29 18:58:20 +0200
commitf23f48172f2c95b053977e6fbddab35c22a4a953 (patch)
treed227b33d54d1017ca3d0b5b064e02e3b2fd28d4d /src/buffer.cc
parent0be8566dd7b40ebd8d463e948c920accde174679 (diff)
Buffer: add some method to work directly with coord instead of iterators
Diffstat (limited to 'src/buffer.cc')
-rw-r--r--src/buffer.cc30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/buffer.cc b/src/buffer.cc
index 14507528..30da1305 100644
--- a/src/buffer.cc
+++ b/src/buffer.cc
@@ -95,9 +95,7 @@ BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column,
ByteCount Buffer::line_length(LineCount line) const
{
kak_assert(line < line_count());
- ByteCount end = (line < line_count() - 1) ?
- m_lines[line + 1].start : byte_count();
- return end - m_lines[line].start;
+ return m_lines[line].length();
}
BufferCoord Buffer::clamp(const BufferCoord& line_and_column,
@@ -630,6 +628,26 @@ void Buffer::notify_saved()
}
}
+BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const
+{
+ ByteCount off = Kakoune::clamp(offset(coord) + count, 0_byte, byte_count());
+ auto it = std::upper_bound(m_lines.begin(), m_lines.end(), off,
+ [](ByteCount s, const Line& l) { return s < l.start; }) - 1;
+ return { LineCount{ (int)(it - m_lines.begin()) }, off - it->start };
+}
+
+ByteCount Buffer::distance(const BufferCoord& begin, const BufferCoord& end) const
+{
+ return offset(end) - offset(begin);
+}
+
+ByteCount Buffer::offset(const BufferCoord& c) const
+{
+ if (c.line == line_count())
+ return m_lines.back().start + m_lines.back().length();
+ return m_lines[c.line].start + c.column;
+}
+
bool Buffer::is_valid(const BufferCoord& c) const
{
return (c.line < line_count() and c.column < m_lines[c.line].length()) or
@@ -637,4 +655,10 @@ bool Buffer::is_valid(const BufferCoord& c) const
(c.line == line_count() and c.column == 0);
}
+bool Buffer::is_end(const BufferCoord& c) const
+{
+ return (c.line == line_count() and c.column == 0) or
+ (c.line == line_count() - 1 and c.column == m_lines.back().length());
+}
+
}