From 773e765c832637c9f806cb275caa14d248204d7c Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 27 Jun 2020 14:02:35 +0200 Subject: [PATCH] Slight rework of StringView. --- src/String/StringView.hpp | 11 +++++++++-- src/String/StringViewLiteral.hpp | 2 -- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/String/StringView.hpp b/src/String/StringView.hpp index 7e33892..1ee9156 100644 --- a/src/String/StringView.hpp +++ b/src/String/StringView.hpp @@ -27,6 +27,10 @@ namespace ArbUt { : _value(new char[value.length() + 1]), _references(1) { strncpy(_value, value.data(), value.length() + 1); } + __ConstStringCharHolder(const char* value, size_t length) noexcept + : _value(new char[length + 1]), _references(1) { + strncpy(_value, value, length + 1); + } ~__ConstStringCharHolder() noexcept { delete[] _value; @@ -40,7 +44,7 @@ namespace ArbUt { } inline void AddReference() noexcept { _references++; } - inline constexpr std::string_view GetValue() const noexcept { return std::string_view(_value); } + inline constexpr const char* GetValue() const noexcept { return _value; } }; } @@ -48,6 +52,7 @@ 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 { @@ -65,6 +70,8 @@ namespace ArbUt { public: StringView(const std::string_view& str) noexcept : BasicStringView(str.length(), Hash(str.data())), _str(new __ConstStringCharHolder(str)) {} + StringView(const char* str) noexcept + : BasicStringView(CalcLength(str), Hash(str)), _str(new __ConstStringCharHolder(str, CalcLength(str))) {} StringView() noexcept : BasicStringView(0, Hash("")), _str(GetEmptyString()) { GetEmptyString()->AddReference(); @@ -87,7 +94,7 @@ namespace ArbUt { ~StringView() noexcept { _str->RemoveReference(); } - [[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str->GetValue().data(); } + [[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str->GetValue(); } [[nodiscard]] inline std::string_view std_str() const noexcept override { return _str->GetValue(); } inline constexpr bool operator==(const std::string_view& rhs) const noexcept override { diff --git a/src/String/StringViewLiteral.hpp b/src/String/StringViewLiteral.hpp index c988540..7c53f5e 100644 --- a/src/String/StringViewLiteral.hpp +++ b/src/String/StringViewLiteral.hpp @@ -5,8 +5,6 @@ #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: