From f64224131bed17db304e1bc6f498a91bdcd85ce9 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 27 Jun 2020 13:27:17 +0200 Subject: [PATCH] Tweaks for StringView to make the backing value the owner of the string. --- src/String/StringView.hpp | 13 +++++++++---- src/String/StringViewLiteral.hpp | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/String/StringView.hpp b/src/String/StringView.hpp index e1126a0..ca31270 100644 --- a/src/String/StringView.hpp +++ b/src/String/StringView.hpp @@ -16,22 +16,28 @@ namespace ArbUt { class __ConstStringCharHolder { - std::string_view _value; + char* _value; std::atomic _references; __ConstStringCharHolder(const __ConstStringCharHolder& o) = delete; __ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete; public: - __ConstStringCharHolder(const std::string_view& value) noexcept : _value(value), _references(1) {} + __ConstStringCharHolder(const std::string_view& value) noexcept + : _value(new char[value.length() + 1]), _references(1) { + strncpy(_value, value.data(), value.length()); + _value[value.length()] = '\0'; + } + ~__ConstStringCharHolder() { delete[] _value; } inline void RemoveReference() noexcept { if (--_references <= 0) { delete this; } } + inline void AddReference() noexcept { _references++; } - inline constexpr const std::string_view& GetValue() const noexcept { return _value; } + inline constexpr std::string_view GetValue() const noexcept { return std::string_view(_value); } }; } @@ -39,7 +45,6 @@ inline static constexpr char charToLower(const char c) { return (c >= 'A' && c < static uint32_t constexpr Hash(char const* input) { return charToLower(*input) ? static_cast(charToLower(*input)) + 33 * Hash(input + 1) : 5381; }; -static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; } namespace ArbUt { class StringView final : public BasicStringView { diff --git a/src/String/StringViewLiteral.hpp b/src/String/StringViewLiteral.hpp index 7c53f5e..c988540 100644 --- a/src/String/StringViewLiteral.hpp +++ b/src/String/StringViewLiteral.hpp @@ -5,6 +5,8 @@ #include "BasicStringView.hpp" #include "StringView.hpp" +static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; } + namespace ArbUt { class StringViewLiteral final : public BasicStringView { private: