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 str()
const noexcept ->
const std::basic_string<char_type>& {
76 constexpr auto rfind(std::basic_string_view<char_type>
string)
const noexcept -> size_type {
77 return _string.rfind(
string);
80 constexpr auto substr(
const size_type position = 0,
const size_type count = npos)
const -> std::basic_string<char_type> {
81 return _string.substr(position, count);
84 constexpr operator hash_type()
const noexcept {
90 std::basic_string<char_type> _string{};
95template<
character Char,
typename Hash = std::u
int64_t,
typename HashFunction = fnv1a_hash<Char, Hash>>
97 return lhs.hash() == rhs.hash();
100using hashed_string = basic_hashed_string<char>;
102using hashed_wstring = basic_hashed_string<wchar_t>;
106inline constexpr auto operator""_hs(
const char*
string,
const std::size_t length) -> hashed_string {
107 return hashed_string{string, length};
110inline constexpr auto operator""_hs(
const wchar_t*
string,
const std::size_t length) -> hashed_wstring {
111 return hashed_wstring{string, length};
118template<sbx::utility::
character Char,
typename Hash,
typename HashFunction>
119struct fmt::formatter<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
121 template<
typename ParseContext>
122 constexpr auto parse(ParseContext& ctx) ->
decltype(ctx.begin()) {
126 template<
typename FormatContext>
128 return fmt::format_to(ctx.out(),
"{}",
string.c_str());
133template<sbx::utility::
character Char,
typename Hash,
typename HashFunction>
134struct std::hash<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
136 return string.hash();
Definition: hashed_string.hpp:15