Add Buffer::iterator_at_line{begin,end}(size_t line) overloads

This commit is contained in:
Maxime Coste 2012-08-21 20:52:49 +02:00
parent 05098a373a
commit 0eaf39b725
2 changed files with 16 additions and 0 deletions

View File

@ -97,6 +97,11 @@ BufferIterator Buffer::iterator_at_line_begin(const BufferIterator& iterator) co
return BufferIterator(*this, { iterator.line(), 0 }); 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 BufferIterator Buffer::iterator_at_line_end(const BufferIterator& iterator) const
{ {
BufferPos line = iterator.line(); 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 }); 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 BufferIterator Buffer::begin() const
{ {
return BufferIterator(*this, { 0, 0 }); return BufferIterator(*this, { 0, 0 });

View File

@ -161,11 +161,15 @@ public:
// returns an iterator pointing to the first character of the line // returns an iterator pointing to the first character of the line
// iterator is on // iterator is on
BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const; 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 // 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 // line iterator is on (which is the first of the next line if iterator is
// not on the last one) // not on the last one)
BufferIterator iterator_at_line_end(const BufferIterator& iterator) const; 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; } const String& line_content(size_t l) const { return m_lines[l].content; }