diff options
| author | Maxime Coste <mawww@kakoune.org> | 2019-03-19 22:00:57 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2019-03-19 22:00:57 +1100 |
| commit | 56611604b28b6c293b13e3637fbae65e6bea064c (patch) | |
| tree | 710d7c264d81ac352292cba196b231aca832afda /src/string.hh | |
| parent | c2be661785418be87e06d366979bdcd8ff3cf0b2 (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.hh')
| -rw-r--r-- | src/string.hh | 5 |
1 files changed, 5 insertions, 0 deletions
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<String, StringView> : std::true_type {}; template<> struct HashCompatible<StringView, String> : 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<typename Type, typename CharType> inline StringView StringOps<Type, CharType>::substr(ByteCount from, ByteCount length) const |
