Reduce amount of copies for HashedString for improved performance
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: Deukhoofd <deukhoofd@gmail.com>
This commit is contained in:
@@ -3,32 +3,33 @@
|
||||
#define PORYGONLANG_HASHEDSTRING_HPP
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace Porygon::Utilities{
|
||||
class HashedString{
|
||||
const uint32_t _hash;
|
||||
const std::u16string _string;
|
||||
const std::shared_ptr<const std::u16string> _string;
|
||||
|
||||
explicit HashedString(const std::u16string& s) : _hash(ConstHash(s.c_str())),_string(nullptr){
|
||||
}
|
||||
|
||||
explicit HashedString(uint32_t hash) : _hash(hash), _string(nullptr){
|
||||
}
|
||||
|
||||
public:
|
||||
explicit HashedString(const std::u16string& s)
|
||||
: _hash(ConstHash(s.c_str())),
|
||||
_string(s)
|
||||
explicit HashedString(const std::u16string* s)
|
||||
: _hash(ConstHash(s->c_str())),
|
||||
_string(std::shared_ptr<const std::u16string>(s))
|
||||
{
|
||||
}
|
||||
explicit HashedString(char16_t const *input)
|
||||
: _hash(ConstHash(input)),
|
||||
_string(std::u16string(input))
|
||||
{
|
||||
static HashedString CreateLookup(const std::u16string& s){
|
||||
return HashedString(s);
|
||||
}
|
||||
static HashedString CreateLookup(uint32_t hash){
|
||||
return HashedString(hash);
|
||||
}
|
||||
|
||||
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){
|
||||
|
||||
};
|
||||
HashedString(const HashedString& b) = default;
|
||||
|
||||
static uint32_t constexpr ConstHash(char16_t const *input) {
|
||||
return *input ?
|
||||
@@ -46,8 +47,8 @@ namespace Porygon::Utilities{
|
||||
return _hash;
|
||||
}
|
||||
|
||||
const std::u16string* GetString() const{
|
||||
return &_string;
|
||||
const std::shared_ptr<const std::u16string> GetString() const{
|
||||
return _string;
|
||||
}
|
||||
|
||||
bool operator==(const HashedString& b) const{
|
||||
|
||||
5
src/Utilities/StringUtils.cpp
Normal file
5
src/Utilities/StringUtils.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace Porygon::Utilities{
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, char16_t> StringUtils::conv;
|
||||
}
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
namespace Porygon::Utilities{
|
||||
class StringUtils{
|
||||
static std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, char16_t> conv;
|
||||
public:
|
||||
static std::u16string IntToString(long const &i) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, char16_t> conv;
|
||||
return conv.from_bytes(std::to_string(i));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user