diff --git a/src/strgen/strgen.h b/src/strgen/strgen.h index c57b803d28..402510485a 100644 --- a/src/strgen/strgen.h +++ b/src/strgen/strgen.h @@ -12,6 +12,7 @@ #include "../core/string_consumer.hpp" #include "../language.h" +#include "../string_type.h" #include "../3rdparty/fmt/format.h" #include @@ -41,7 +42,7 @@ struct LangString { /** Information about the currently known strings. */ struct StringData { std::vector> strings; ///< List of all known strings. - std::unordered_map> name_to_string; ///< Lookup table for the strings. + std::unordered_map, StringHash, std::equal_to<>> name_to_string; ///< Lookup table for the strings. size_t tabs; ///< The number of 'tabs' of strings. size_t max_strings; ///< The maximum number of strings. size_t next_string_id;///< The next string ID to allocate. @@ -49,7 +50,7 @@ struct StringData { StringData(size_t tabs); void FreeTranslation(); void Add(std::shared_ptr ls); - LangString *Find(const std::string &s); + LangString *Find(std::string_view s); uint32_t Version() const; size_t CountInUse(size_t tab) const; }; diff --git a/src/strgen/strgen_base.cpp b/src/strgen/strgen_base.cpp index 4a08dcafae..3430426b4a 100644 --- a/src/strgen/strgen_base.cpp +++ b/src/strgen/strgen_base.cpp @@ -100,7 +100,7 @@ void StringData::Add(std::shared_ptr ls) * @param s The string name to search on. * @return The LangString or nullptr if it is not known. */ -LangString *StringData::Find(const std::string &s) +LangString *StringData::Find(std::string_view s) { auto it = this->name_to_string.find(s); if (it == this->name_to_string.end()) return nullptr; @@ -527,7 +527,7 @@ void StringReader::HandleString(std::string_view src) } /* Check if this string already exists.. */ - LangString *ent = this->data.Find(std::string(str_name)); + LangString *ent = this->data.Find(str_name); if (this->master) { if (casep.has_value()) { diff --git a/src/string_type.h b/src/string_type.h index 345a7dcb4e..b572735dc0 100644 --- a/src/string_type.h +++ b/src/string_type.h @@ -59,4 +59,14 @@ using StringValidationSettings = EnumBitSet; /** Type for a list of strings. */ typedef std::vector StringList; +/** Helper to provide transparent hashing for string types in e.g. std::unordered_map. */ +struct StringHash { + using hash_type = std::hash; + using is_transparent = void; + + std::size_t operator()(const char *str) const { return hash_type{}(str); } + std::size_t operator()(std::string_view str) const { return hash_type{}(str); } + std::size_t operator()(const std::string &str) const { return hash_type{}(str); } +}; + #endif /* STRING_TYPE_H */