summaryrefslogtreecommitdiff
path: root/src/shared_string.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-01-22 13:39:29 +0000
committerMaxime Coste <frrrwww@gmail.com>2015-01-22 13:39:29 +0000
commitcb197f57ba77ae16ed90d0e470f79d3245e16a70 (patch)
tree680eba24571af275b81b84071b5805a3ca535797 /src/shared_string.hh
parent2516c16bb9b35db18661ab180a41b0e157addefb (diff)
Avoid temporary strings on buffer load/reload
Pass directly a Vector<ref_ptr<StringStorage>> to the buffer
Diffstat (limited to 'src/shared_string.hh')
-rw-r--r--src/shared_string.hh13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/shared_string.hh b/src/shared_string.hh
index 489d9326..b8db3c89 100644
--- a/src/shared_string.hh
+++ b/src/shared_string.hh
@@ -17,14 +17,16 @@ struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
StringView strview() const { return {data, length}; }
- static StringStorage* create(StringView str)
+ static StringStorage* create(StringView str, char back = 0)
{
- const int len = (int)str.length();
+ const int len = (int)str.length() + (back != 0 ? 1 : 0);
void* ptr = StringStorage::operator new(sizeof(StringStorage) + len);
StringStorage* res = reinterpret_cast<StringStorage*>(ptr);
- memcpy(res->data, str.data(), len);
+ memcpy(res->data, str.data(), (int)str.length());
res->refcount = 0;
res->length = len;
+ if (back != 0)
+ res->data[len-1] = back;
res->data[len] = 0;
return res;
}
@@ -38,6 +40,11 @@ struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); }
};
+inline ref_ptr<StringStorage> operator""_ss(const char* ptr, size_t len)
+{
+ return StringStorage::create({ptr, (int)len});
+}
+
class SharedString : public StringView
{
public: