Make SharedString::create take a list of StringViews

This commit is contained in:
Maxime Coste 2017-01-30 12:05:04 +00:00
parent 9d09d14d99
commit f30e164232
2 changed files with 20 additions and 13 deletions

View File

@ -46,7 +46,7 @@ static ParsedLines parse_lines(StringView data)
while (pos < data.end()) while (pos < data.end())
{ {
const char* eol = std::find(pos, data.end(), '\n'); const char* eol = std::find(pos, data.end(), '\n');
res.lines.emplace_back(StringData::create({pos, eol - (crlf and eol != data.end() ? 1 : 0)}, '\n')); res.lines.emplace_back(StringData::create({{pos, eol - (crlf and eol != data.end() ? 1 : 0)}, "\n"}));
pos = eol + 1; pos = eol + 1;
} }
@ -76,7 +76,7 @@ Buffer::Buffer(String name, Flags flags, StringView data,
ParsedLines parsed_lines = parse_lines(data); ParsedLines parsed_lines = parse_lines(data);
if (parsed_lines.lines.empty()) if (parsed_lines.lines.empty())
parsed_lines.lines.emplace_back(StringData::create("\n")); parsed_lines.lines.emplace_back(StringData::create({"\n"}));
#ifdef KAK_DEBUG #ifdef KAK_DEBUG
for (auto& line : parsed_lines.lines) for (auto& line : parsed_lines.lines)
@ -230,7 +230,7 @@ void Buffer::reload(StringView data, timespec fs_timestamp)
ParsedLines parsed_lines = parse_lines(data); ParsedLines parsed_lines = parse_lines(data);
if (parsed_lines.lines.empty()) if (parsed_lines.lines.empty())
parsed_lines.lines.emplace_back(StringData::create("\n")); parsed_lines.lines.emplace_back(StringData::create({"\n"}));
const bool record_undo = not (m_flags & Flags::NoUndo); const bool record_undo = not (m_flags & Flags::NoUndo);
@ -470,9 +470,9 @@ BufferCoord Buffer::do_insert(BufferCoord pos, StringView content)
} }
} }
if (start == 0) if (start == 0)
new_lines.push_back(StringData::create(prefix + content + suffix)); new_lines.push_back(StringData::create({prefix, content, suffix}));
else if (start != content.length() or not suffix.empty()) else if (start != content.length() or not suffix.empty())
new_lines.push_back(StringData::create(content.substr(start) + suffix)); new_lines.push_back(StringData::create({content.substr(start), suffix}));
auto line_it = m_lines.begin() + (int)pos.line; auto line_it = m_lines.begin() + (int)pos.line;
auto new_lines_it = new_lines.begin(); auto new_lines_it = new_lines.begin();
@ -497,13 +497,13 @@ BufferCoord Buffer::do_erase(BufferCoord begin, BufferCoord end)
kak_assert(is_valid(end)); kak_assert(is_valid(end));
StringView prefix = m_lines[begin.line].substr(0, begin.column); StringView prefix = m_lines[begin.line].substr(0, begin.column);
StringView suffix = m_lines[end.line].substr(end.column); StringView suffix = m_lines[end.line].substr(end.column);
String new_line = prefix + suffix;
BufferCoord next; BufferCoord next;
if (new_line.length() != 0) if (not prefix.empty() or not suffix.empty())
{ {
auto new_line = StringData::create({prefix, suffix});
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.get_storage(begin.line) = StringData::create(new_line); m_lines.get_storage(begin.line) = std::move(new_line);
next = begin; next = begin;
} }
else else

View File

@ -6,6 +6,8 @@
#include "utils.hh" #include "utils.hh"
#include "unordered_map.hh" #include "unordered_map.hh"
#include <numeric>
namespace Kakoune namespace Kakoune
{ {
@ -30,14 +32,19 @@ struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
static void ptr_moved(StringData*, void*, void*) noexcept {} static void ptr_moved(StringData*, void*, void*) noexcept {}
}; };
static RefPtr<StringData, PtrPolicy> create(StringView str, char back = 0) static RefPtr<StringData, PtrPolicy> create(ArrayView<const StringView> strs)
{ {
const int len = (int)str.length() + (back != 0 ? 1 : 0); const int len = std::accumulate(strs.begin(), strs.end(), 0,
[](int l, StringView s)
{ return l + (int)s.length(); });
void* ptr = StringData::operator new(sizeof(StringData) + len + 1); void* ptr = StringData::operator new(sizeof(StringData) + len + 1);
auto* res = new (ptr) StringData(0, len); auto* res = new (ptr) StringData(0, len);
std::copy(str.begin(), str.end(), res->data()); auto* data = res->data();
if (back != 0) for (auto& str : strs)
res->data()[len-1] = back; {
memcpy(data, str.begin(), (size_t)str.length());
data += (int)str.length();
}
res->data()[len] = 0; res->data()[len] = 0;
return RefPtr<StringData, PtrPolicy>{res}; return RefPtr<StringData, PtrPolicy>{res};
} }