This commit is contained in:
@@ -47,19 +47,33 @@ namespace ArbUt {
|
||||
std::shared_ptr<__ConstStringCharHolder> _str = GetEmptyString();
|
||||
|
||||
public:
|
||||
/// @brief Instantiate a StringView using a C string.
|
||||
/// @param str A null-terminated C string.
|
||||
StringView(const char* str) noexcept
|
||||
: BasicStringView(CalcLength(str), Hash(str)), _str(new __ConstStringCharHolder(str, CalcLength(str))) {}
|
||||
/// @brief Instantiate a StringView using a C string, as well as it's length.
|
||||
/// @param str A null-terminated C string.
|
||||
/// @param length The length of the C string
|
||||
StringView(const char* str, size_t length) noexcept
|
||||
: BasicStringView(length, Hash(str)), _str(new __ConstStringCharHolder(str, length)) {}
|
||||
StringView() noexcept : BasicStringView(0, Hash("")) {}
|
||||
|
||||
/* Copy operators */
|
||||
/// @brief Copy operator
|
||||
/// @param other Other StringView
|
||||
StringView(const StringView& other) noexcept : BasicStringView(other._length, other._hash), _str(other._str) {}
|
||||
/// @brief Special constructor of stringview, specifically designed for use of conversion from StringViewLiteral to StringView.
|
||||
/// @param other Other StringView
|
||||
/// @param c A null-terminated C string.
|
||||
/// @param length The length of the C string.
|
||||
StringView(const BasicStringView& other, const char* c, size_t length) noexcept
|
||||
: BasicStringView(length, other.GetHash()), _str(new __ConstStringCharHolder(c, length)) {}
|
||||
|
||||
/// @brief Assignment operator.
|
||||
/// @param other Stringview to assign.
|
||||
/// @return current stringview.
|
||||
StringView& operator=(const StringView& other) noexcept {
|
||||
if (_str == other._str) {
|
||||
if (this == &other || _str == other._str) {
|
||||
return *this;
|
||||
}
|
||||
_str = other._str;
|
||||
@@ -68,7 +82,11 @@ namespace ArbUt {
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// @brief Returns null-terminated C string.
|
||||
/// @return Null-terminated C string.
|
||||
[[nodiscard]] inline const char* c_str() const noexcept override { return _str->GetValue(); }
|
||||
/// @brief Returns std string_view of internal C string.
|
||||
/// @return std::string_view.
|
||||
[[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 {
|
||||
@@ -80,14 +98,23 @@ namespace ArbUt {
|
||||
inline constexpr bool operator==(const char* rhs) const noexcept override { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* rhs) const noexcept override { return _hash != Hash(rhs); }
|
||||
|
||||
/// @brief Calculates the hash for a given C string.
|
||||
/// @param val A null-terminated C string.
|
||||
/// @return A hash of the given string.
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr uint32_t CalculateHash(const char* val) noexcept {
|
||||
return Hash(val);
|
||||
}
|
||||
|
||||
/// @brief Calculates the hash for a given std string.
|
||||
/// @param val A std string.
|
||||
/// @return A hash of the given string.
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr uint32_t
|
||||
CalculateHash(const std::string_view& val) noexcept {
|
||||
return Hash(val.data());
|
||||
}
|
||||
|
||||
/// @brief Returns an empty string.
|
||||
/// @return An empty string.
|
||||
static const StringView& EmptyString();
|
||||
};
|
||||
}
|
||||
@@ -95,6 +122,9 @@ namespace ArbUt {
|
||||
namespace std {
|
||||
/// @brief Helper class for hashing string views.
|
||||
template <> struct hash<ArbUt::StringView> {
|
||||
/// @brief Returns the hash of a stringview.
|
||||
/// @param s a StringView.
|
||||
/// @return The hash of the StringView.
|
||||
constexpr std::size_t operator()(ArbUt::StringView const& s) const noexcept { return s.GetHash(); }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,14 +13,24 @@ namespace ArbUt {
|
||||
const char* _str;
|
||||
|
||||
public:
|
||||
constexpr StringViewLiteral(const char* str, size_t size) noexcept
|
||||
/// @brief Compile time initialisation of a StringViewLiteral.
|
||||
/// @param str A null terminated C string.
|
||||
/// @param size A The length of the string.
|
||||
consteval StringViewLiteral(const char* str, size_t size) noexcept
|
||||
: BasicStringView(size, Hash(str)), _str(str) {}
|
||||
constexpr StringViewLiteral(const char* str) noexcept : StringViewLiteral(str, CalcLength(str)){};
|
||||
/// @brief Compile time initialisation of a StringViewLiteral. Length is calculated at compile.
|
||||
/// @param str A null terminated C string.
|
||||
consteval StringViewLiteral(const char* str) noexcept : StringViewLiteral(str, CalcLength(str)){};
|
||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str; }
|
||||
[[nodiscard]] constexpr std::string_view std_str() const noexcept override {
|
||||
return std::string_view(_str, _length);
|
||||
}
|
||||
constexpr std::size_t operator()(StringViewLiteral const& s) const noexcept { return s.GetHash(); }
|
||||
/// @brief Returns the hash of a s.tringview literal.
|
||||
/// @param s A stringview literal.
|
||||
/// @return The hash of the stringview literal.
|
||||
consteval std::size_t operator()(StringViewLiteral const& s) const noexcept { return s.GetHash(); }
|
||||
/// @brief Converts stringview literal into non-literal (for use during runtime).
|
||||
/// @return A normal StringView.
|
||||
inline operator StringView() const noexcept { return StringView(*this, _str, _length); }
|
||||
|
||||
inline constexpr bool operator==(const std::string_view& rhs) const noexcept override {
|
||||
@@ -37,14 +47,17 @@ namespace ArbUt {
|
||||
namespace std {
|
||||
/// @brief Helper class for getting the hash of a string view literal.
|
||||
template <> struct hash<ArbUt::StringViewLiteral> {
|
||||
constexpr std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); }
|
||||
/// @brief Returns the hash of a stringview.
|
||||
/// @param s a StringView.
|
||||
/// @return The hash of the StringView.
|
||||
consteval std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); }
|
||||
};
|
||||
}
|
||||
|
||||
inline constexpr ArbUt::StringViewLiteral operator"" _const_nocase(const char* c, size_t l) {
|
||||
inline consteval ArbUt::StringViewLiteral operator"" _const_nocase(const char* c, size_t l) {
|
||||
return ArbUt::StringViewLiteral(c, l);
|
||||
}
|
||||
inline constexpr ArbUt::StringViewLiteral operator"" _cnc(const char* c, size_t l) {
|
||||
inline consteval ArbUt::StringViewLiteral operator"" _cnc(const char* c, size_t l) {
|
||||
return ArbUt::StringViewLiteral(c, l);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user