Expand on the use of defines and non_null/nullable
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include "../Defines.hpp"
|
||||
|
||||
/// \defgroup Strings Strings
|
||||
/// \brief Group of non-editable strings with faster hashing.
|
||||
@@ -14,20 +15,20 @@ namespace ArbUt {
|
||||
class BasicStringView {
|
||||
protected:
|
||||
size_t _length = 0;
|
||||
uint32_t _hash = 0;
|
||||
u32 _hash = 0;
|
||||
|
||||
/// @brief Construct a basic string view.
|
||||
constexpr BasicStringView(size_t length, uint32_t hash) : _length(length), _hash(hash) {}
|
||||
constexpr BasicStringView(size_t length, u32 hash) : _length(length), _hash(hash) {}
|
||||
|
||||
public:
|
||||
/// @brief The amount of characters of the string.
|
||||
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; }
|
||||
/// @brief The unique hash of the string.
|
||||
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; }
|
||||
[[nodiscard]] inline constexpr u32 GetHash() const noexcept { return _hash; }
|
||||
/// @brief The unique hash of the string.
|
||||
[[nodiscard]] inline constexpr std::size_t operator()() const noexcept { return _hash; }
|
||||
/// @brief The unique hash of the string.
|
||||
[[nodiscard]] inline constexpr operator uint32_t() const noexcept { return _hash; }
|
||||
[[nodiscard]] inline constexpr operator u32() const noexcept { return _hash; }
|
||||
/// @brief Check whether two StringViews are equal
|
||||
[[nodiscard]] inline constexpr bool operator==(const BasicStringView& rhs) const noexcept {
|
||||
return _hash == rhs._hash;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include "../Ensure.hpp"
|
||||
#include "BasicStringView.hpp"
|
||||
|
||||
#if WINDOWS
|
||||
@@ -13,21 +14,22 @@
|
||||
|
||||
namespace ArbUt {
|
||||
class __ConstStringCharHolder {
|
||||
char* _value = 0;
|
||||
char* non_null _value = 0;
|
||||
|
||||
__ConstStringCharHolder(const __ConstStringCharHolder& o) = delete;
|
||||
__ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete;
|
||||
|
||||
public:
|
||||
__ConstStringCharHolder(const char* value, size_t length) noexcept : _value(new char[length + 1]) {
|
||||
__ConstStringCharHolder(const char* non_null value, size_t length) : _value(new char[length + 1]) {
|
||||
EnsureNotNull(value);
|
||||
strncpy(_value, value, length + 1);
|
||||
}
|
||||
~__ConstStringCharHolder() noexcept { delete[] _value; }
|
||||
inline constexpr const char* GetValue() const noexcept { return _value; }
|
||||
inline constexpr const char* non_null GetValue() const noexcept { return _value; }
|
||||
};
|
||||
}
|
||||
|
||||
constexpr uint32_t crc_table[256] = {
|
||||
constexpr u32 crc_table[256] = {
|
||||
0, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832,
|
||||
0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
|
||||
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A,
|
||||
@@ -59,14 +61,14 @@ constexpr uint32_t crc_table[256] = {
|
||||
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};
|
||||
|
||||
inline static constexpr char charToLower(const char c) { return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
|
||||
static uint32_t constexpr Hash(std::string_view input) {
|
||||
uint32_t crc = 0xffffffff;
|
||||
static u32 constexpr Hash(std::string_view input) {
|
||||
u32 crc = 0xffffffff;
|
||||
for (auto c : input) {
|
||||
crc = (crc >> 8) ^ crc_table[(crc ^ charToLower(c)) & 0xff];
|
||||
}
|
||||
return crc ^ 0xffffffff;
|
||||
};
|
||||
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
|
||||
static int constexpr CalcLength(const char* non_null str) { return *str ? 1 + CalcLength(str + 1) : 0; }
|
||||
|
||||
namespace ArbUt {
|
||||
/// \ingroup Strings
|
||||
@@ -81,12 +83,12 @@ namespace ArbUt {
|
||||
public:
|
||||
/// @brief Instantiate a StringView using a C string.
|
||||
/// @param str A null-terminated C string.
|
||||
StringView(const char* str) noexcept
|
||||
StringView(const char* non_null str)
|
||||
: BasicStringView(CalcLength(str), Hash(str)), _str(new __ConstStringCharHolder(str, CalcLength(str))) {}
|
||||
/// @brief Instantiate a StringView using a C string, as well as it's length.
|
||||
/// @param str A null-terminated C string.
|
||||
/// @param length The length of the C string
|
||||
StringView(const char* str, size_t length) noexcept
|
||||
StringView(const char* non_null str, size_t length)
|
||||
: BasicStringView(length, Hash(str)), _str(new __ConstStringCharHolder(str, length)) {}
|
||||
StringView() noexcept : BasicStringView(0, Hash("")) {}
|
||||
|
||||
@@ -99,7 +101,7 @@ namespace ArbUt {
|
||||
/// @param other Other StringView
|
||||
/// @param c A null-terminated C string.
|
||||
/// @param length The length of the C string.
|
||||
StringView(const BasicStringView& other, const char* c, size_t length) noexcept
|
||||
StringView(const BasicStringView& other, const char* non_null c, size_t length) noexcept
|
||||
: BasicStringView(length, other.GetHash()), _str(new __ConstStringCharHolder(c, length)) {}
|
||||
|
||||
/// @brief Assignment operator.
|
||||
@@ -117,7 +119,7 @@ namespace ArbUt {
|
||||
|
||||
/// @brief Returns null-terminated C string.
|
||||
/// @return Null-terminated C string.
|
||||
[[nodiscard]] inline const char* c_str() const noexcept final { return _str->GetValue(); }
|
||||
[[nodiscard]] inline const char* non_null c_str() const noexcept final { return _str->GetValue(); }
|
||||
/// @brief Returns std string_view of internal C string.
|
||||
/// @return std::string_view.
|
||||
[[nodiscard]] inline std::string_view std_str() const noexcept final { return _str->GetValue(); }
|
||||
@@ -128,21 +130,20 @@ namespace ArbUt {
|
||||
inline constexpr bool operator!=(const std::string_view& rhs) const noexcept final {
|
||||
return _hash != Hash(rhs.data());
|
||||
}
|
||||
inline constexpr bool operator==(const char* rhs) const noexcept final { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* rhs) const noexcept final { return _hash != Hash(rhs); }
|
||||
inline constexpr bool operator==(const char* non_null rhs) const noexcept final { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* non_null rhs) const noexcept final { return _hash != Hash(rhs); }
|
||||
|
||||
/// @brief Calculates the hash for a given C string.
|
||||
/// @param val A null-terminated C string.
|
||||
/// @return A hash of the given string.
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr uint32_t CalculateHash(const char* val) noexcept {
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr u32 CalculateHash(const char* non_null val) noexcept {
|
||||
return Hash(val);
|
||||
}
|
||||
|
||||
/// @brief Calculates the hash for a given std string.
|
||||
/// @param val A std string.
|
||||
/// @return A hash of the given string.
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr uint32_t
|
||||
CalculateHash(const std::string_view& val) noexcept {
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr u32 CalculateHash(const std::string_view& val) noexcept {
|
||||
return Hash(val.data());
|
||||
}
|
||||
|
||||
|
||||
@@ -9,18 +9,18 @@ namespace ArbUt {
|
||||
/// @brief A literal representation of a string view. Used for compile time string processing.
|
||||
class StringViewLiteral final : public BasicStringView {
|
||||
private:
|
||||
const char* _str;
|
||||
const char* non_null _str;
|
||||
|
||||
public:
|
||||
/// @brief Compile time initialisation of a StringViewLiteral.
|
||||
/// @param str A null terminated C string.
|
||||
/// @param size A The length of the string.
|
||||
constexpr StringViewLiteral(const char* str, size_t size) noexcept
|
||||
constexpr StringViewLiteral(const char* non_null str, size_t size) noexcept
|
||||
: BasicStringView(size, Hash(str)), _str(str) {}
|
||||
/// @brief Compile time initialisation of a StringViewLiteral. Length is calculated at compile.
|
||||
/// @param str A null terminated C string.
|
||||
constexpr StringViewLiteral(const char* str) noexcept : StringViewLiteral(str, CalcLength(str)){};
|
||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept final { return _str; }
|
||||
constexpr StringViewLiteral(const char* non_null str) noexcept : StringViewLiteral(str, CalcLength(str)){};
|
||||
[[nodiscard]] inline constexpr const char* non_null c_str() const noexcept final { return _str; }
|
||||
[[nodiscard]] constexpr std::string_view std_str() const noexcept final {
|
||||
return std::string_view(_str, _length);
|
||||
}
|
||||
@@ -38,8 +38,8 @@ namespace ArbUt {
|
||||
inline constexpr bool operator!=(const std::string_view& rhs) const noexcept final {
|
||||
return _hash != Hash(rhs.data());
|
||||
}
|
||||
inline constexpr bool operator==(const char* rhs) const noexcept final { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* rhs) const noexcept final { return _hash != Hash(rhs); }
|
||||
inline constexpr bool operator==(const char* non_null rhs) const noexcept final { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* non_null rhs) const noexcept final { return _hash != Hash(rhs); }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -53,10 +53,10 @@ namespace std {
|
||||
};
|
||||
}
|
||||
|
||||
inline constexpr ArbUt::StringViewLiteral operator"" _const_nocase(const char* c, size_t l) {
|
||||
inline constexpr ArbUt::StringViewLiteral operator"" _const_nocase(const char* non_null c, size_t l) {
|
||||
return ArbUt::StringViewLiteral(c, l);
|
||||
}
|
||||
inline constexpr ArbUt::StringViewLiteral operator"" _cnc(const char* c, size_t l) {
|
||||
inline constexpr ArbUt::StringViewLiteral operator"" _cnc(const char* non_null c, size_t l) {
|
||||
return ArbUt::StringViewLiteral(c, l);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user