Buffer: add some utf8 sanity checks
This commit is contained in:
parent
bff015d5b9
commit
194bf6ac98
|
@ -5,6 +5,7 @@
|
||||||
#include "assert.hh"
|
#include "assert.hh"
|
||||||
#include "utils.hh"
|
#include "utils.hh"
|
||||||
#include "context.hh"
|
#include "context.hh"
|
||||||
|
#include "utf8.hh"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
@ -236,6 +237,7 @@ void Buffer::check_invariant() const
|
||||||
|
|
||||||
void Buffer::do_insert(const BufferIterator& pos, const String& content)
|
void Buffer::do_insert(const BufferIterator& pos, const String& content)
|
||||||
{
|
{
|
||||||
|
assert(pos.is_end() or utf8::is_character_start(pos));
|
||||||
++m_timestamp;
|
++m_timestamp;
|
||||||
CharCount offset = pos.offset();
|
CharCount offset = pos.offset();
|
||||||
|
|
||||||
|
@ -310,26 +312,27 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content)
|
||||||
listener->on_insert(begin_it, end_it);
|
listener->on_insert(begin_it, end_it);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::do_erase(const BufferIterator& pos, CharCount length)
|
void Buffer::do_erase(const BufferIterator& begin, const BufferIterator& end)
|
||||||
{
|
{
|
||||||
|
assert(utf8::is_character_start(begin) and
|
||||||
|
(end.is_end() or utf8::is_character_start(end)));
|
||||||
++m_timestamp;
|
++m_timestamp;
|
||||||
BufferIterator end = pos + length;
|
const CharCount length = end - begin;
|
||||||
assert(end.is_valid());
|
String prefix = m_lines[begin.line()].content.substr(0, begin.column());
|
||||||
String prefix = m_lines[pos.line()].content.substr(0, pos.column());
|
|
||||||
String suffix = m_lines[end.line()].content.substr(end.column());
|
String suffix = m_lines[end.line()].content.substr(end.column());
|
||||||
Line new_line = { m_lines[pos.line()].start, prefix + suffix };
|
Line new_line = { m_lines[begin.line()].start, prefix + suffix };
|
||||||
|
|
||||||
m_lines.erase(m_lines.begin() + (int)pos.line(), m_lines.begin() + (int)end.line() + 1);
|
m_lines.erase(m_lines.begin() + (int)begin.line(), m_lines.begin() + (int)end.line() + 1);
|
||||||
if (new_line.length() != 0)
|
if (new_line.length() != 0)
|
||||||
m_lines.insert(m_lines.begin() + (int)pos.line(), std::move(new_line));
|
m_lines.insert(m_lines.begin() + (int)begin.line(), std::move(new_line));
|
||||||
|
|
||||||
for (LineCount i = pos.line()+1; i < line_count(); ++i)
|
for (LineCount i = begin.line()+1; i < line_count(); ++i)
|
||||||
m_lines[i].start -= length;
|
m_lines[i].start -= length;
|
||||||
|
|
||||||
check_invariant();
|
check_invariant();
|
||||||
|
|
||||||
for (auto listener : m_change_listeners)
|
for (auto listener : m_change_listeners)
|
||||||
listener->on_erase(pos, end);
|
listener->on_erase(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::apply_modification(const Modification& modification)
|
void Buffer::apply_modification(const Modification& modification)
|
||||||
|
@ -349,9 +352,9 @@ void Buffer::apply_modification(const Modification& modification)
|
||||||
case Modification::Erase:
|
case Modification::Erase:
|
||||||
{
|
{
|
||||||
CharCount count = modification.content.length();
|
CharCount count = modification.content.length();
|
||||||
assert(string(modification.position, modification.position + count)
|
BufferIterator end = modification.position + count;
|
||||||
== modification.content);
|
assert(string(modification.position, end) == modification.content);
|
||||||
do_erase(modification.position, count);
|
do_erase(modification.position, end);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -382,7 +385,7 @@ void Buffer::erase(BufferIterator begin, BufferIterator end)
|
||||||
|
|
||||||
m_current_undo_group.emplace_back(Modification::Erase, begin,
|
m_current_undo_group.emplace_back(Modification::Erase, begin,
|
||||||
string(begin, end));
|
string(begin, end));
|
||||||
do_erase(begin, end - begin);
|
do_erase(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
Window* Buffer::get_or_create_window()
|
Window* Buffer::get_or_create_window()
|
||||||
|
|
|
@ -205,7 +205,7 @@ private:
|
||||||
LineList m_lines;
|
LineList m_lines;
|
||||||
|
|
||||||
void do_insert(const BufferIterator& pos, const String& content);
|
void do_insert(const BufferIterator& pos, const String& content);
|
||||||
void do_erase(const BufferIterator& pos, CharCount length);
|
void do_erase(const BufferIterator& begin, const BufferIterator& end);
|
||||||
|
|
||||||
String m_name;
|
String m_name;
|
||||||
const Type m_type;
|
const Type m_type;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user