Lots of documentation.
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:
@@ -2,34 +2,53 @@
|
||||
#define ARBUTILS_BASICSTRINGVIEW_HPP
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
/// \defgroup Strings Strings
|
||||
/// \brief Group of non-editable strings with faster hashing.
|
||||
|
||||
namespace ArbUt {
|
||||
|
||||
/// \ingroup Strings
|
||||
/// @brief Abstract base class of the string views.
|
||||
class BasicStringView {
|
||||
protected:
|
||||
size_t _length = 0;
|
||||
uint32_t _hash = 0;
|
||||
|
||||
/// @brief Construct a basic string view.
|
||||
constexpr BasicStringView(size_t length, uint32_t 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; }
|
||||
/// @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; }
|
||||
/// @brief Check whether two StringViews are equal
|
||||
[[nodiscard]] inline constexpr bool operator==(const BasicStringView& rhs) const noexcept {
|
||||
return _hash == rhs._hash;
|
||||
}
|
||||
/// @brief Check whether two StringViews are unequal
|
||||
inline constexpr bool operator!=(const BasicStringView& rhs) const noexcept { return _hash != rhs._hash; }
|
||||
/// @brief Check whether a string view has a length of 0.
|
||||
inline constexpr bool IsEmpty() const noexcept { return Length() == 0; }
|
||||
|
||||
/// @brief The C string interpretation of the string.
|
||||
[[nodiscard]] virtual constexpr const char* c_str() const noexcept = 0;
|
||||
/// @brief The C++ std string interpretation of the string.
|
||||
[[nodiscard]] virtual constexpr std::string_view std_str() const noexcept = 0;
|
||||
|
||||
/// @brief Check the equality between a std string_view and our StringViews.
|
||||
virtual constexpr bool operator==(const std::string_view& rhs) const noexcept = 0;
|
||||
/// @brief Check the inequality between a std string_view and our StringViews.
|
||||
virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0;
|
||||
/// @brief Check the equality between a C string and our StringViews.
|
||||
virtual constexpr bool operator==(const char* rhs) const noexcept = 0;
|
||||
/// @brief Check the equality between a C string and our StringViews.
|
||||
virtual constexpr bool operator!=(const char* rhs) const noexcept = 0;
|
||||
|
||||
/// @brief Pipe the current string into an output stream.
|
||||
friend std::ostream& operator<<(std::ostream& out, const BasicStringView& c) {
|
||||
out << c.c_str();
|
||||
return out;
|
||||
|
||||
@@ -37,6 +37,8 @@ static uint32_t constexpr Hash(char const* input) {
|
||||
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
|
||||
|
||||
namespace ArbUt {
|
||||
/// \ingroup Strings
|
||||
/// @brief A constant, non-editable string that allows copies to share the same string object.
|
||||
class StringView final : public BasicStringView {
|
||||
private:
|
||||
static std::shared_ptr<__ConstStringCharHolder> __emptyString;
|
||||
@@ -91,6 +93,7 @@ namespace ArbUt {
|
||||
}
|
||||
|
||||
namespace std {
|
||||
/// @brief Helper class for hashing string views.
|
||||
template <> struct hash<ArbUt::StringView> {
|
||||
constexpr std::size_t operator()(ArbUt::StringView const& s) const noexcept { return s.GetHash(); }
|
||||
};
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "StringView.hpp"
|
||||
|
||||
namespace ArbUt {
|
||||
/// \ingroup Strings
|
||||
/// @brief A literal representation of a string view. Used for compile time string processing.
|
||||
class StringViewLiteral final : public BasicStringView {
|
||||
private:
|
||||
const char* _str;
|
||||
@@ -14,23 +16,26 @@ namespace ArbUt {
|
||||
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); }
|
||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str; }
|
||||
[[nodiscard]] constexpr std::string_view std_str() const noexcept override {
|
||||
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(*this, _str, _length); }
|
||||
|
||||
inline constexpr bool operator==(const std::string_view& rhs) const noexcept {
|
||||
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 {
|
||||
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 { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* rhs) const noexcept { return _hash != Hash(rhs); }
|
||||
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); }
|
||||
};
|
||||
}
|
||||
|
||||
namespace std {
|
||||
/// @brief Helper class for getting the hash of a string view literal.
|
||||
template <> struct hash<ArbUt::StringViewLiteral> {
|
||||
constexpr std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user