2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2021-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-12-23 17:16:05 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string_view>
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
#include "quark.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper functions wrapped around tr_quark
|
|
|
|
*/
|
|
|
|
class tr_interned_string
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tr_interned_string() = default;
|
|
|
|
|
|
|
|
explicit tr_interned_string(tr_quark quark)
|
|
|
|
: quark_{ quark }
|
2022-11-03 23:08:02 +00:00
|
|
|
, sv_{ tr_quark_get_string_view(quark_) }
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
explicit tr_interned_string(std::string_view sv)
|
|
|
|
: tr_interned_string{ tr_quark_new(sv) }
|
|
|
|
{
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
explicit tr_interned_string(char const* c_str)
|
|
|
|
: tr_interned_string{ std::string_view{ c_str ? c_str : "" } }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_interned_string& operator=(tr_quark quark)
|
|
|
|
{
|
|
|
|
quark_ = quark;
|
2022-11-03 23:08:02 +00:00
|
|
|
sv_ = tr_quark_get_string_view(quark_);
|
2021-12-23 17:16:05 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string& operator=(std::string_view sv)
|
|
|
|
{
|
|
|
|
return *this = tr_quark_new(sv);
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string& operator=(char const* c_str)
|
|
|
|
{
|
|
|
|
return *this = std::string_view{ c_str != nullptr ? c_str : "" };
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto quark() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return quark_;
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto sv() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return sv_;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr char const* c_str() const noexcept
|
2022-08-28 21:17:07 +00:00
|
|
|
{
|
|
|
|
return std::data(sv()); // tr_quark strs are always zero-terminated
|
|
|
|
}
|
2021-12-23 17:16:05 +00:00
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto data() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return std::data(sv());
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto empty() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return quark_ == TR_KEY_NONE;
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto size() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return std::size(sv());
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
|
|
|
constexpr void clear()
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
*this = tr_interned_string{};
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto begin() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return std::begin(sv());
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto end() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return std::end(sv());
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto rbegin() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return std::rbegin(sv());
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto rend() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return std::rend(sv());
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto compare(tr_interned_string const& that) const noexcept // <=>
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2021-12-25 17:22:12 +00:00
|
|
|
if (this->quark() < that.quark())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->quark() > that.quark())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool operator<(tr_interned_string const& that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return this->compare(that) < 0;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool operator>(tr_interned_string const& that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return this->compare(that) > 0;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(tr_interned_string const& that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return this->compare(that) == 0;
|
|
|
|
}
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(tr_interned_string const& that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return this->compare(that) != 0;
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto operator==(std::string_view that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return sv() == that;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr auto operator!=(std::string_view that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2022-11-03 23:08:02 +00:00
|
|
|
return sv() != that;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(char const* that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return *this == std::string_view{ that != nullptr ? that : "" };
|
|
|
|
}
|
2022-11-03 23:08:02 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(char const* that) const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return *this != std::string_view{ that != nullptr ? that : "" };
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
[[nodiscard]] constexpr operator std::string_view() const noexcept
|
2022-03-28 02:55:30 +00:00
|
|
|
{
|
|
|
|
return sv();
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
private:
|
|
|
|
tr_quark quark_ = TR_KEY_NONE;
|
2022-11-03 23:08:02 +00:00
|
|
|
std::string_view sv_ = "";
|
2021-12-23 17:16:05 +00:00
|
|
|
};
|
2022-03-28 02:55:30 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct fmt::formatter<tr_interned_string> : formatter<std::string_view>
|
|
|
|
{
|
|
|
|
template<typename FormatContext>
|
2022-03-29 10:57:04 +00:00
|
|
|
constexpr auto format(tr_interned_string const& is, FormatContext& ctx) const
|
2022-03-28 02:55:30 +00:00
|
|
|
{
|
2022-03-29 10:57:04 +00:00
|
|
|
return formatter<std::string_view>::format(is.sv(), ctx);
|
2022-03-28 02:55:30 +00:00
|
|
|
}
|
|
|
|
};
|