From 56611604b28b6c293b13e3637fbae65e6bea064c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 19 Mar 2019 22:00:57 +1100 Subject: 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. --- src/string.hh | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/string.hh') diff --git a/src/string.hh b/src/string.hh index ae41642c..e69be344 100644 --- a/src/string.hh +++ b/src/string.hh @@ -121,6 +121,9 @@ public: explicit String(StringView str); + struct NoCopy{}; + String(NoCopy, StringView str); + [[gnu::always_inline]] char* data() { return m_data.data(); } @@ -175,6 +178,7 @@ public: } s; Data() { set_empty(); } + Data(NoCopy, const char* data, size_t size); Data(const char* data, size_t size, size_t capacity); Data(const char* data, size_t size) : Data(data, size, size) {} Data(const Data& other) : Data{other.data(), other.size()} {} @@ -257,6 +261,7 @@ template<> struct HashCompatible : std::true_type {}; template<> struct HashCompatible : std::true_type {}; inline String::String(StringView str) : String{str.begin(), str.length()} {} +inline String::String(NoCopy, StringView str) : m_data{NoCopy{}, str.begin(), (size_t)str.length()} {} template inline StringView StringOps::substr(ByteCount from, ByteCount length) const -- cgit v1.2.3