diff --git a/src/__ConstStringCore.hpp b/src/__ConstStringCore.hpp index 1866989..87c4d19 100644 --- a/src/__ConstStringCore.hpp +++ b/src/__ConstStringCore.hpp @@ -11,9 +11,11 @@ namespace Arbutils { \ class name { \ private: \ - __ConstStringCharHolder* _str = nullptr; \ + __ConstStringCharHolder* _str; \ size_t _length; \ uint32_t _hash; \ + hashFunction; \ + \ static __ConstStringCharHolder* __emptyString; \ static inline __ConstStringCharHolder* GetEmptyString() { \ if (__emptyString == nullptr) \ @@ -21,13 +23,12 @@ return __emptyString; \ } \ \ - hashFunction; \ - \ inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ \ public: \ name(const char* str, size_t size) \ : _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) {} \ + \ name() : _str(GetEmptyString()), _length(0), _hash(Hash("")) { GetEmptyString()->AddReference(); }; \ explicit name(const char* str) : name(str, Length(str)){}; \ explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \ @@ -35,8 +36,9 @@ _str->AddReference(); \ } \ name& operator=(const name& other) { \ - if (_str != nullptr) \ - _str->RemoveReference(); \ + if (_str == other._str) \ + return *this; \ + _str->RemoveReference(); \ _str = other._str; \ _str->AddReference(); \ _length = other._length; \ @@ -44,10 +46,7 @@ return *this; \ } \ \ - ~name() { \ - if (_str != nullptr) \ - _str->RemoveReference(); \ - } \ + ~name() { _str->RemoveReference(); } \ \ [[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str->GetValue(); } \ [[nodiscard]] inline std::string std_str() const { return std::string(_str->GetValue(), _length); } \