libER 0.1.4.2
ELDEN RING API library
Loading...
Searching...
No Matches
text.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <detail/preprocessor.hpp>
12
13#include <string>
14#include <utility>
15
16// TODO: anchor vtable
17namespace from {
18// "Dantelion text" namespace
19namespace DLTX {
33 string_hash() : str(nullptr), hash_value(0), needs_hashing(true) {}
34
40 explicit string_hash(const wchar_t* str)
41 : str(str), hash_value(0), needs_hashing(true) {}
42
47 void may_change() noexcept {
48 this->needs_hashing = true;
49 }
50
58 template <typename T>
59 int get_hash(const T& str) noexcept {
60 if (this->needs_hashing)
61 this->hash_string(str);
62 return this->hash_value;
63 }
64
78 template <typename T>
79 int hash_string(const T& str) noexcept {
80 int result = 0;
81 for (int chr : str) {
82 // To lowercase (approximate)
83 if (chr <= 'Z')
84 chr += 0x20;
85 // Treat backslashes identically
86 else if (chr == '\\')
87 chr = '/';
88 result *= 137;
89 result += chr;
90 }
91 this->hash_value = result;
92 this->needs_hashing = false;
93 return result;
94 }
95
101 void set_string_ptr(const wchar_t* new_ptr) {
102 this->str = new_ptr;
103 }
104
105private:
106 const wchar_t* str;
107 int hash_value;
108 bool needs_hashing;
109};
110
119 using string_type = from::wstring;
120
121public:
122 LIBER_CLASS(FD4BasicHashString);
123
124 virtual ~FD4BasicHashString() = default;
125
132 template <typename... Args>
133 FD4BasicHashString(Args&&... args) : string(std::forward<Args>(args)...) {}
134
140 operator string_type&() noexcept {
141 this->string_hash.may_change();
142 return this->get_string();
143 }
144
150 operator const string_type&() const noexcept {
151 return this->get_string();
152 }
153
159 string_type& get_string() noexcept {
160 this->string_hash.may_change();
161 return this->string;
162 }
163
169 const string_type& get_string() const noexcept {
170 return this->string;
171 }
172
180 int get_hash() const noexcept {
181 return string_hash.get_hash(*this);
182 }
183
189 auto begin() noexcept {
190 return this->string.begin();
191 }
192
198 auto begin() const noexcept {
199 return this->string.begin();
200 }
201
207 auto end() noexcept {
208 return this->string.end();
209 }
210
216 auto end() const noexcept {
217 return this->string.end();
218 }
219
220private:
221 string_type string;
222 // May be rehashed
223 mutable string_hash string_hash;
224};
225
232using DLString = std::pair<from::wstring, bool>;
233
234LIBER_ASSERTS_BEGIN(FD4BasicHashString);
235LIBER_ASSERT_SIZE(0x40);
236LIBER_ASSERTS_END;
237} // namespace DLTX
238} // namespace from
A minimal FD4BasicHashString implementation.
Definition text.hpp:118
auto end() noexcept
Iterator end.
Definition text.hpp:207
FD4BasicHashString(Args &&... args)
Construct a new FD4BasicHashString object with from::wstring constructor arguments.
Definition text.hpp:133
auto begin() noexcept
Iterator begin.
Definition text.hpp:189
int get_hash() const noexcept
Access the hash.
Definition text.hpp:180
const string_type & get_string() const noexcept
Access the string.
Definition text.hpp:169
auto end() const noexcept
Iterator end.
Definition text.hpp:216
string_type & get_string() noexcept
Access the string.
Definition text.hpp:159
auto begin() const noexcept
Iterator const begin.
Definition text.hpp:198
from::basic_string based on std::basic_string
from::basic_string< wchar_t, std::char_traits< wchar_t >, from::default_allocator_tag > wstring
std::wstring with from::allocator.
Definition from_string.hpp:45
from::basic_string< char, std::char_traits< char >, from::default_allocator_tag > string
std::string with from::allocator.
Definition from_string.hpp:35
Simple structure that holds a string pointer and a hash of the string.
Definition text.hpp:28
string_hash()
Construct a new string hash object.
Definition text.hpp:33
string_hash(const wchar_t *str)
Construct a new string hash object.
Definition text.hpp:40
void set_string_ptr(const wchar_t *new_ptr)
Set the underlying string pointer of the hash.
Definition text.hpp:101
int get_hash(const T &str) noexcept
Get the hash of the string.
Definition text.hpp:59
void may_change() noexcept
Request a lazy rehash if the string may change.
Definition text.hpp:47
int hash_string(const T &str) noexcept
Hash the string and get the hash.
Definition text.hpp:79
std::pair< from::wstring, bool > DLString
Common Dantelion string type.
Definition text.hpp:232