From 0eaf39b72513cca363dd12a39d943a363f3a5280 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 21 Aug 2012 20:52:49 +0200 Subject: [PATCH] Add Buffer::iterator_at_line{begin,end}(size_t line) overloads --- src/buffer.cc | 12 ++++++++++++ src/buffer.hh | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/buffer.cc b/src/buffer.cc index 6bf82020..aeff8400 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -97,6 +97,11 @@ BufferIterator Buffer::iterator_at_line_begin(const BufferIterator& iterator) co return BufferIterator(*this, { iterator.line(), 0 }); } +BufferIterator Buffer::iterator_at_line_begin(size_t line) const +{ + return BufferIterator(*this, clamp({ (int)line, 0 })); +} + BufferIterator Buffer::iterator_at_line_end(const BufferIterator& iterator) const { BufferPos line = iterator.line(); @@ -104,6 +109,13 @@ BufferIterator Buffer::iterator_at_line_end(const BufferIterator& iterator) cons return ++BufferIterator(*this, { line, line_length(line) - 1 }); } +BufferIterator Buffer::iterator_at_line_end(size_t line) const +{ + line = std::min(line, (size_t)line_count()-1); + assert(line_length(line) > 0); + return ++BufferIterator(*this, { (int)line, line_length(line) - 1 }); +} + BufferIterator Buffer::begin() const { return BufferIterator(*this, { 0, 0 }); diff --git a/src/buffer.hh b/src/buffer.hh index 0c57f851..83bdcf22 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -161,11 +161,15 @@ public: // returns an iterator pointing to the first character of the line // iterator is on BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const; + // the same but taking a line number instead of an iterator + BufferIterator iterator_at_line_begin(size_t line) 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; + // the same but taking a line number instead of an iterator + BufferIterator iterator_at_line_end(size_t line) const; const String& line_content(size_t l) const { return m_lines[l].content; }