From 10106e8c8e5cd6a5bd52c2068e3dd06d353a66b8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 7 Oct 2011 14:19:58 +0000 Subject: [PATCH] DisplayBuffer: do not store content in atom, begin and end are sufficient --- src/display_buffer.cc | 12 +++++------- src/display_buffer.hh | 7 ++----- src/filters.cc | 16 ++++++++-------- src/main.cc | 2 +- src/window.cc | 17 ++++++++--------- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 8a8eb09e..f36527be 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -9,17 +9,15 @@ DisplayBuffer::DisplayBuffer() { } -DisplayBuffer::iterator DisplayBuffer::split(iterator atom, size_t pos_in_atom) +DisplayBuffer::iterator DisplayBuffer::split(iterator atom, const BufferIterator& pos) { assert(atom < end()); - assert(pos_in_atom > 0); - assert(pos_in_atom < atom->content.length()); - DisplayAtom new_atom(atom->begin, atom->begin + pos_in_atom, - atom->content.substr(0, pos_in_atom), + assert(pos > atom->begin); + assert(pos < atom->end); + DisplayAtom new_atom(atom->begin, pos, atom->fg_color, atom->bg_color, atom->attribute); - atom->begin = atom->begin + pos_in_atom; - atom->content = atom->content.substr(pos_in_atom); + atom->begin = pos; return insert(atom, std::move(new_atom)); } diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 62b4731c..59468eea 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -35,7 +35,6 @@ enum class Color struct DisplayAtom { - std::string content; BufferIterator begin; BufferIterator end; Color fg_color; @@ -43,12 +42,10 @@ struct DisplayAtom Attribute attribute; DisplayAtom(BufferIterator begin, BufferIterator end, - const std::string& content, Color fg_color = Color::Default, Color bg_color = Color::Default, Attribute attribute = Attributes::Normal) - : content(content), - begin(begin), + : begin(begin), end(end), fg_color(fg_color), bg_color(bg_color), @@ -68,7 +65,7 @@ public: void clear() { m_atoms.clear(); } void append(const DisplayAtom& atom) { m_atoms.push_back(atom); } iterator insert(iterator where, const DisplayAtom& atom) { return m_atoms.insert(where, atom); } - iterator split(iterator atom, size_t pos_in_atom); + iterator split(iterator atom, const BufferIterator& pos); iterator begin() { return m_atoms.begin(); } iterator end() { return m_atoms.end(); } diff --git a/src/filters.cc b/src/filters.cc index 0ef15811..19f37105 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -9,16 +9,16 @@ void colorize_regex(DisplayBuffer& display_buffer, for (auto atom_it = display_buffer.begin(); atom_it != display_buffer.end(); ++atom_it) { - boost::smatch matches; - if (boost::regex_search(atom_it->content, matches, ex, boost::match_nosubs)) + boost::match_results matches; + if (boost::regex_search(atom_it->begin, atom_it->end, matches, ex, boost::match_nosubs)) { - size_t pos = matches.begin()->first - atom_it->content.begin(); - if (pos != 0) - atom_it = display_buffer.split(atom_it, pos) + 1; + const BufferIterator& begin = matches.begin()->first; + if (begin != atom_it->begin) + atom_it = display_buffer.split(atom_it, begin) + 1; - pos = matches.begin()->second - matches.begin()->first; - if (pos != atom_it->content.length()) - atom_it = display_buffer.split(atom_it, pos); + const BufferIterator& end = matches.begin()->second; + if (end != atom_it->end) + atom_it = display_buffer.split(atom_it, end); atom_it->fg_color = color; } diff --git a/src/main.cc b/src/main.cc index 32a13508..d190fbe6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -81,7 +81,7 @@ void draw_window(Window& window) WindowCoord position; for (const DisplayAtom& atom : window.display_buffer()) { - const std::string& content = atom.content; + const std::string content = window.buffer().string(atom.begin, atom.end); set_attribute(A_UNDERLINE, atom.attribute & Underline); set_attribute(A_REVERSE, atom.attribute & Reverse); diff --git a/src/window.cc b/src/window.cc index cd9554c9..169a8b09 100644 --- a/src/window.cc +++ b/src/window.cc @@ -62,8 +62,7 @@ public: // [###------] if (atom.begin >= sel.begin() and atom.begin < sel.end() and atom.end > sel.end()) { - size_t length = sel.end() - atom.begin; - atom_it = display_buffer.split(atom_it, length); + atom_it = display_buffer.split(atom_it, sel.end()); atom_it->attribute |= Attributes::Underline; ++atom_it; ++sel_it; @@ -71,10 +70,8 @@ public: // [---###---] else if (atom.begin < sel.begin() and atom.end > sel.end()) { - size_t prefix_length = sel.begin() - atom.begin; - atom_it = display_buffer.split(atom_it, prefix_length); - size_t sel_length = sel.end() - sel.begin(); - atom_it = display_buffer.split(atom_it + 1, sel_length); + atom_it = display_buffer.split(atom_it, sel.begin()); + atom_it = display_buffer.split(atom_it + 1, sel.end()); atom_it->attribute |= Attributes::Underline; ++atom_it; ++sel_it; @@ -82,8 +79,7 @@ public: // [------###] else if (atom.begin < sel.begin() and atom.end > sel.begin()) { - size_t length = sel.begin() - atom.begin; - atom_it = display_buffer.split(atom_it, length) + 1; + atom_it = display_buffer.split(atom_it, sel.begin()) + 1; atom_it->attribute |= Attributes::Underline; ++atom_it; } @@ -289,7 +285,10 @@ void Window::update_display_buffer() BufferIterator begin = m_buffer.iterator_at(m_position); BufferIterator end = m_buffer.iterator_at(m_position + BufferCoord(m_dimensions.line, m_dimensions.column+1)); - m_display_buffer.append(DisplayAtom(begin, end, m_buffer.string(begin, end))); + if (begin == end) + return; + + m_display_buffer.append(DisplayAtom(begin, end)); for (auto& filter : m_filters) {