rename SharedString::Storage to StringStorage and use directly in Buffer

This commit is contained in:
Maxime Coste 2015-01-19 19:31:56 +00:00
parent 3697548e35
commit b1281d225d
5 changed files with 71 additions and 54 deletions

View File

@ -34,7 +34,7 @@ Buffer::Buffer(String name, Flags flags, Vector<String> lines,
for (auto& line : lines)
{
kak_assert(not line.empty() and line.back() == '\n');
m_lines.emplace_back(std::move(line));
m_lines.emplace_back(StringStorage::create(line));
}
m_changes.push_back({ Change::Insert, {0,0}, line_count(), true });
@ -107,7 +107,7 @@ ByteCoord Buffer::clamp(ByteCoord coord) const
ByteCoord Buffer::offset_coord(ByteCoord coord, CharCount offset)
{
auto& line = m_lines[coord.line];
StringView line = m_lines[coord.line];
auto character = std::max(0_char, std::min(line.char_count_to(coord.column) + offset,
line.char_length() - 1));
return {coord.line, line.byte_count_to(character)};
@ -117,7 +117,7 @@ ByteCoordAndTarget Buffer::offset_coord(ByteCoordAndTarget coord, LineCount offs
{
auto character = coord.target == -1 ? m_lines[coord.line].char_count_to(coord.column) : coord.target;
auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1);
auto& content = m_lines[line];
StringView content = m_lines[line];
character = std::max(0_char, std::min(character, content.char_length() - 2));
return {line, content.byte_count_to(character), character};
@ -178,7 +178,7 @@ void Buffer::reload(Vector<String> lines, time_t fs_timestamp)
for (auto& line : lines)
{
kak_assert(not line.empty() and line.back() == '\n');
m_lines.emplace_back(std::move(line));
m_lines.emplace_back(StringStorage::create(line));
if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(
Modification::Insert, line_count()-1, m_lines.back());
@ -243,8 +243,8 @@ void Buffer::check_invariant() const
kak_assert(not m_lines.empty());
for (auto& line : m_lines)
{
kak_assert(line.length() > 0);
kak_assert(line.back() == '\n');
kak_assert(line->strview().length() > 0);
kak_assert(line->strview().back() == '\n');
}
#endif
}
@ -268,12 +268,12 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
{
if (content[i] == '\n')
{
m_lines.push_back(content.substr(start, i + 1 - start));
m_lines.push_back(StringStorage::create(content.substr(start, i + 1 - start)));
start = i + 1;
}
}
if (start != content.length())
m_lines.push_back(content.substr(start));
m_lines.push_back(StringStorage::create(content.substr(start)));
begin = pos.column == 0 ? pos : ByteCoord{ pos.line + 1, 0 };
end = ByteCoord{ line_count(), 0 };
@ -284,7 +284,7 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
StringView prefix = m_lines[pos.line].substr(0, pos.column);
StringView suffix = m_lines[pos.line].substr(pos.column);
Vector<SharedString> new_lines;
LineList new_lines;
ByteCount start = 0;
for (ByteCount i = 0; i < content.length(); ++i)
@ -293,16 +293,16 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
{
StringView line_content = content.substr(start, i + 1 - start);
if (start == 0)
new_lines.emplace_back(prefix + line_content);
new_lines.emplace_back(StringStorage::create(prefix + line_content));
else
new_lines.push_back(line_content);
new_lines.push_back(StringStorage::create(line_content));
start = i + 1;
}
}
if (start == 0)
new_lines.emplace_back(prefix + content + suffix);
new_lines.emplace_back(StringStorage::create(prefix + content + suffix));
else if (start != content.length() or not suffix.empty())
new_lines.emplace_back(content.substr(start) + suffix);
new_lines.emplace_back(StringStorage::create(content.substr(start) + suffix));
LineCount last_line = pos.line + new_lines.size() - 1;
@ -332,7 +332,7 @@ ByteCoord Buffer::do_erase(ByteCoord begin, ByteCoord end)
if (new_line.length() != 0)
{
m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line);
m_lines[begin.line] = SharedString(new_line);
m_lines.get_storage(begin.line) = StringStorage::create(new_line);
next = begin;
}
else
@ -547,7 +547,7 @@ String Buffer::debug_description() const
size_t content_size = 0;
for (auto& line : m_lines)
content_size += (int)line.length();
content_size += (int)line->strview().length();
size_t additional_size = 0;
for (auto& undo_group : m_history)

View File

@ -121,9 +121,12 @@ public:
BufferIterator end() const;
LineCount line_count() const;
const SharedString& operator[](LineCount line) const
StringView operator[](LineCount line) const
{ return m_lines[line]; }
SharedString shared_line(LineCount line) const
{ return m_lines.get_shared(line); }
// returns an iterator at given coordinates. clamp line_and_column
BufferIterator iterator_at(ByteCoord coord) const;
@ -166,16 +169,27 @@ private:
void on_option_changed(const Option& option) override;
using LineListBase = Vector<SharedString, MemoryDomain::BufferContent>;
using LineListBase = Vector<ref_ptr<StringStorage>, MemoryDomain::BufferContent>;
struct LineList : LineListBase
{
[[gnu::always_inline]]
SharedString& operator[](LineCount line)
ref_ptr<StringStorage>& get_storage(LineCount line)
{ return LineListBase::operator[]((int)line); }
[[gnu::always_inline]]
const SharedString& operator[](LineCount line) const
const ref_ptr<StringStorage>& get_storage(LineCount line) const
{ return LineListBase::operator[]((int)line); }
[[gnu::always_inline]]
SharedString get_shared(LineCount line) const
{ return SharedString{get_storage(line)}; }
[[gnu::always_inline]]
StringView operator[](LineCount line) const
{ return get_storage(line)->strview(); }
StringView front() const { return LineListBase::front()->strview(); }
StringView back() const { return LineListBase::back()->strview(); }
};
LineList m_lines;

View File

@ -12,7 +12,7 @@ SharedString StringRegistry::intern(StringView str)
SharedString shared_str = str;
it = m_strings.emplace(shared_str, shared_str.m_storage).first;
}
return {it->second->strview(), it->second};
return SharedString{it->second};
}
void StringRegistry::purge_unused()

View File

@ -9,22 +9,19 @@
namespace Kakoune
{
class SharedString : public StringView
struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
{
public:
struct Storage : UseMemoryDomain<MemoryDomain::SharedString>
{
int refcount;
int length;
char data[1];
StringView strview() const { return {data, length}; }
static Storage* create(StringView str)
static StringStorage* create(StringView str)
{
const int len = (int)str.length();
void* ptr = Storage::operator new(sizeof(Storage) + len);
Storage* res = reinterpret_cast<Storage*>(ptr);
void* ptr = StringStorage::operator new(sizeof(StringStorage) + len);
StringStorage* res = reinterpret_cast<StringStorage*>(ptr);
memcpy(res->data, str.data(), len);
res->refcount = 0;
res->length = len;
@ -32,21 +29,24 @@ public:
return res;
}
static void destroy(Storage* s)
static void destroy(StringStorage* s)
{
Storage::operator delete(s, sizeof(Storage) + s->length);
StringStorage::operator delete(s, sizeof(StringStorage) + s->length);
}
friend void inc_ref_count(Storage* s) { ++s->refcount; }
friend void dec_ref_count(Storage* s) { if (--s->refcount == 0) Storage::destroy(s); }
};
friend void inc_ref_count(StringStorage* s) { ++s->refcount; }
friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); }
};
class SharedString : public StringView
{
public:
SharedString() = default;
SharedString(StringView str)
{
if (not str.empty())
{
m_storage = Storage::create(str);
m_storage = StringStorage::create(str);
StringView::operator=(m_storage->strview());
}
}
@ -64,12 +64,15 @@ public:
return SharedString{StringView::substr(from, length), m_storage};
}
explicit SharedString(ref_ptr<StringStorage> storage)
: StringView{storage->strview()}, m_storage(std::move(storage)) {}
private:
SharedString(StringView str, ref_ptr<Storage> storage)
SharedString(StringView str, ref_ptr<StringStorage> storage)
: StringView{str}, m_storage(std::move(storage)) {}
friend class StringRegistry;
ref_ptr<Storage> m_storage;
ref_ptr<StringStorage> m_storage;
};
inline size_t hash_value(const SharedString& str)
@ -85,7 +88,7 @@ public:
void purge_unused();
private:
UnorderedMap<StringView, ref_ptr<SharedString::Storage>, MemoryDomain::SharedString> m_strings;
UnorderedMap<StringView, ref_ptr<StringStorage>, MemoryDomain::SharedString> m_strings;
};
inline SharedString intern(StringView str)

View File

@ -119,7 +119,7 @@ void WordDB::update_db()
if (modif.new_line + l >= buffer.line_count())
break;
new_lines.push_back(buffer[modif.new_line + l]);
new_lines.push_back(buffer.shared_line(modif.new_line + l));
add_words(get_words(new_lines.back()));
}
}