DisplayBuffer: do not store content in atom, begin and end are sufficient

This commit is contained in:
Maxime Coste 2011-10-07 14:19:58 +00:00
parent a19f4f059d
commit 10106e8c8e
5 changed files with 24 additions and 30 deletions

View File

@ -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(atom < end());
assert(pos_in_atom > 0); assert(pos > atom->begin);
assert(pos_in_atom < atom->content.length()); assert(pos < atom->end);
DisplayAtom new_atom(atom->begin, atom->begin + pos_in_atom, DisplayAtom new_atom(atom->begin, pos,
atom->content.substr(0, pos_in_atom),
atom->fg_color, atom->bg_color, atom->attribute); atom->fg_color, atom->bg_color, atom->attribute);
atom->begin = atom->begin + pos_in_atom; atom->begin = pos;
atom->content = atom->content.substr(pos_in_atom);
return insert(atom, std::move(new_atom)); return insert(atom, std::move(new_atom));
} }

View File

@ -35,7 +35,6 @@ enum class Color
struct DisplayAtom struct DisplayAtom
{ {
std::string content;
BufferIterator begin; BufferIterator begin;
BufferIterator end; BufferIterator end;
Color fg_color; Color fg_color;
@ -43,12 +42,10 @@ struct DisplayAtom
Attribute attribute; Attribute attribute;
DisplayAtom(BufferIterator begin, BufferIterator end, DisplayAtom(BufferIterator begin, BufferIterator end,
const std::string& content,
Color fg_color = Color::Default, Color fg_color = Color::Default,
Color bg_color = Color::Default, Color bg_color = Color::Default,
Attribute attribute = Attributes::Normal) Attribute attribute = Attributes::Normal)
: content(content), : begin(begin),
begin(begin),
end(end), end(end),
fg_color(fg_color), fg_color(fg_color),
bg_color(bg_color), bg_color(bg_color),
@ -68,7 +65,7 @@ public:
void clear() { m_atoms.clear(); } void clear() { m_atoms.clear(); }
void append(const DisplayAtom& atom) { m_atoms.push_back(atom); } void append(const DisplayAtom& atom) { m_atoms.push_back(atom); }
iterator insert(iterator where, const DisplayAtom& atom) { return m_atoms.insert(where, 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 begin() { return m_atoms.begin(); }
iterator end() { return m_atoms.end(); } iterator end() { return m_atoms.end(); }

View File

@ -9,16 +9,16 @@ void colorize_regex(DisplayBuffer& display_buffer,
for (auto atom_it = display_buffer.begin(); for (auto atom_it = display_buffer.begin();
atom_it != display_buffer.end(); ++atom_it) atom_it != display_buffer.end(); ++atom_it)
{ {
boost::smatch matches; boost::match_results<BufferIterator> matches;
if (boost::regex_search(atom_it->content, matches, ex, boost::match_nosubs)) 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(); const BufferIterator& begin = matches.begin()->first;
if (pos != 0) if (begin != atom_it->begin)
atom_it = display_buffer.split(atom_it, pos) + 1; atom_it = display_buffer.split(atom_it, begin) + 1;
pos = matches.begin()->second - matches.begin()->first; const BufferIterator& end = matches.begin()->second;
if (pos != atom_it->content.length()) if (end != atom_it->end)
atom_it = display_buffer.split(atom_it, pos); atom_it = display_buffer.split(atom_it, end);
atom_it->fg_color = color; atom_it->fg_color = color;
} }

View File

@ -81,7 +81,7 @@ void draw_window(Window& window)
WindowCoord position; WindowCoord position;
for (const DisplayAtom& atom : window.display_buffer()) 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_UNDERLINE, atom.attribute & Underline);
set_attribute(A_REVERSE, atom.attribute & Reverse); set_attribute(A_REVERSE, atom.attribute & Reverse);

View File

@ -62,8 +62,7 @@ public:
// [###------] // [###------]
if (atom.begin >= sel.begin() and atom.begin < sel.end() and atom.end > sel.end()) 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, sel.end());
atom_it = display_buffer.split(atom_it, length);
atom_it->attribute |= Attributes::Underline; atom_it->attribute |= Attributes::Underline;
++atom_it; ++atom_it;
++sel_it; ++sel_it;
@ -71,10 +70,8 @@ public:
// [---###---] // [---###---]
else if (atom.begin < sel.begin() and atom.end > sel.end()) 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, sel.begin());
atom_it = display_buffer.split(atom_it, prefix_length); atom_it = display_buffer.split(atom_it + 1, sel.end());
size_t sel_length = sel.end() - sel.begin();
atom_it = display_buffer.split(atom_it + 1, sel_length);
atom_it->attribute |= Attributes::Underline; atom_it->attribute |= Attributes::Underline;
++atom_it; ++atom_it;
++sel_it; ++sel_it;
@ -82,8 +79,7 @@ public:
// [------###] // [------###]
else if (atom.begin < sel.begin() and atom.end > sel.begin()) 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, sel.begin()) + 1;
atom_it = display_buffer.split(atom_it, length) + 1;
atom_it->attribute |= Attributes::Underline; atom_it->attribute |= Attributes::Underline;
++atom_it; ++atom_it;
} }
@ -289,7 +285,10 @@ void Window::update_display_buffer()
BufferIterator begin = m_buffer.iterator_at(m_position); BufferIterator begin = m_buffer.iterator_at(m_position);
BufferIterator end = m_buffer.iterator_at(m_position + BufferIterator end = m_buffer.iterator_at(m_position +
BufferCoord(m_dimensions.line, m_dimensions.column+1)); 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) for (auto& filter : m_filters)
{ {