3535#ifdef FROZEN_LETITGO_HAS_STRING_VIEW
3636#include < string_view>
3737#endif
38+
39+ #ifdef FROZEN_LETITGO_HAS_THREE_WAY_COMPARISON
40+ #include < compare>
41+ #endif
3842#endif
3943
4044namespace frozen {
@@ -57,7 +61,7 @@ class basic_string {
5761 constexpr basic_string (std::basic_string_view<chr_t > data)
5862 : data_(data.data()), size_(data.size()) {}
5963
60- explicit constexpr operator std::basic_string_view<chr_t >() const {
64+ constexpr explicit operator std::basic_string_view<chr_t >() const {
6165 return std::basic_string_view<chr_t >(data_, size_);
6266 }
6367#endif
@@ -70,7 +74,32 @@ class basic_string {
7074
7175 constexpr chr_t operator [](std::size_t i) const { return data_[i]; }
7276
73- constexpr bool operator ==(basic_string other) const {
77+ #ifdef FROZEN_LETITGO_HAS_THREE_WAY_COMPARISON
78+ constexpr std::weak_ordering operator <=>(const basic_string &other) const {
79+ bool equal = true ;
80+ if (size_ == other.size_ )
81+ for (std::size_t i = 0 ; i < size_; ++i)
82+ if (data_[i] != other.data_ [i]) {
83+ equal = false ;
84+ break ;
85+ }
86+
87+ if (equal) return std::weak_ordering::equivalent;
88+
89+ unsigned i = 0 ;
90+ while (i < size () && i < other.size ()) {
91+ if ((*this )[i] < other[i]) {
92+ return std::weak_ordering::less;
93+ }
94+ if ((*this )[i] > other[i]) {
95+ return std::weak_ordering::greater;
96+ }
97+ ++i;
98+ }
99+ return size () < other.size () ? std::weak_ordering::less : std::weak_ordering::greater;
100+ }
101+ #else
102+ constexpr bool operator ==(const basic_string &other) const {
74103 if (size_ != other.size_ )
75104 return false ;
76105 for (std::size_t i = 0 ; i < size_; ++i)
@@ -79,6 +108,10 @@ class basic_string {
79108 return true ;
80109 }
81110
111+ friend constexpr bool operator !=(const basic_string &lhs, const basic_string &rhs) {
112+ return !(lhs == rhs);
113+ }
114+
82115 constexpr bool operator <(const basic_string &other) const {
83116 unsigned i = 0 ;
84117 while (i < size () && i < other.size ()) {
@@ -102,6 +135,7 @@ class basic_string {
102135 friend constexpr bool operator <=(const basic_string& lhs, const basic_string& rhs) {
103136 return !(lhs > rhs);
104137 }
138+ #endif
105139
106140 constexpr const chr_t *data () const { return data_; }
107141 constexpr const chr_t *begin () const { return data (); }
0 commit comments