summaryrefslogtreecommitdiff
path: root/src/string.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2019-03-19 22:00:57 +1100
committerMaxime Coste <mawww@kakoune.org>2019-03-19 22:00:57 +1100
commit56611604b28b6c293b13e3637fbae65e6bea064c (patch)
tree710d7c264d81ac352292cba196b231aca832afda /src/string.cc
parentc2be661785418be87e06d366979bdcd8ff3cf0b2 (diff)
Make String able to reference external data without copying
Sometimes we really need to have a String instead of a StringView, but some of those strings might not need to own their data. Make it possible to explicitely construct a String that does not own the underlying buffer. Use it when parsing balanced strings.
Diffstat (limited to 'src/string.cc')
-rw-r--r--src/string.cc11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/string.cc b/src/string.cc
index 6ff89450..d44963ba 100644
--- a/src/string.cc
+++ b/src/string.cc
@@ -6,6 +6,13 @@
namespace Kakoune
{
+String::Data::Data(String::NoCopy, const char* data, size_t size)
+{
+ l.ptr = const_cast<char*>(data);
+ l.size = size;
+ l.capacity = 0;
+}
+
String::Data::Data(const char* data, size_t size, size_t capacity)
{
if (capacity > Short::capacity)
@@ -71,7 +78,7 @@ String::Data& String::Data::operator=(Data&& other) noexcept
template<bool copy>
void String::Data::reserve(size_t new_capacity)
{
- if (new_capacity <= capacity())
+ if (capacity() != 0 and new_capacity <= capacity())
return;
if (is_long())
@@ -123,7 +130,7 @@ void String::Data::clear()
void String::Data::release()
{
- if (is_long())
+ if (is_long() and l.capacity != 0)
Alloc{}.deallocate(l.ptr, l.capacity+1);
}