Merge branch 'c++20-spaceship' of github.com:vslavik/wxWidgets

Implement C++20 spaceship operator for wxString.

Fixes silent wxString comparison breakage in C++20 with std::tuple etc.

See #26306.
This commit is contained in:
Vadim Zeitlin
2026-03-19 02:08:32 +01:00
2 changed files with 33 additions and 0 deletions

View File

@@ -49,6 +49,11 @@
#endif #endif
#endif #endif
// Check if C++20 three-way-comparison operator is available
#ifdef __cpp_impl_three_way_comparison
#include <compare>
#endif
#include "wx/afterstd.h" #include "wx/afterstd.h"
// by default we cache the mapping of the positions in UTF-8 string to the byte // by default we cache the mapping of the positions in UTF-8 string to the byte
@@ -2288,6 +2293,19 @@ public:
friend bool operator>=(const wxString& s1, const wxString& s2) friend bool operator>=(const wxString& s1, const wxString& s2)
{ return s1.Cmp(s2) >= 0; } { return s1.Cmp(s2) >= 0; }
#ifdef __cpp_impl_three_way_comparison
friend auto operator<=>(const wxString& s1, const wxString& s2)
{
const int cmp = s1.Cmp(s2);
if (cmp < 0)
return std::strong_ordering::less;
else if (cmp > 0)
return std::strong_ordering::greater;
else
return std::strong_ordering::equal;
}
#endif
friend bool operator==(const wxString& s1, const wxCStrData& s2) friend bool operator==(const wxString& s1, const wxCStrData& s2)
{ return s1 == s2.AsString(); } { return s1 == s2.AsString(); }
friend bool operator==(const wxCStrData& s1, const wxString& s2) friend bool operator==(const wxCStrData& s1, const wxString& s2)

View File

@@ -453,6 +453,21 @@ TEST_CASE("StringCompare", "[wxString]")
CHECK( wxString("!").Cmp("z") < 0 ); CHECK( wxString("!").Cmp("z") < 0 );
} }
#ifdef __cpp_lib_three_way_comparison
TEST_CASE("StringCompareThreeWay", "[wxString]")
{
// test that operator<=> works and compares by string contents
wxString a(wxT("bar"));
wxString b(wxT("bar"));
wxString c(wxT("baz"));
CHECK((a <=> b) == std::strong_ordering::equal);
CHECK((a <=> c) == std::strong_ordering::less);
CHECK((c <=> a) == std::strong_ordering::greater);
}
#endif // __cpp_lib_three_way_comparison
TEST_CASE("StringCompareNoCase", "[wxString]") TEST_CASE("StringCompareNoCase", "[wxString]")
{ {
wxString s1 = wxT("AHH"); wxString s1 = wxT("AHH");