summaryrefslogtreecommitdiff
path: root/absl/strings/cord.h
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2024-06-26 08:13:11 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2024-06-26 08:14:50 -0700
commitc98bd9c8840f9ded87cf1fd1238455468d325628 (patch)
tree92d3514de1b1ffb48db9faf17ddaf59cba6ebfa2 /absl/strings/cord.h
parent3ff94461f8cf00fa976225f3b020e65268e24626 (diff)
Three-way comparison spaceship <=> operators for Cord.
This is portable because cord already has `operator<` etc., which will be unaffected. This just allows C++ >= 20 users to explicitly call `operator<=>`. PiperOrigin-RevId: 646951415 Change-Id: I1432e224bd5dc09b99d56a1d27e95078463adf45
Diffstat (limited to 'absl/strings/cord.h')
-rw-r--r--absl/strings/cord.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/absl/strings/cord.h b/absl/strings/cord.h
index 0d8d7007..c68b6f10 100644
--- a/absl/strings/cord.h
+++ b/absl/strings/cord.h
@@ -96,6 +96,7 @@
#include "absl/strings/internal/resize_uninitialized.h"
#include "absl/strings/internal/string_constant.h"
#include "absl/strings/string_view.h"
+#include "absl/types/compare.h"
#include "absl/types/optional.h"
namespace absl {
@@ -849,6 +850,38 @@ class Cord {
friend bool operator==(const Cord& lhs, const Cord& rhs);
friend bool operator==(const Cord& lhs, absl::string_view rhs);
+#ifdef __cpp_impl_three_way_comparison
+
+ // Cords support comparison with other Cords and string_views via operator<
+ // and others; here we provide a wrapper for the C++20 three-way comparison
+ // <=> operator.
+
+ static inline std::strong_ordering ConvertCompareResultToStrongOrdering(
+ int c) {
+ if (c == 0) {
+ return std::strong_ordering::equal;
+ } else if (c < 0) {
+ return std::strong_ordering::less;
+ } else {
+ return std::strong_ordering::greater;
+ }
+ }
+
+ friend inline std::strong_ordering operator<=>(const Cord& x, const Cord& y) {
+ return ConvertCompareResultToStrongOrdering(x.Compare(y));
+ }
+
+ friend inline std::strong_ordering operator<=>(const Cord& lhs,
+ absl::string_view rhs) {
+ return ConvertCompareResultToStrongOrdering(lhs.Compare(rhs));
+ }
+
+ friend inline std::strong_ordering operator<=>(absl::string_view lhs,
+ const Cord& rhs) {
+ return ConvertCompareResultToStrongOrdering(-rhs.Compare(lhs));
+ }
+#endif
+
friend absl::Nullable<const CordzInfo*> GetCordzInfoForTesting(
const Cord& cord);