From 4ab5c7a203e3d89773e316756a188617babc96c1 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 2 May 2013 18:57:15 +0200 Subject: [PATCH] DisplayBuffer: optimize Text and ReplacedBufferRange as well --- src/display_buffer.cc | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 929fb9b4..2be1c8c3 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -19,26 +19,41 @@ DisplayLine::iterator DisplayLine::split(iterator it, BufferIterator pos) void DisplayLine::optimize() { - for (auto atom_it = m_atoms.begin(); atom_it != m_atoms.end(); ++atom_it) - { - decltype(atom_it) next_atom_it; - while ((next_atom_it = atom_it + 1) != m_atoms.end()) - { - auto& atom = *atom_it; - auto& next_atom = *next_atom_it; + if (m_atoms.empty()) + return; - if (atom.colors == next_atom.colors and - atom.attribute == next_atom.attribute and - atom.content.type() == AtomContent::BufferRange and - next_atom.content.type() == AtomContent::BufferRange and + auto atom_it = m_atoms.begin(); + auto next_atom_it = atom_it + 1; + while (next_atom_it != m_atoms.end()) + { + auto& atom = *atom_it; + auto& next_atom = *next_atom_it; + bool merged = false; + + if (atom.colors == next_atom.colors and + atom.attribute == next_atom.attribute and + atom.content.type() == next_atom.content.type()) + { + auto type = atom.content.type(); + if ((type == AtomContent::BufferRange or + type == AtomContent::ReplacedBufferRange) and next_atom.content.begin() == atom.content.end()) { atom.content.m_end = next_atom.content.end(); - atom_it = m_atoms.erase(next_atom_it) - 1; + if (type == AtomContent::ReplacedBufferRange) + atom.content.m_text += next_atom.content.m_text; + merged = true; + } + if (type == AtomContent::Text) + { + atom.content.m_text += next_atom.content.m_text; + merged = true; } - else - break; } + if (merged) + next_atom_it = m_atoms.erase(next_atom_it); + else + atom_it = next_atom_it++; } }