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 }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
explicit tr_interned_string(std::string_view sv)
|
|
|
|
: tr_interned_string{ tr_quark_new(sv) }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
tr_interned_string& operator=(std::string_view sv)
|
|
|
|
{
|
|
|
|
return *this = tr_quark_new(sv);
|
|
|
|
}
|
|
|
|
tr_interned_string& operator=(char const* c_str)
|
|
|
|
{
|
|
|
|
return *this = std::string_view{ c_str != nullptr ? c_str : "" };
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr tr_quark quark() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
|
|
|
return quark_;
|
|
|
|
}
|
|
|
|
[[nodiscard]] std::string_view sv() const
|
|
|
|
{
|
|
|
|
return tr_quark_get_string_view(quark_);
|
|
|
|
}
|
2022-08-28 21:17:07 +00:00
|
|
|
[[nodiscard]] char const* c_str() const
|
|
|
|
{
|
|
|
|
return std::data(sv()); // tr_quark strs are always zero-terminated
|
|
|
|
}
|
2021-12-23 17:16:05 +00:00
|
|
|
|
|
|
|
[[nodiscard]] auto data() const
|
|
|
|
{
|
|
|
|
return std::data(this->sv());
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
[[nodiscard]] auto size() const
|
|
|
|
{
|
|
|
|
return std::size(this->sv());
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
*this = TR_KEY_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto begin() const
|
|
|
|
{
|
|
|
|
return std::begin(this->sv());
|
|
|
|
}
|
|
|
|
[[nodiscard]] auto end() const
|
|
|
|
{
|
|
|
|
return std::end(this->sv());
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto rbegin() const
|
|
|
|
{
|
|
|
|
return std::rbegin(this->sv());
|
|
|
|
}
|
|
|
|
[[nodiscard]] auto rend() const
|
|
|
|
{
|
|
|
|
return std::rend(this->sv());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool operator==(std::string_view that) const
|
|
|
|
{
|
|
|
|
return this->sv() == that;
|
|
|
|
}
|
|
|
|
[[nodiscard]] bool operator!=(std::string_view that) const
|
|
|
|
{
|
|
|
|
return this->sv() != that;
|
|
|
|
}
|
|
|
|
[[nodiscard]] bool operator==(char const* that) const
|
|
|
|
{
|
|
|
|
return *this == std::string_view{ that != nullptr ? that : "" };
|
|
|
|
}
|
|
|
|
[[nodiscard]] bool operator!=(char const* that) const
|
|
|
|
{
|
|
|
|
return *this != std::string_view{ that != nullptr ? that : "" };
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
operator std::string_view() const
|
|
|
|
{
|
|
|
|
return sv();
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
private:
|
|
|
|
tr_quark quark_ = TR_KEY_NONE;
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
};
|