diff --git a/src/__ConstStringCore.hpp b/src/__ConstStringCore.hpp index 8a241ec..cd3ef4f 100644 --- a/src/__ConstStringCore.hpp +++ b/src/__ConstStringCore.hpp @@ -19,15 +19,28 @@ \ inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ \ + name(const char* str, size_t size, uint32_t hash) : _str(new char[size + 1]), _length(size), _hash(hash) { \ + strncpy(_str, str, size + 1); \ + } \ + \ public: \ - name(const char* str, size_t size) : _str(nullptr), _length(size), _hash(Hash(str)) { \ - _str = new char[size + 1]; \ - strcpy(_str, str); \ + name(const char* str, size_t size) : _str(new char[size + 1]), _length(size), _hash(Hash(str)) { \ + strncpy(_str, str, size + 1); \ } \ name() : name("", 0){}; \ explicit name(const char* str) : name(str, Length(str)){}; \ explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \ - name(const name& other) : name(other._str, other._length) {} \ + \ + name(const name& other) : name(other._str, other._length, other._hash) {} \ + name& operator=(const name& other) { \ + delete[] _str; \ + _str = new char[other._length + 1]; \ + strncpy(_str, other._str, other._length + 1); \ + _length = other._length; \ + _hash = other._hash; \ + return *this; \ + } \ + \ ~name() { delete[] _str; } \ \ [[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; } \ @@ -66,9 +79,6 @@ \ inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ \ - constexpr explicit name##_Literal(const char* str, size_t size, bool) \ - : _str(const_cast(str)), _length(size), _hash(Hash(str)) {} \ - \ public: \ constexpr name##_Literal(const char* str, size_t size) : _str(str), _length(size), _hash(Hash(str)) {} \ explicit name##_Literal(const char* str) : name##_Literal(str, Length(str)){}; \ diff --git a/tests/ConstStringTests.cpp b/tests/ConstStringTests.cpp index e155082..d1c2032 100644 --- a/tests/ConstStringTests.cpp +++ b/tests/ConstStringTests.cpp @@ -63,5 +63,15 @@ TEST_CASE("Out of scope char* doesn't lose reference", "[Utilities]") { REQUIRE(strcmp(val.c_str(), "foobar") == 0); } +TEST_CASE("Literal conststring to non literal, then use", "[Utilities]") { + Arbutils::CaseInsensitiveConstString val; + { + val = "foobar"_cnc; + } + INFO(val.c_str()); + REQUIRE(strcmp(val.c_str(), "foobar") == 0); +} + + #endif \ No newline at end of file