Slight cleanup in Buffer::do_insert

This commit is contained in:
Maxime Coste 2016-03-15 23:06:49 +00:00
parent 5fe2872904
commit d2dfb9ecb1

View File

@ -344,10 +344,10 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
ByteCoord begin;
ByteCoord end;
bool at_end = false;
const bool at_end = is_end(pos);
// if we inserted at the end of the buffer, we have created a new
// line without inserting a '\n'
if (is_end(pos))
if (at_end)
{
ByteCount start = 0;
for (ByteCount i = 0; i < content.length(); ++i)
@ -363,7 +363,6 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
begin = pos.column == 0 ? pos : ByteCoord{ pos.line + 1, 0 };
end = ByteCoord{ line_count(), 0 };
at_end = true;
}
else
{
@ -377,20 +376,15 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
{
if (content[i] == '\n')
{
StringView line_content = content.substr(start, i + 1 - start);
if (start == 0)
new_lines.emplace_back(StringData::create(prefix + line_content));
else
new_lines.push_back(StringData::create(line_content));
StringView line = content.substr(start, i + 1 - start);
new_lines.push_back(StringData::create(start == 0 ? prefix + line : line));
start = i + 1;
}
}
if (start == 0)
new_lines.emplace_back(StringData::create(prefix + content + suffix));
new_lines.push_back(StringData::create(prefix + content + suffix));
else if (start != content.length() or not suffix.empty())
new_lines.emplace_back(StringData::create(content.substr(start) + suffix));
LineCount last_line = pos.line + new_lines.size() - 1;
new_lines.push_back(StringData::create(content.substr(start) + suffix));
auto line_it = m_lines.begin() + (int)pos.line;
*line_it = std::move(*new_lines.begin());
@ -399,6 +393,7 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
std::make_move_iterator(new_lines.end()));
begin = pos;
const LineCount last_line = pos.line + new_lines.size() - 1;
end = ByteCoord{ last_line, m_lines[last_line].length() - suffix.length() };
}