1 #ifndef ENTT_CORE_HASHED_STRING_HPP 2 #define ENTT_CORE_HASHED_STRING_HPP 6 #include "../config/config.h" 26 struct fnv1a_traits<std::uint32_t> {
27 static constexpr std::uint32_t offset = 2166136261;
28 static constexpr std::uint32_t prime = 16777619;
33 struct fnv1a_traits<std::uint64_t> {
34 static constexpr std::uint64_t offset = 14695981039346656037ull;
35 static constexpr std::uint64_t prime = 1099511628211ull;
58 using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
60 struct const_wrapper {
62 constexpr const_wrapper(
const char *curr) ENTT_NOEXCEPT: str{curr} {}
67 inline static constexpr ENTT_ID_TYPE helper(ENTT_ID_TYPE partial,
const char *curr) ENTT_NOEXCEPT {
68 return curr[0] == 0 ? partial : helper((partial^curr[0])*traits_type::prime, curr+1);
90 template<std::
size_t N>
92 return helper(traits_type::offset, str);
101 return helper(traits_type::offset, wrapper.str);
111 ENTT_ID_TYPE partial{traits_type::offset};
112 while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
118 : str{
nullptr}, hash{}
135 template<std::
size_t N>
137 : str{curr}, hash{helper(traits_type::offset, curr)}
146 : str{wrapper.str}, hash{helper(traits_type::offset, wrapper.str)}
153 constexpr
const char *
data() const ENTT_NOEXCEPT {
169 constexpr
operator const char *()
const ENTT_NOEXCEPT {
return str; }
172 constexpr
operator hash_type() const ENTT_NOEXCEPT {
return hash; }
180 return hash == other.hash;
196 return !(lhs == rhs);
208 constexpr
entt::hashed_string operator"" ENTT_HS_SUFFIX(
const char *str, std::size_t) ENTT_NOEXCEPT {
213 #endif // ENTT_CORE_HASHED_STRING_HPP static hash_type to_value(const char *str, std::size_t size) ENTT_NOEXCEPT
Returns directly the numeric representation of a string view.
Zero overhead unique identifier.
constexpr hashed_string() ENTT_NOEXCEPT
Constructs an empty hashed string.
static constexpr hash_type to_value(const char(&str)[N]) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
constexpr hashed_string(const char(&curr)[N]) ENTT_NOEXCEPT
Constructs a hashed string from an array of const chars.
constexpr bool operator==(const hashed_string &other) const ENTT_NOEXCEPT
Compares two hashed strings.
static hash_type to_value(const_wrapper wrapper) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
constexpr hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
Explicit constructor on purpose to avoid constructing a hashed string directly from a const char *...
constexpr hash_type value() const ENTT_NOEXCEPT
Returns the numeric representation of a hashed string.
constexpr const char * data() const ENTT_NOEXCEPT
Returns the human-readable representation of a hashed string.
ENTT_ID_TYPE hash_type
Unsigned integer type.
constexpr bool operator!=(const hashed_string &lhs, const hashed_string &rhs) ENTT_NOEXCEPT
Compares two hashed strings.