Tweak SharedString

This commit is contained in:
Maxime Coste 2015-01-25 22:36:05 +00:00
parent 79954e89ab
commit 42966317b8
2 changed files with 10 additions and 9 deletions

View File

@ -32,7 +32,7 @@ Buffer::Buffer(String name, Flags flags, BufferLines lines,
for (auto& line : lines) for (auto& line : lines)
{ {
kak_assert(not line->length == 0 and line->data[line->length-1] == '\n'); kak_assert(not line->length == 0 and line->data()[line->length-1] == '\n');
} }
static_cast<BufferLines&>(m_lines) = std::move(lines); static_cast<BufferLines&>(m_lines) = std::move(lines);
@ -173,7 +173,7 @@ void Buffer::reload(BufferLines lines, time_t fs_timestamp)
for (auto& line : lines) for (auto& line : lines)
{ {
kak_assert(not line->length == 0 and line->data[line->length-1] == '\n'); kak_assert(not line->length == 0 and line->data()[line->length-1] == '\n');
if (not (m_flags & Flags::NoUndo)) if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back( m_current_undo_group.emplace_back(
Modification::Insert, line_count()-1, m_lines.back()); Modification::Insert, line_count()-1, m_lines.back());

View File

@ -13,27 +13,28 @@ struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
{ {
int refcount; int refcount;
int length; int length;
char data[1];
StringView strview() const { return {data, length}; } char* data() { return reinterpret_cast<char*>(this + 1); }
const char* data() const { return reinterpret_cast<const char*>(this + 1); }
StringView strview() const { return {data(), length}; }
static StringStorage* create(StringView str, char back = 0) static StringStorage* create(StringView str, char back = 0)
{ {
const int len = (int)str.length() + (back != 0 ? 1 : 0); const int len = (int)str.length() + (back != 0 ? 1 : 0);
void* ptr = StringStorage::operator new(sizeof(StringStorage) + len); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1);
StringStorage* res = reinterpret_cast<StringStorage*>(ptr); StringStorage* res = reinterpret_cast<StringStorage*>(ptr);
memcpy(res->data, str.data(), (int)str.length()); memcpy(res->data(), str.data(), (int)str.length());
res->refcount = 0; res->refcount = 0;
res->length = len; res->length = len;
if (back != 0) if (back != 0)
res->data[len-1] = back; res->data()[len-1] = back;
res->data[len] = 0; res->data()[len] = 0;
return res; return res;
} }
static void destroy(StringStorage* s) static void destroy(StringStorage* s)
{ {
StringStorage::operator delete(s, sizeof(StringStorage) + s->length); StringStorage::operator delete(s, sizeof(StringStorage) + s->length + 1);
} }
friend void inc_ref_count(StringStorage* s) { ++s->refcount; } friend void inc_ref_count(StringStorage* s) { ++s->refcount; }