rename SharedString::Storage to StringStorage and use directly in Buffer
This commit is contained in:
parent
3697548e35
commit
b1281d225d
|
@ -34,7 +34,7 @@ Buffer::Buffer(String name, Flags flags, Vector<String> lines,
|
||||||
for (auto& line : lines)
|
for (auto& line : lines)
|
||||||
{
|
{
|
||||||
kak_assert(not line.empty() and line.back() == '\n');
|
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 });
|
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)
|
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,
|
auto character = std::max(0_char, std::min(line.char_count_to(coord.column) + offset,
|
||||||
line.char_length() - 1));
|
line.char_length() - 1));
|
||||||
return {coord.line, line.byte_count_to(character)};
|
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 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 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));
|
character = std::max(0_char, std::min(character, content.char_length() - 2));
|
||||||
return {line, content.byte_count_to(character), character};
|
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)
|
for (auto& line : lines)
|
||||||
{
|
{
|
||||||
kak_assert(not line.empty() and line.back() == '\n');
|
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))
|
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());
|
||||||
|
@ -243,8 +243,8 @@ void Buffer::check_invariant() const
|
||||||
kak_assert(not m_lines.empty());
|
kak_assert(not m_lines.empty());
|
||||||
for (auto& line : m_lines)
|
for (auto& line : m_lines)
|
||||||
{
|
{
|
||||||
kak_assert(line.length() > 0);
|
kak_assert(line->strview().length() > 0);
|
||||||
kak_assert(line.back() == '\n');
|
kak_assert(line->strview().back() == '\n');
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -268,12 +268,12 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content)
|
||||||
{
|
{
|
||||||
if (content[i] == '\n')
|
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;
|
start = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (start != content.length())
|
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 };
|
begin = pos.column == 0 ? pos : ByteCoord{ pos.line + 1, 0 };
|
||||||
end = ByteCoord{ line_count(), 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 prefix = m_lines[pos.line].substr(0, pos.column);
|
||||||
StringView suffix = m_lines[pos.line].substr(pos.column);
|
StringView suffix = m_lines[pos.line].substr(pos.column);
|
||||||
|
|
||||||
Vector<SharedString> new_lines;
|
LineList new_lines;
|
||||||
|
|
||||||
ByteCount start = 0;
|
ByteCount start = 0;
|
||||||
for (ByteCount i = 0; i < content.length(); ++i)
|
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);
|
StringView line_content = content.substr(start, i + 1 - start);
|
||||||
if (start == 0)
|
if (start == 0)
|
||||||
new_lines.emplace_back(prefix + line_content);
|
new_lines.emplace_back(StringStorage::create(prefix + line_content));
|
||||||
else
|
else
|
||||||
new_lines.push_back(line_content);
|
new_lines.push_back(StringStorage::create(line_content));
|
||||||
start = i + 1;
|
start = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (start == 0)
|
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())
|
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;
|
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)
|
if (new_line.length() != 0)
|
||||||
{
|
{
|
||||||
m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line);
|
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;
|
next = begin;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -547,7 +547,7 @@ String Buffer::debug_description() const
|
||||||
|
|
||||||
size_t content_size = 0;
|
size_t content_size = 0;
|
||||||
for (auto& line : m_lines)
|
for (auto& line : m_lines)
|
||||||
content_size += (int)line.length();
|
content_size += (int)line->strview().length();
|
||||||
|
|
||||||
size_t additional_size = 0;
|
size_t additional_size = 0;
|
||||||
for (auto& undo_group : m_history)
|
for (auto& undo_group : m_history)
|
||||||
|
|
|
@ -121,9 +121,12 @@ public:
|
||||||
BufferIterator end() const;
|
BufferIterator end() const;
|
||||||
LineCount line_count() const;
|
LineCount line_count() const;
|
||||||
|
|
||||||
const SharedString& operator[](LineCount line) const
|
StringView operator[](LineCount line) const
|
||||||
{ return m_lines[line]; }
|
{ 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
|
// returns an iterator at given coordinates. clamp line_and_column
|
||||||
BufferIterator iterator_at(ByteCoord coord) const;
|
BufferIterator iterator_at(ByteCoord coord) const;
|
||||||
|
|
||||||
|
@ -166,16 +169,27 @@ private:
|
||||||
|
|
||||||
void on_option_changed(const Option& option) override;
|
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
|
struct LineList : LineListBase
|
||||||
{
|
{
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
SharedString& operator[](LineCount line)
|
ref_ptr<StringStorage>& get_storage(LineCount line)
|
||||||
{ return LineListBase::operator[]((int)line); }
|
{ return LineListBase::operator[]((int)line); }
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
const SharedString& operator[](LineCount line) const
|
const ref_ptr<StringStorage>& get_storage(LineCount line) const
|
||||||
{ return LineListBase::operator[]((int)line); }
|
{ 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;
|
LineList m_lines;
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ SharedString StringRegistry::intern(StringView str)
|
||||||
SharedString shared_str = str;
|
SharedString shared_str = str;
|
||||||
it = m_strings.emplace(shared_str, shared_str.m_storage).first;
|
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()
|
void StringRegistry::purge_unused()
|
||||||
|
|
|
@ -9,44 +9,44 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
|
||||||
|
{
|
||||||
|
int refcount;
|
||||||
|
int length;
|
||||||
|
char data[1];
|
||||||
|
|
||||||
|
StringView strview() const { return {data, length}; }
|
||||||
|
|
||||||
|
static StringStorage* create(StringView str)
|
||||||
|
{
|
||||||
|
const int len = (int)str.length();
|
||||||
|
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;
|
||||||
|
res->data[len] = 0;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void destroy(StringStorage* s)
|
||||||
|
{
|
||||||
|
StringStorage::operator delete(s, sizeof(StringStorage) + s->length);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
class SharedString : public StringView
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
struct Storage : UseMemoryDomain<MemoryDomain::SharedString>
|
|
||||||
{
|
|
||||||
int refcount;
|
|
||||||
int length;
|
|
||||||
char data[1];
|
|
||||||
|
|
||||||
StringView strview() const { return {data, length}; }
|
|
||||||
|
|
||||||
static Storage* create(StringView str)
|
|
||||||
{
|
|
||||||
const int len = (int)str.length();
|
|
||||||
void* ptr = Storage::operator new(sizeof(Storage) + len);
|
|
||||||
Storage* res = reinterpret_cast<Storage*>(ptr);
|
|
||||||
memcpy(res->data, str.data(), len);
|
|
||||||
res->refcount = 0;
|
|
||||||
res->length = len;
|
|
||||||
res->data[len] = 0;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void destroy(Storage* s)
|
|
||||||
{
|
|
||||||
Storage::operator delete(s, sizeof(Storage) + 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); }
|
|
||||||
};
|
|
||||||
|
|
||||||
SharedString() = default;
|
SharedString() = default;
|
||||||
SharedString(StringView str)
|
SharedString(StringView str)
|
||||||
{
|
{
|
||||||
if (not str.empty())
|
if (not str.empty())
|
||||||
{
|
{
|
||||||
m_storage = Storage::create(str);
|
m_storage = StringStorage::create(str);
|
||||||
StringView::operator=(m_storage->strview());
|
StringView::operator=(m_storage->strview());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,12 +64,15 @@ public:
|
||||||
return SharedString{StringView::substr(from, length), m_storage};
|
return SharedString{StringView::substr(from, length), m_storage};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
explicit SharedString(ref_ptr<StringStorage> storage)
|
||||||
|
: StringView{storage->strview()}, m_storage(std::move(storage)) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SharedString(StringView str, ref_ptr<Storage> storage)
|
SharedString(StringView str, ref_ptr<StringStorage> storage)
|
||||||
: StringView{str}, m_storage(std::move(storage)) {}
|
: StringView{str}, m_storage(std::move(storage)) {}
|
||||||
|
|
||||||
friend class StringRegistry;
|
friend class StringRegistry;
|
||||||
ref_ptr<Storage> m_storage;
|
ref_ptr<StringStorage> m_storage;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline size_t hash_value(const SharedString& str)
|
inline size_t hash_value(const SharedString& str)
|
||||||
|
@ -85,7 +88,7 @@ public:
|
||||||
void purge_unused();
|
void purge_unused();
|
||||||
|
|
||||||
private:
|
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)
|
inline SharedString intern(StringView str)
|
||||||
|
|
|
@ -119,7 +119,7 @@ void WordDB::update_db()
|
||||||
if (modif.new_line + l >= buffer.line_count())
|
if (modif.new_line + l >= buffer.line_count())
|
||||||
break;
|
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()));
|
add_words(get_words(new_lines.back()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user