From 083c00f85e37c8fe546a9fffe24178708ab7e3ab Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 27 Jun 2020 14:16:15 +0200 Subject: [PATCH] Changes for how Literal changes to normal StringView. --- src/String/StringView.hpp | 2 ++ src/String/StringViewLiteral.hpp | 2 +- tests/ConstStringTests.cpp | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/String/StringView.hpp b/src/String/StringView.hpp index 1ee9156..9e87e80 100644 --- a/src/String/StringView.hpp +++ b/src/String/StringView.hpp @@ -72,6 +72,8 @@ namespace ArbUt { : 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(const char* str, size_t length) noexcept + : BasicStringView(length, Hash(str)), _str(new __ConstStringCharHolder(str, length)) {} StringView() noexcept : BasicStringView(0, Hash("")), _str(GetEmptyString()) { GetEmptyString()->AddReference(); diff --git a/src/String/StringViewLiteral.hpp b/src/String/StringViewLiteral.hpp index 7c53f5e..01e14f5 100644 --- a/src/String/StringViewLiteral.hpp +++ b/src/String/StringViewLiteral.hpp @@ -17,7 +17,7 @@ namespace ArbUt { [[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; } constexpr std::string_view std_str() const noexcept { return std::string_view(_str, _length); } constexpr std::size_t operator()(StringViewLiteral const& s) const noexcept { return s.GetHash(); } - inline operator StringView() const noexcept { return StringView(std::string_view(_str, _length)); } + inline operator StringView() const noexcept { return StringView(_str, _length); } inline constexpr bool operator==(const std::string_view& rhs) const noexcept { return _hash == Hash(rhs.data()); diff --git a/tests/ConstStringTests.cpp b/tests/ConstStringTests.cpp index 3038bc0..c3a7cc7 100644 --- a/tests/ConstStringTests.cpp +++ b/tests/ConstStringTests.cpp @@ -41,4 +41,26 @@ TEST_CASE("Literal conststring to non literal, then use", "[Utilities]") { REQUIRE(strcmp(val.c_str(), "foobar") == 0); } +#ifndef WINDOWS +__attribute__((optnone)) +#endif +static ArbUt::StringView +TestCreateConstString() { + char originalVal[7]; + originalVal[0] = 'f'; + originalVal[1] = 'o'; + originalVal[2] = 'o'; + originalVal[3] = 'b'; + originalVal[4] = 'a'; + originalVal[5] = 'r'; + originalVal[6] = '\0'; + return ArbUt::StringView(originalVal); +} + +TEST_CASE("Out of scope char* doesn't lose reference", "[Utilities]") { + ArbUt::StringView val = TestCreateConstString(); + INFO(val.c_str()); + REQUIRE(strcmp(val.c_str(), "foobar") == 0); +} + #endif \ No newline at end of file