Fixed assignment memory issue.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-10 14:38:01 +02:00
parent 0427aa9ef1
commit 60802341b0
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 27 additions and 7 deletions

View File

@ -19,15 +19,28 @@
\ \
inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ 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: \ public: \
name(const char* str, size_t size) : _str(nullptr), _length(size), _hash(Hash(str)) { \ name(const char* str, size_t size) : _str(new char[size + 1]), _length(size), _hash(Hash(str)) { \
_str = new char[size + 1]; \ strncpy(_str, str, size + 1); \
strcpy(_str, str); \
} \ } \
name() : name("", 0){}; \ name() : name("", 0){}; \
explicit name(const char* str) : name(str, Length(str)){}; \ explicit name(const char* str) : name(str, Length(str)){}; \
explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \ 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; } \ ~name() { delete[] _str; } \
\ \
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _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; } \ 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<char*>(str)), _length(size), _hash(Hash(str)) {} \
\
public: \ public: \
constexpr name##_Literal(const char* str, size_t size) : _str(str), _length(size), _hash(Hash(str)) {} \ 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)){}; \ explicit name##_Literal(const char* str) : name##_Literal(str, Length(str)){}; \

View File

@ -63,5 +63,15 @@ TEST_CASE("Out of scope char* doesn't lose reference", "[Utilities]") {
REQUIRE(strcmp(val.c_str(), "foobar") == 0); 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 #endif