1#ifndef LIBSBX_UTILITY_HASHED_STRING_HPP_
2#define LIBSBX_UTILITY_HASHED_STRING_HPP_
10#include <fmt/format.h>
12namespace sbx::utility {
14template<
character Char,
typename Hash = std::u
int64_t,
typename HashFunction = fnv1a_hash<Char, Hash>>
19 using char_type = HashFunction::char_type;
20 using size_type = HashFunction::size_type;
21 using hash_type = HashFunction::hash_type;
23 inline static constexpr auto npos = std::basic_string<char_type>::npos;
30 : _string{string, length},
31 _hash{HashFunction{}(_string)} {}
33 template<std::
size_t Size>
35 : _string{string, Size - 1},
36 _hash{HashFunction{}(_string)} {}
40 _hash{HashFunction{}(_string)} {}
53 return _hash == other._hash;
56 constexpr auto data()
const noexcept ->
const char_type* {
57 return _string.data();
60 constexpr auto size()
const noexcept -> size_type {
61 return _string.size();
64 constexpr auto hash()
const noexcept -> hash_type {
68 constexpr auto c_str()
const noexcept ->
const char_type* {
69 return _string.c_str();
72 constexpr auto is_empty()
const noexcept ->
bool {
73 return _string.empty();
76 constexpr auto str()
const noexcept ->
const std::basic_string<char_type>& {
80 constexpr auto rfind(std::basic_string_view<char_type>
string)
const noexcept -> size_type {
81 return _string.rfind(
string);
84 constexpr auto substr(
const size_type position = 0,
const size_type count = npos)
const -> std::basic_string<char_type> {
85 return _string.substr(position, count);
88 constexpr operator hash_type()
const noexcept {
94 std::basic_string<char_type> _string{};
99template<
character Char,
typename Hash = std::u
int64_t,
typename HashFunction = fnv1a_hash<Char, Hash>>
101 return lhs.hash() == rhs.hash();
104using hashed_string = basic_hashed_string<char>;
106using hashed_wstring = basic_hashed_string<wchar_t>;
110inline constexpr auto operator""_hs(
const char*
string,
const std::size_t length) -> hashed_string {
111 return hashed_string{string, length};
114inline constexpr auto operator""_hs(
const wchar_t*
string,
const std::size_t length) -> hashed_wstring {
115 return hashed_wstring{string, length};
122template<sbx::utility::
character Char,
typename Hash,
typename HashFunction>
123struct fmt::formatter<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
125 template<
typename ParseContext>
126 constexpr auto parse(ParseContext& ctx) ->
decltype(ctx.begin()) {
130 template<
typename FormatContext>
132 return fmt::format_to(ctx.out(),
"{}",
string.c_str());
137template<sbx::utility::
character Char,
typename Hash,
typename HashFunction>
138struct std::hash<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
140 return string.hash();
Definition: hashed_string.hpp:15