Large rework of the project, specifically the String classes.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-06-26 15:56:00 +02:00
parent 8eba3b28ff
commit abca51d331
15 changed files with 250 additions and 260 deletions

View File

@@ -0,0 +1,35 @@
#ifndef ARBUTILS_BASICSTRINGVIEW_HPP
#define ARBUTILS_BASICSTRINGVIEW_HPP
#include <string>
namespace ArbUt {
class BasicStringView {
protected:
size_t _length;
uint32_t _hash;
constexpr BasicStringView(size_t length, uint32_t hash) : _length(length), _hash(hash) {}
public:
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; }
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; }
[[nodiscard]] inline constexpr std::size_t operator()(BasicStringView const& s) const noexcept { return _hash; }
[[nodiscard]] inline constexpr operator uint32_t() const noexcept { return _hash; }
[[nodiscard]] inline constexpr bool operator==(const BasicStringView& rhs) const noexcept {
return _hash == rhs._hash;
}
inline constexpr bool operator!=(const BasicStringView& rhs) const noexcept { return _hash != rhs._hash; }
inline constexpr bool Empty() const noexcept { return Length() == 0; }
[[nodiscard]] virtual constexpr const char* c_str() const noexcept = 0;
[[nodiscard]] virtual constexpr std::string_view std_str() const noexcept = 0;
virtual constexpr bool operator==(const std::string_view& rhs) const noexcept = 0;
virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0;
virtual constexpr bool operator==(const char* rhs) const noexcept = 0;
virtual constexpr bool operator!=(const char* rhs) const noexcept = 0;
};
}
#endif // ARBUTILS_BASICSTRINGVIEW_HPP

View File

@@ -0,0 +1,4 @@
#include "StringView.hpp"
ArbUt::__ConstStringCharHolder* ArbUt::StringView::__emptyString =
new __ConstStringCharHolder(std::string_view("", 0));

104
src/String/StringView.hpp Normal file
View File

@@ -0,0 +1,104 @@
#ifndef ARBUTILS_STRINGVIEW_HPP
#define ARBUTILS_STRINGVIEW_HPP
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include "BasicStringView.hpp"
#if WINDOWS
#define STDSTRINGCONSTEXPR
#else
#define STDSTRINGCONSTEXPR constexpr
#endif
namespace ArbUt {
class __ConstStringCharHolder {
std::string_view _value;
std::atomic<size_t> _references;
__ConstStringCharHolder(const __ConstStringCharHolder& o) = delete;
__ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete;
public:
__ConstStringCharHolder(const std::string_view& value) noexcept : _value(value), _references(1) {}
inline void RemoveReference() noexcept {
if (--_references <= 0) {
delete this;
}
}
inline void AddReference() noexcept { _references++; }
inline constexpr const std::string_view& GetValue() const noexcept { return _value; }
};
}
inline static constexpr char charToLower(const char c) { return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
static uint32_t constexpr Hash(char const* input) {
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * Hash(input + 1) : 5381;
};
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
namespace ArbUt {
class StringView : public BasicStringView {
private:
static __ConstStringCharHolder* __emptyString;
static inline __ConstStringCharHolder* GetEmptyString() { return __emptyString; }
__ConstStringCharHolder* _str;
inline __ConstStringCharHolder* CloneHolder() const noexcept {
_str->AddReference();
return _str;
}
public:
StringView(const std::string_view& str) noexcept
: BasicStringView(str.length(), Hash(str.data())), _str(new __ConstStringCharHolder(str)) {}
StringView() noexcept : BasicStringView(0, Hash("")), _str(GetEmptyString()) {
GetEmptyString()->AddReference();
}
/* Copy operators */
StringView(const StringView& other) noexcept
: BasicStringView(other._length, other._hash), _str(other.CloneHolder()) {}
StringView& operator=(const StringView& other) noexcept {
if (_str == other._str) {
_str->AddReference();
return *this;
}
_str->RemoveReference();
_str = other.CloneHolder();
_length = other._length;
_hash = other._hash;
return *this;
}
~StringView() noexcept { _str->RemoveReference(); }
[[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str->GetValue().data(); }
[[nodiscard]] inline std::string_view std_str() const noexcept override { return _str->GetValue(); }
inline constexpr bool operator==(const std::string_view& rhs) const noexcept override {
return _hash == Hash(rhs.data());
}
inline constexpr bool operator!=(const std::string_view& rhs) const noexcept override {
return _hash != Hash(rhs.data());
}
inline constexpr bool operator==(const char* rhs) const noexcept override { return _hash == Hash(rhs); }
inline constexpr bool operator!=(const char* rhs) const noexcept override { return _hash != Hash(rhs); }
inline static constexpr uint32_t CalculateHash(const char* val) noexcept { return Hash(val); }
inline static constexpr uint32_t CalculateHash(const std::string_view& val) noexcept { return Hash(val.data()); }
};
}
namespace std {
template <> struct hash<ArbUt::StringView> {
constexpr std::size_t operator()(ArbUt::StringView const& s) const noexcept { return s.GetHash(); }
};
}
#endif // ARBUTILS_STRINGVIEW_HPP

View File

@@ -0,0 +1,46 @@
#ifndef ARBUTILS_STRINGVIEWLITERAL_HPP
#define ARBUTILS_STRINGVIEWLITERAL_HPP
#include <cstdint>
#include "BasicStringView.hpp"
#include "StringView.hpp"
namespace ArbUt {
class StringViewLiteral : public BasicStringView {
private:
const char* _str;
public:
constexpr StringViewLiteral(const char* str, size_t size) noexcept
: BasicStringView(size, Hash(str)), _str(str) {}
constexpr StringViewLiteral(const char* str) noexcept : StringViewLiteral(str, CalcLength(str)){};
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; }
constexpr std::string_view std_str() const noexcept { return std::string_view(_str, _length); }
constexpr std::size_t operator()(StringViewLiteral const& s) const noexcept { return s.GetHash(); }
inline operator StringView() const noexcept { return StringView(std::string_view(_str, _length)); }
inline constexpr bool operator==(const std::string_view& rhs) const noexcept {
return _hash == Hash(rhs.data());
}
inline constexpr bool operator!=(const std::string_view& rhs) const noexcept {
return _hash != Hash(rhs.data());
}
inline constexpr bool operator==(const char* rhs) const noexcept { return _hash == Hash(rhs); }
inline constexpr bool operator!=(const char* rhs) const noexcept { return _hash != Hash(rhs); }
};
}
namespace std {
template <> struct hash<ArbUt::StringViewLiteral> {
constexpr std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); }
};
}
inline constexpr ArbUt::StringViewLiteral operator"" _const_nocase(const char* c, size_t l) {
return ArbUt::StringViewLiteral(c, l);
}
inline constexpr ArbUt::StringViewLiteral operator"" _cnc(const char* c, size_t l) {
return ArbUt::StringViewLiteral(c, l);
}
#endif // ARBUTILS_STRINGVIEWLITERAL_HPP