refactor: use char8_t as underlying type of tr_interned_string (#8556)

This commit is contained in:
Yat Ho
2026-02-25 23:07:01 +08:00
committed by GitHub
parent 765debd61c
commit d56a8e80c3
2 changed files with 52 additions and 76 deletions

View File

@@ -5,6 +5,7 @@
#pragma once
#include <compare>
#include <cstddef> // size_t
#include <optional>
#include <string>
@@ -31,29 +32,19 @@ public:
tr_tracker_tier_t tier = 0;
tr_tracker_id_t id = 0;
[[nodiscard]] constexpr int compare(tracker_info const& that) const noexcept // <=>
[[nodiscard]] constexpr auto operator<=>(tracker_info const& that) const noexcept
{
if (this->tier != that.tier)
if (auto const res = this->tier <=> that.tier; res != 0)
{
return this->tier < that.tier ? -1 : 1;
return res;
}
if (int const i{ this->announce.compare(that.announce) }; i != 0)
{
return i;
}
return 0;
}
[[nodiscard]] constexpr bool operator<(tracker_info const& that) const noexcept
{
return compare(that) < 0;
return this->announce <=> that.announce;
}
[[nodiscard]] constexpr bool operator==(tracker_info const& that) const noexcept
{
return compare(that) == 0;
return (*this <=> that) == 0;
}
};
@@ -93,11 +84,6 @@ public:
return trackers_ == that.trackers_;
}
[[nodiscard]] TR_CONSTEXPR_VEC bool operator!=(tr_announce_list const& that) const
{
return trackers_ != that.trackers_;
}
void add_to_map(tr_variant::Map& setme) const;
bool add(std::string_view announce_url)

View File

@@ -5,6 +5,8 @@
#pragma once
#include <algorithm>
#include <compare>
#include <string_view>
#include <fmt/format.h>
@@ -21,7 +23,12 @@ public:
explicit tr_interned_string(tr_quark quark)
: quark_{ quark }
, sv_{ tr_quark_get_string_view(quark_) }
, sv_{ tr_quark_get_u8string_view(quark_) }
{
}
explicit tr_interned_string(std::u8string_view sv)
: tr_interned_string{ tr_quark_new(sv) }
{
}
@@ -38,10 +45,15 @@ public:
tr_interned_string& operator=(tr_quark quark)
{
quark_ = quark;
sv_ = tr_quark_get_string_view(quark_);
sv_ = tr_quark_get_u8string_view(quark_);
return *this;
}
tr_interned_string& operator=(std::u8string_view sv)
{
return *this = tr_quark_new(sv);
}
tr_interned_string& operator=(std::string_view sv)
{
return *this = tr_quark_new(sv);
@@ -57,19 +69,19 @@ public:
return quark_;
}
[[nodiscard]] constexpr auto sv() const noexcept
[[nodiscard]] constexpr auto u8sv() const noexcept
{
return sv_;
}
[[nodiscard]] constexpr char const* c_str() const noexcept
{
return std::data(sv()); // tr_quark strs are always zero-terminated
}
[[nodiscard]] constexpr auto data() const noexcept
{
return std::data(sv());
return std::data(u8sv());
}
[[nodiscard]] char const* c_str() const noexcept
{
return reinterpret_cast<char const*>(data()); // tr_quark strs are always zero-terminated
}
[[nodiscard]] constexpr auto empty() const noexcept
@@ -79,7 +91,12 @@ public:
[[nodiscard]] constexpr auto size() const noexcept
{
return std::size(sv());
return std::size(u8sv());
}
[[nodiscard]] auto sv() const noexcept
{
return std::string_view{ reinterpret_cast<char const*>(data()), size() };
}
constexpr void clear()
@@ -89,83 +106,56 @@ public:
[[nodiscard]] constexpr auto begin() const noexcept
{
return std::begin(sv());
return std::begin(u8sv());
}
[[nodiscard]] constexpr auto end() const noexcept
{
return std::end(sv());
return std::end(u8sv());
}
[[nodiscard]] constexpr auto rbegin() const noexcept
{
return std::rbegin(sv());
return std::rbegin(u8sv());
}
[[nodiscard]] constexpr auto rend() const noexcept
{
return std::rend(sv());
return std::rend(u8sv());
}
[[nodiscard]] constexpr auto compare(tr_interned_string const& that) const noexcept // <=>
[[nodiscard]] constexpr auto operator<=>(tr_interned_string const& that) const noexcept
{
if (this->quark() < that.quark())
{
return -1;
}
if (this->quark() > that.quark())
{
return 1;
}
return 0;
return this->quark() <=> that.quark();
}
[[nodiscard]] constexpr bool operator<(tr_interned_string const& that) const noexcept
{
return this->compare(that) < 0;
}
[[nodiscard]] constexpr bool operator>(tr_interned_string const& that) const noexcept
{
return this->compare(that) > 0;
}
[[nodiscard]] constexpr bool operator==(tr_interned_string const& that) const noexcept
{
return this->compare(that) == 0;
}
[[nodiscard]] constexpr bool operator!=(tr_interned_string const& that) const noexcept
{
return this->compare(that) != 0;
return (*this <=> that) == 0;
}
[[nodiscard]] constexpr auto operator==(std::string_view that) const noexcept
[[nodiscard]] constexpr auto operator<=>(std::string_view that) const noexcept
{
return sv() == that;
return std::lexicographical_compare_three_way(begin(), end(), that.begin(), that.end());
}
[[nodiscard]] constexpr auto operator!=(std::string_view that) const noexcept
[[nodiscard]] constexpr bool operator==(std::string_view that) const noexcept
{
return sv() != that;
}
[[nodiscard]] constexpr bool operator==(char const* that) const noexcept
{
return *this == std::string_view{ that != nullptr ? that : "" };
}
[[nodiscard]] constexpr bool operator!=(char const* that) const noexcept
{
return *this != std::string_view{ that != nullptr ? that : "" };
return std::ranges::equal(*this, that);
}
// NOLINTNEXTLINE(google-explicit-constructor)
[[nodiscard]] constexpr operator std::string_view() const noexcept
[[nodiscard]] constexpr operator std::u8string_view() const noexcept
{
return u8sv();
}
// NOLINTNEXTLINE(google-explicit-constructor)
[[nodiscard]] operator std::string_view() const noexcept
{
return sv();
}
private:
tr_quark quark_ = TR_KEY_NONE;
std::string_view sv_;
std::u8string_view sv_;
};
template<>