From d2dfb9ecb18cf36dfafc89571eb94a3152fe1752 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 15 Mar 2016 23:06:49 +0000 Subject: Slight cleanup in Buffer::do_insert --- src/buffer.cc | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/buffer.cc') diff --git a/src/buffer.cc b/src/buffer.cc index c0e25f0a..f56d5d2e 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -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() }; } -- cgit v1.2.3