sandbox
Loading...
Searching...
No Matches
hashed_string.hpp
1#ifndef LIBSBX_UTILITY_HASHED_STRING_HPP_
2#define LIBSBX_UTILITY_HASHED_STRING_HPP_
3
4#include <concepts>
5#include <cinttypes>
6#include <string>
7
9
10#include <fmt/format.h>
11
12namespace sbx::utility {
13
14template<character Char, typename Hash = std::uint64_t, typename HashFunction = fnv1a_hash<Char, Hash>>
16
17public:
18
19 using char_type = HashFunction::char_type;
20 using size_type = HashFunction::size_type;
21 using hash_type = HashFunction::hash_type;
22
23 inline static constexpr auto npos = std::basic_string<char_type>::npos;
24
25 constexpr basic_hashed_string()
26 : _string{},
27 _hash{} {}
28
29 constexpr basic_hashed_string(const char_type* string, const size_type length)
30 : _string{string, length},
31 _hash{HashFunction{}(_string)} {}
32
33 template<std::size_t Size>
34 constexpr basic_hashed_string(const char_type (&string)[Size])
35 : _string{string, Size - 1},
36 _hash{HashFunction{}(_string)} {}
37
38 constexpr basic_hashed_string(const std::basic_string<char_type>& string)
39 : _string{string},
40 _hash{HashFunction{}(_string)} {}
41
42 constexpr basic_hashed_string(const basic_hashed_string& other) = default;
43
44 constexpr basic_hashed_string(basic_hashed_string&& other) noexcept = default;
45
46 constexpr ~basic_hashed_string() = default;
47
48 constexpr auto operator=(const basic_hashed_string& other) -> basic_hashed_string& = default;
49
50 constexpr auto operator=(basic_hashed_string&& other) noexcept -> basic_hashed_string& = default;
51
52 constexpr auto operator==(const basic_hashed_string& other) const noexcept -> bool {
53 return _hash == other._hash;
54 }
55
56 constexpr auto data() const noexcept -> const char_type* {
57 return _string.data();
58 }
59
60 constexpr auto size() const noexcept -> size_type {
61 return _string.size();
62 }
63
64 constexpr auto hash() const noexcept -> hash_type {
65 return _hash;
66 }
67
68 constexpr auto c_str() const noexcept -> const char_type* {
69 return _string.c_str();
70 }
71
72 constexpr auto is_empty() const noexcept -> bool {
73 return _string.empty();
74 }
75
76 constexpr auto str() const noexcept -> const std::basic_string<char_type>& {
77 return _string;
78 }
79
80 constexpr auto rfind(std::basic_string_view<char_type> string) const noexcept -> size_type {
81 return _string.rfind(string);
82 }
83
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);
86 }
87
88 constexpr operator hash_type() const noexcept {
89 return _hash;
90 }
91
92private:
93
94 std::basic_string<char_type> _string{};
95 hash_type _hash{};
96
97}; // class basic_hashed_string
98
99template<character Char, typename Hash = std::uint64_t, typename HashFunction = fnv1a_hash<Char, Hash>>
100constexpr auto operator==(const basic_hashed_string<Char, Hash, HashFunction>& lhs, const basic_hashed_string<Char, Hash, HashFunction>& rhs) noexcept -> bool {
101 return lhs.hash() == rhs.hash();
102}
103
104using hashed_string = basic_hashed_string<char>;
105
106using hashed_wstring = basic_hashed_string<wchar_t>;
107
108namespace literals {
109
110inline constexpr auto operator""_hs(const char* string, const std::size_t length) -> hashed_string {
111 return hashed_string{string, length};
112}
113
114inline constexpr auto operator""_hs(const wchar_t* string, const std::size_t length) -> hashed_wstring {
115 return hashed_wstring{string, length};
116}
117
118} // namespace literals
119
120} // namespace sbx::utility
121
122template<sbx::utility::character Char, typename Hash, typename HashFunction>
123struct fmt::formatter<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
124
125 template<typename ParseContext>
126 constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
127 return ctx.begin();
128 }
129
130 template<typename FormatContext>
131 auto format(const sbx::utility::basic_hashed_string<Char, Hash, HashFunction>& string, FormatContext& ctx) const -> decltype(ctx.out()) {
132 return fmt::format_to(ctx.out(), "{}", string.c_str());
133 }
134
135}; // struct fmt::formatter
136
137template<sbx::utility::character Char, typename Hash, typename HashFunction>
138struct std::hash<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
139 auto operator()(const sbx::utility::basic_hashed_string<Char, Hash, HashFunction>& string) const noexcept -> std::size_t {
140 return string.hash();
141 }
142}; // struct std::hash
143
144#endif // LIBSBX_UTILITY_HASHED_STRING_HPP_
Definition: hashed_string.hpp:15