Large rework of the project, specifically the String classes.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
#include "ConstString.hpp"
|
||||
|
||||
ArbUt::__ConstStringCharHolder* ArbUt::CaseInsensitiveConstString::__emptyString = new __ConstStringCharHolder("", 0);
|
||||
ArbUt::__ConstStringCharHolder* ArbUt::ConstString::__emptyString = new __ConstStringCharHolder("", 0);
|
||||
@@ -1,65 +0,0 @@
|
||||
#ifndef ARBUTILS_CONSTSTRING_HPP
|
||||
#define ARBUTILS_CONSTSTRING_HPP
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include "__ConstStringCore.hpp"
|
||||
|
||||
namespace ArbUt {
|
||||
class __ConstStringCharHolder {
|
||||
char* _value;
|
||||
std::atomic<size_t> _references;
|
||||
|
||||
__ConstStringCharHolder(const __ConstStringCharHolder& o) = delete;
|
||||
__ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete;
|
||||
|
||||
public:
|
||||
__ConstStringCharHolder(const char* value, size_t size) noexcept : _value(new char[size + 1]), _references(1) {
|
||||
strncpy(_value, value, size + 1);
|
||||
}
|
||||
|
||||
~__ConstStringCharHolder() noexcept { delete[] _value; }
|
||||
|
||||
inline void RemoveReference() noexcept {
|
||||
if (--_references <= 0) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
inline void AddReference() noexcept { _references++; }
|
||||
inline constexpr const char* GetValue() const noexcept { return _value; }
|
||||
};
|
||||
}
|
||||
|
||||
ConstStringCore(
|
||||
ConstString,
|
||||
inline static uint32_t constexpr Hash(char const* input) {
|
||||
return (*input) ? static_cast<uint32_t>((*input)) + 33 * Hash(input + 1) : 5381;
|
||||
};)
|
||||
|
||||
inline constexpr ArbUt::ConstString_Literal
|
||||
operator"" _const(const char* c, size_t l) {
|
||||
return ArbUt::ConstString_Literal(c, l);
|
||||
}
|
||||
inline constexpr ArbUt::ConstString_Literal operator"" _c(const char* c, size_t l) {
|
||||
return ArbUt::ConstString_Literal(c, l);
|
||||
}
|
||||
|
||||
ConstStringCore(
|
||||
CaseInsensitiveConstString,
|
||||
inline static constexpr char charToLower(const char c) {
|
||||
return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
|
||||
} inline static uint32_t constexpr Hash(char const* input) {
|
||||
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * Hash(input + 1) : 5381;
|
||||
};);
|
||||
|
||||
inline constexpr ArbUt::CaseInsensitiveConstString_Literal operator"" _const_nocase(const char* c, size_t l) {
|
||||
return ArbUt::CaseInsensitiveConstString_Literal(c, l);
|
||||
}
|
||||
inline constexpr ArbUt::CaseInsensitiveConstString_Literal operator"" _cnc(const char* c, size_t l) {
|
||||
return ArbUt::CaseInsensitiveConstString_Literal(c, l);
|
||||
}
|
||||
|
||||
#endif // ARBUTILS_CONSTSTRING_HPP
|
||||
12
src/Enum.hpp
12
src/Enum.hpp
@@ -4,12 +4,13 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include "MacroUtils.hpp"
|
||||
#include "StringView.hpp"
|
||||
|
||||
#define ENUM_VALUE(x, value) x = value,
|
||||
#
|
||||
#define ENUM_CASE(x, name) \
|
||||
case name::x: \
|
||||
return #x;
|
||||
return ArbUt::StringViewLiteral(#x);
|
||||
#
|
||||
#define ENUM_PARSE_CASE(x, name) \
|
||||
case ConstHash(#x): \
|
||||
@@ -64,14 +65,9 @@
|
||||
} \
|
||||
\
|
||||
public: \
|
||||
constexpr static const char* ToString(name value) noexcept { \
|
||||
constexpr static ArbUt::StringViewLiteral ToString(name value) noexcept { \
|
||||
switch (value) { MACRO_UTILS_FOR_EACH(ENUM_CASE, name, values) } \
|
||||
/*If we haven't found a value, we want to stringify the number*/ \
|
||||
auto v = static_cast<type>(value); \
|
||||
auto size = (int)((ceil(log10(v)) + 1) * sizeof(char)); \
|
||||
char* snum = new char[size + 1]; \
|
||||
sprintf(snum, "%d", v); \
|
||||
return snum; \
|
||||
return "out of bounds"_cnc; \
|
||||
} \
|
||||
constexpr static name Parse(const char* input, bool caseInsensitive = false) { \
|
||||
if (caseInsensitive) \
|
||||
|
||||
35
src/String/BasicStringView.hpp
Normal file
35
src/String/BasicStringView.hpp
Normal 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
|
||||
4
src/String/StringView.cpp
Normal file
4
src/String/StringView.cpp
Normal 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
104
src/String/StringView.hpp
Normal 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
|
||||
46
src/String/StringViewLiteral.hpp
Normal file
46
src/String/StringViewLiteral.hpp
Normal 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
|
||||
6
src/StringView.hpp
Normal file
6
src/StringView.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "String/StringView.hpp"
|
||||
#include "String/StringViewLiteral.hpp"
|
||||
|
||||
namespace ArbUt {
|
||||
[[deprecated("Moved to StringView")]] typedef StringView CaseInsensitiveConstString;
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
#ifndef ARBUTILS_CONSTSTRINGCORE_HPP
|
||||
#define ARBUTILS_CONSTSTRINGCORE_HPP
|
||||
|
||||
#if WINDOWS
|
||||
#define STDSTRINGCONSTEXPR
|
||||
#else
|
||||
#define STDSTRINGCONSTEXPR constexpr
|
||||
#endif
|
||||
|
||||
#define ConstStringCore(name, hashFunction) \
|
||||
namespace ArbUt { \
|
||||
class name { \
|
||||
private: \
|
||||
__ConstStringCharHolder* _str; \
|
||||
size_t _length; \
|
||||
uint32_t _hash; \
|
||||
hashFunction; \
|
||||
\
|
||||
static __ConstStringCharHolder* __emptyString; \
|
||||
static inline __ConstStringCharHolder* GetEmptyString() { return __emptyString; } \
|
||||
\
|
||||
inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \
|
||||
inline __ConstStringCharHolder* CloneHolder() const noexcept { \
|
||||
_str->AddReference(); \
|
||||
return _str; \
|
||||
} \
|
||||
\
|
||||
public: \
|
||||
name(const char* str, size_t size) noexcept \
|
||||
: _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) {} \
|
||||
\
|
||||
name() noexcept : _str(GetEmptyString()), _length(0), _hash(Hash("")) { \
|
||||
GetEmptyString()->AddReference(); \
|
||||
}; \
|
||||
explicit name(const char* str) noexcept : name(str, Length(str)){}; \
|
||||
explicit name(const std::string& str) noexcept : name(str.c_str(), str.size()){}; \
|
||||
\
|
||||
/* Copy operators */ \
|
||||
name(const name& other) noexcept \
|
||||
: _str(other.CloneHolder()), _length(other._length), _hash(other._hash) {} \
|
||||
name& operator=(const name& other) noexcept { \
|
||||
if (_str == other._str) { \
|
||||
_str->AddReference(); \
|
||||
return *this; \
|
||||
} \
|
||||
_str->RemoveReference(); \
|
||||
_str = other.CloneHolder(); \
|
||||
_length = other._length; \
|
||||
_hash = other._hash; \
|
||||
return *this; \
|
||||
} \
|
||||
\
|
||||
~name() noexcept { _str->RemoveReference(); } \
|
||||
\
|
||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str->GetValue(); } \
|
||||
[[nodiscard]] inline std::string std_str() const noexcept { \
|
||||
return std::string(_str->GetValue(), _length); \
|
||||
} \
|
||||
\
|
||||
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \
|
||||
\
|
||||
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; } \
|
||||
inline constexpr bool Empty() const noexcept { return _length == 0; } \
|
||||
\
|
||||
constexpr std::size_t operator()(name const& s) const noexcept { return s.GetHash(); } \
|
||||
inline constexpr operator uint32_t() const noexcept { return _hash; } \
|
||||
\
|
||||
inline constexpr bool operator==(const name& rhs) const noexcept { return _hash == rhs._hash; } \
|
||||
inline constexpr bool operator!=(const name& rhs) const noexcept { return _hash != rhs._hash; } \
|
||||
inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const noexcept { \
|
||||
return _hash == Hash(rhs.c_str()); \
|
||||
} \
|
||||
inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const noexcept { \
|
||||
return _hash != Hash(rhs.c_str()); \
|
||||
} \
|
||||
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); } \
|
||||
\
|
||||
inline static constexpr uint32_t GetHash(const char* val) noexcept { return Hash(val); } \
|
||||
inline static STDSTRINGCONSTEXPR uint32_t GetHash(const std::string& val) noexcept { \
|
||||
return Hash(val.c_str()); \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
class name##_Literal { \
|
||||
private: \
|
||||
const char* _str; \
|
||||
size_t _length; \
|
||||
uint32_t _hash; \
|
||||
\
|
||||
hashFunction; \
|
||||
\
|
||||
inline static int constexpr Length(const char* str) noexcept { return *str ? 1 + Length(str + 1) : 0; } \
|
||||
\
|
||||
public: \
|
||||
constexpr name##_Literal(const char* str, size_t size) noexcept \
|
||||
: _str(str), _length(size), _hash(Hash(str)) {} \
|
||||
explicit name##_Literal(const char* str) noexcept : name##_Literal(str, Length(str)){}; \
|
||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; } \
|
||||
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \
|
||||
\
|
||||
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; } \
|
||||
inline constexpr bool Empty() const noexcept { return _length == 0; } \
|
||||
\
|
||||
constexpr std::size_t operator()(name##_Literal const& s) const noexcept { return s.GetHash(); } \
|
||||
inline constexpr operator uint32_t() const noexcept { return _hash; } \
|
||||
inline operator name() const noexcept { return name(_str, _length); } \
|
||||
\
|
||||
inline constexpr bool operator==(const name##_Literal& rhs) const noexcept { return _hash == rhs._hash; } \
|
||||
inline constexpr bool operator!=(const name##_Literal& rhs) const noexcept { return _hash != rhs._hash; } \
|
||||
inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const noexcept { \
|
||||
return _hash == Hash(rhs.c_str()); \
|
||||
} \
|
||||
inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const noexcept { \
|
||||
return _hash != Hash(rhs.c_str()); \
|
||||
} \
|
||||
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::name> { \
|
||||
constexpr std::size_t operator()(ArbUt::name const& s) const noexcept { return s.GetHash(); } \
|
||||
}; \
|
||||
template <> struct hash<ArbUt::name##_Literal> { \
|
||||
constexpr std::size_t operator()(ArbUt::name##_Literal const& s) const noexcept { return s.GetHash(); } \
|
||||
}; \
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user