71 lines
1.8 KiB
C++
71 lines
1.8 KiB
C++
|
|
#ifndef PORYGONLANG_HASHEDSTRING_HPP
|
|
#define PORYGONLANG_HASHEDSTRING_HPP
|
|
|
|
#include <string>
|
|
|
|
namespace Porygon::Utilities{
|
|
class HashedString{
|
|
const uint32_t _hash;
|
|
const std::u16string _string;
|
|
public:
|
|
explicit HashedString(const std::u16string& s)
|
|
: _hash(ConstHash(s.c_str())),
|
|
_string(s)
|
|
{
|
|
}
|
|
explicit HashedString(char16_t const *input)
|
|
: _hash(ConstHash(input)),
|
|
_string(std::u16string(input))
|
|
{
|
|
}
|
|
|
|
explicit HashedString(char const *input) : _hash(ConstHash(input)){
|
|
}
|
|
|
|
explicit HashedString(uint32_t hash) : _hash(hash){
|
|
}
|
|
|
|
HashedString(const HashedString& b) :_hash(b._hash), _string(b._string){
|
|
|
|
};
|
|
|
|
static uint32_t constexpr ConstHash(char16_t const *input) {
|
|
return *input ?
|
|
static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) :
|
|
5381;
|
|
}
|
|
|
|
static uint32_t constexpr ConstHash(char const *input) {
|
|
return *input ?
|
|
static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) :
|
|
5381;
|
|
}
|
|
|
|
const uint32_t GetHash() const{
|
|
return _hash;
|
|
}
|
|
|
|
const std::u16string* GetString() const{
|
|
return &_string;
|
|
}
|
|
|
|
bool operator==(const HashedString& b) const{
|
|
return _hash == b._hash;
|
|
}
|
|
bool operator!=(const HashedString& b) const{
|
|
return _hash != b._hash;
|
|
}
|
|
bool operator<(const HashedString& b) const{
|
|
return _hash < b._hash;
|
|
}
|
|
bool operator>(const HashedString& b) const{
|
|
return _hash > b._hash;
|
|
}
|
|
|
|
|
|
};
|
|
}
|
|
|
|
#endif //PORYGONLANG_HASHEDSTRING_HPP
|