From 1edbfdef6588b0e81b4c545031ce0bb2e104534b Mon Sep 17 00:00:00 2001 From: Andreas Pehrson Date: Thu, 27 Aug 2026 07:57:19 +0000 Subject: [PATCH] Bug 2051711 - Add operator<=> to nsTStringRepr. r=xpcom-reviewers,webrtc-reviewers,bwc,emilio Differential Revision: https://phabricator.services.mozilla.com/D309690 --- .../gtest/peer_connection_unittest.cpp | 4 +-- xpcom/string/nsTStringRepr.h | 25 +++---------------- xpcom/tests/gtest/TestStrings.cpp | 19 ++++++++++++++ 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/media/webrtc/signaling/gtest/peer_connection_unittest.cpp b/media/webrtc/signaling/gtest/peer_connection_unittest.cpp index 5d30fc5762e2..ef5722938382 100644 --- a/media/webrtc/signaling/gtest/peer_connection_unittest.cpp +++ b/media/webrtc/signaling/gtest/peer_connection_unittest.cpp @@ -13,9 +13,7 @@ namespace mozilla { static std::strong_ordering RtpExtensionHeaderUriComparator( const PeerConnectionImpl::RtpExtensionHeader& aHeader, const char* aUri) { - // TODO bug 2051711: use nsCString::operator<=> directly. - return std::string_view(aHeader.extensionname.get()) <=> - std::string_view(aUri); + return aHeader.extensionname <=> nsDependentCString(aUri); } static const PeerConnectionImpl::RtpExtensionHeader* FindExtension( diff --git a/xpcom/string/nsTStringRepr.h b/xpcom/string/nsTStringRepr.h index fb1a822dc92f..8eb1d2ab0f1a 100644 --- a/xpcom/string/nsTStringRepr.h +++ b/xpcom/string/nsTStringRepr.h @@ -525,18 +525,6 @@ inline constexpr bool operator!=(const mozilla::detail::nsTStringRepr& aLhs, return !aLhs.Equals(aRhs); } -template -inline bool operator<(const mozilla::detail::nsTStringRepr& aLhs, - const mozilla::detail::nsTStringRepr& aRhs) { - return Compare(aLhs, aRhs) < 0; -} - -template -inline bool operator<=(const mozilla::detail::nsTStringRepr& aLhs, - const mozilla::detail::nsTStringRepr& aRhs) { - return Compare(aLhs, aRhs) <= 0; -} - template inline bool operator==(const mozilla::detail::nsTStringRepr& aLhs, const mozilla::detail::nsTStringRepr& aRhs) { @@ -550,15 +538,10 @@ inline bool operator==(const mozilla::detail::nsTStringRepr& aLhs, } template -inline bool operator>=(const mozilla::detail::nsTStringRepr& aLhs, - const mozilla::detail::nsTStringRepr& aRhs) { - return Compare(aLhs, aRhs) >= 0; -} - -template -inline bool operator>(const mozilla::detail::nsTStringRepr& aLhs, - const mozilla::detail::nsTStringRepr& aRhs) { - return Compare(aLhs, aRhs) > 0; +inline std::strong_ordering operator<=>( + const mozilla::detail::nsTStringRepr& aLhs, + const mozilla::detail::nsTStringRepr& aRhs) { + return Compare(aLhs, aRhs) <=> 0; } template diff --git a/xpcom/tests/gtest/TestStrings.cpp b/xpcom/tests/gtest/TestStrings.cpp index 86d0baf7bbfc..05ea9909c0da 100644 --- a/xpcom/tests/gtest/TestStrings.cpp +++ b/xpcom/tests/gtest/TestStrings.cpp @@ -2415,6 +2415,25 @@ TEST_F(Strings, printf) { #undef verify_printf_strings #undef create_printf_strings +TEST(String, SpaceshipCompare) +{ + nsAutoCString a("A"); + nsCString b("B"); + EXPECT_EQ(a <=> a, std::strong_ordering::equal); + EXPECT_EQ(a <=> b, std::strong_ordering::less); + EXPECT_EQ(b <=> a, std::strong_ordering::greater); + EXPECT_EQ(b <=> b, std::strong_ordering::equal); + + nsLiteralCString ab = "AB"_ns; + EXPECT_EQ(a <=> ab, std::strong_ordering::less); + EXPECT_EQ(b <=> ab, std::strong_ordering::greater); + + a.Append("B"); + EXPECT_EQ(a <=> a, std::strong_ordering::equal); + EXPECT_EQ(a <=> b, std::strong_ordering::less); + EXPECT_EQ(a <=> ab, std::strong_ordering::equal); +} + // Note the five calls in the loop, so divide by 100k MOZ_GTEST_BENCH_F(Strings, PerfStripWhitespace, [this] { nsCString test1(mExample1Utf8);