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