Arbutils/src/String/BasicStringView.hpp

62 lines
2.9 KiB
C++

#ifndef ARBUTILS_BASICSTRINGVIEW_HPP
#define ARBUTILS_BASICSTRINGVIEW_HPP
#include <cstdint>
#include <sstream>
#include <string_view>
#include "../Defines.hpp"
/// \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;
u32 _hash = 0;
/// @brief Construct a basic string view.
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 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 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;
}
/// @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;
}
};
}
#endif // ARBUTILS_BASICSTRINGVIEW_HPP