Slight rework of StringView.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-06-27 14:02:35 +02:00
parent 030f82d34f
commit 773e765c83
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 9 additions and 4 deletions

View File

@ -27,6 +27,10 @@ namespace ArbUt {
: _value(new char[value.length() + 1]), _references(1) { : _value(new char[value.length() + 1]), _references(1) {
strncpy(_value, value.data(), value.length() + 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 { ~__ConstStringCharHolder() noexcept {
delete[] _value; delete[] _value;
@ -40,7 +44,7 @@ namespace ArbUt {
} }
inline void AddReference() noexcept { _references++; } 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) { static uint32_t constexpr Hash(char const* input) {
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * Hash(input + 1) : 5381; return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * Hash(input + 1) : 5381;
}; };
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
namespace ArbUt { namespace ArbUt {
class StringView final : public BasicStringView { class StringView final : public BasicStringView {
@ -65,6 +70,8 @@ namespace ArbUt {
public: public:
StringView(const std::string_view& str) noexcept StringView(const std::string_view& str) noexcept
: BasicStringView(str.length(), Hash(str.data())), _str(new __ConstStringCharHolder(str)) {} : 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()) { StringView() noexcept : BasicStringView(0, Hash("")), _str(GetEmptyString()) {
GetEmptyString()->AddReference(); GetEmptyString()->AddReference();
@ -87,7 +94,7 @@ namespace ArbUt {
~StringView() noexcept { _str->RemoveReference(); } ~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(); } [[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 { inline constexpr bool operator==(const std::string_view& rhs) const noexcept override {

View File

@ -5,8 +5,6 @@
#include "BasicStringView.hpp" #include "BasicStringView.hpp"
#include "StringView.hpp" #include "StringView.hpp"
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
namespace ArbUt { namespace ArbUt {
class StringViewLiteral final : public BasicStringView { class StringViewLiteral final : public BasicStringView {
private: private: