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 str() const noexcept -> const std::basic_string<char_type>& {
73 return _string;
74 }
75
76 constexpr auto rfind(std::basic_string_view<char_type> string) const noexcept -> size_type {
77 return _string.rfind(string);
78 }
79
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);
82 }
83
84 constexpr operator hash_type() const noexcept {
85 return _hash;
86 }
87
88private:
89
90 std::basic_string<char_type> _string{};
91 hash_type _hash{};
92
93}; // class basic_hashed_string
94
95template<character Char, typename Hash = std::uint64_t, typename HashFunction = fnv1a_hash<Char, Hash>>
96constexpr auto operator==(const basic_hashed_string<Char, Hash, HashFunction>& lhs, const basic_hashed_string<Char, Hash, HashFunction>& rhs) noexcept -> bool {
97 return lhs.hash() == rhs.hash();
98}
99
100using hashed_string = basic_hashed_string<char>;
101
102using hashed_wstring = basic_hashed_string<wchar_t>;
103
104namespace literals {
105
106inline constexpr auto operator""_hs(const char* string, const std::size_t length) -> hashed_string {
107 return hashed_string{string, length};
108}
109
110inline constexpr auto operator""_hs(const wchar_t* string, const std::size_t length) -> hashed_wstring {
111 return hashed_wstring{string, length};
112}
113
114} // namespace literals
115
116} // namespace sbx::utility
117
118template<sbx::utility::character Char, typename Hash, typename HashFunction>
119struct fmt::formatter<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
120
121 template<typename ParseContext>
122 constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
123 return ctx.begin();
124 }
125
126 template<typename FormatContext>
127 auto format(const sbx::utility::basic_hashed_string<Char, Hash, HashFunction>& string, FormatContext& ctx) const -> decltype(ctx.out()) {
128 return fmt::format_to(ctx.out(), "{}", string.c_str());
129 }
130
131}; // struct fmt::formatter
132
133template<sbx::utility::character Char, typename Hash, typename HashFunction>
134struct std::hash<sbx::utility::basic_hashed_string<Char, Hash, HashFunction>> {
135 auto operator()(const sbx::utility::basic_hashed_string<Char, Hash, HashFunction>& string) const noexcept -> std::size_t {
136 return string.hash();
137 }
138}; // struct std::hash
139
140#endif // LIBSBX_UTILITY_HASHED_STRING_HPP_
Definition: hashed_string.hpp:15