72 lines
3.3 KiB
C++
72 lines
3.3 KiB
C++
#ifndef ARBUTILS_STRINGVIEWLITERAL_HPP
|
|
#define ARBUTILS_STRINGVIEWLITERAL_HPP
|
|
|
|
#include "BasicStringView.hpp"
|
|
#include "StringView.hpp"
|
|
|
|
namespace ArbUt {
|
|
/// \ingroup Strings
|
|
/// @brief A literal representation of a string view. Used for compile time string processing.
|
|
class StringViewLiteral final : public BasicStringView {
|
|
private:
|
|
const char* non_null _str;
|
|
|
|
public:
|
|
/// @brief Compile time initialisation of a StringViewLiteral.
|
|
/// @param str A null terminated C string.
|
|
/// @param size A The length of the string.
|
|
constexpr StringViewLiteral(const char* non_null str, size_t size) noexcept
|
|
: BasicStringView(size, Hash(str, size)), _str(str) {}
|
|
/// @brief Compile time initialisation of a StringViewLiteral. Length is calculated at compile.
|
|
/// @param str A null terminated C string.
|
|
constexpr StringViewLiteral(const char* non_null str) noexcept : StringViewLiteral(str, CalcLength(str)){};
|
|
[[nodiscard]] inline constexpr const char* non_null c_str() const noexcept final { return _str; }
|
|
[[nodiscard]] constexpr std::string_view std_str() const noexcept final {
|
|
return std::string_view(_str, _length);
|
|
}
|
|
/// @brief Returns the hash of a s.tringview literal.
|
|
/// @param s A stringview literal.
|
|
/// @return The hash of the stringview literal.
|
|
constexpr 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); }
|
|
|
|
/// @brief Check equality with standard C++ string.
|
|
inline constexpr bool operator==(const std::string_view& rhs) const noexcept final {
|
|
return _hash == Hash(rhs.data(), rhs.size());
|
|
}
|
|
/// @brief Check inequality with standard C++ string.
|
|
inline constexpr bool operator!=(const std::string_view& rhs) const noexcept final {
|
|
return _hash != Hash(rhs.data(), rhs.size());
|
|
}
|
|
/// @brief Check equality with standard C style string.
|
|
inline constexpr bool operator==(const char* non_null rhs) const noexcept final {
|
|
return _hash == Hash(rhs, CalcLength(rhs));
|
|
}
|
|
/// @brief Check inequality with standard C style string.
|
|
inline constexpr bool operator!=(const char* non_null rhs) const noexcept final {
|
|
return _hash != Hash(rhs, CalcLength(rhs));
|
|
}
|
|
};
|
|
}
|
|
|
|
namespace std {
|
|
/// @brief Helper class for getting the hash of a string view literal.
|
|
template <> struct hash<ArbUt::StringViewLiteral> {
|
|
/// @brief Returns the hash of a stringview.
|
|
/// @param s a StringView.
|
|
/// @return The hash of the StringView.
|
|
constexpr std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); }
|
|
};
|
|
}
|
|
|
|
inline constexpr ArbUt::StringViewLiteral operator"" _const_nocase(const char* non_null c, size_t l) {
|
|
return ArbUt::StringViewLiteral(c, l);
|
|
}
|
|
inline constexpr ArbUt::StringViewLiteral operator"" _cnc(const char* non_null c, size_t l) {
|
|
return ArbUt::StringViewLiteral(c, l);
|
|
}
|
|
|
|
#endif // ARBUTILS_STRINGVIEWLITERAL_HPP
|