From b24dec89a24241825b36e0a9c6b3b85961fe6587 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 28 Nov 2011 19:31:29 +0000 Subject: [PATCH] Buffer: add iterator_at_line_{begin,end} methods --- src/buffer.cc | 11 +++++++++++ src/buffer.hh | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/buffer.cc b/src/buffer.cc index 81e63b95..0587cf45 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -96,6 +96,17 @@ BufferCoord Buffer::clamp(const BufferCoord& line_and_column) const return result; } +BufferIterator Buffer::iterator_at_line_begin(const BufferIterator& iterator) const +{ + return BufferIterator(*this, m_lines[line_at(iterator)]); +} + +BufferIterator Buffer::iterator_at_line_end(const BufferIterator& iterator) const +{ + BufferPos line = line_at(iterator) + 1; + return line < m_lines.size() ? BufferIterator(*this, m_lines[line]) : end(); +} + void Buffer::compute_lines() { m_lines.clear(); diff --git a/src/buffer.hh b/src/buffer.hh index 35572a2d..5a336f23 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -149,6 +149,15 @@ public: void register_modification_listener(BufferModificationListener* listener); void unregister_modification_listener(BufferModificationListener* listener); + // returns an iterator pointing to the first character of the line + // iterator is on + BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const; + + // returns an iterator pointing to the character after the last of the + // line iterator is on (which is the first of the next line if iterator is + // not on the last one) + BufferIterator iterator_at_line_end(const BufferIterator& iterator) const; + private: BufferChar at(BufferPos position) const;