fix some corner cases in Buffer modification

This commit is contained in:
Maxime Coste 2012-04-04 12:25:42 +00:00
parent 46c65fea10
commit 6cedff8fb2

View File

@ -191,6 +191,7 @@ void Buffer::check_invariant() const
for (auto& line : m_lines) for (auto& line : m_lines)
{ {
assert(line.start == start); assert(line.start == start);
assert(line.length() > 0);
start += line.length(); start += line.length();
} }
} }
@ -264,7 +265,8 @@ void Buffer::erase(const BufferIterator& pos, BufferSize length)
Line new_line = { m_lines[pos.line()].start, prefix + suffix }; Line new_line = { m_lines[pos.line()].start, prefix + suffix };
m_lines.erase(m_lines.begin() + pos.line(), m_lines.begin() + end.line() + 1); m_lines.erase(m_lines.begin() + pos.line(), m_lines.begin() + end.line() + 1);
m_lines.insert(m_lines.begin() + pos.line(), new_line); if (new_line.length())
m_lines.insert(m_lines.begin() + pos.line(), std::move(new_line));
for (size_t i = pos.line()+1; i < line_count(); ++i) for (size_t i = pos.line()+1; i < line_count(); ++i)
m_lines[i].start -= length; m_lines[i].start -= length;
@ -280,8 +282,12 @@ void Buffer::apply_modification(const Modification& modification)
switch (modification.type) switch (modification.type)
{ {
case Modification::Insert: case Modification::Insert:
insert(modification.position, modification.content); {
BufferIterator pos = modification.position < end() ?
modification.position : end();
insert(pos, modification.content);
break; break;
}
case Modification::Erase: case Modification::Erase:
{ {
size_t size = modification.content.size(); size_t size = modification.content.size();