summaryrefslogtreecommitdiff
path: root/absl/strings/internal/cord_internal.h
diff options
context:
space:
mode:
authorGravatar Martijn Vels <mvels@google.com>2022-09-29 18:14:53 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-09-29 18:15:59 -0700
commit92bc0b6b68cd138aefeb94d566499f5bf14f859f (patch)
treea2558321254062576ad4fc30c792ae88b7542ba6 /absl/strings/internal/cord_internal.h
parentb39aa365e14984035bbbe60f52ec40a66419f18e (diff)
Cleanup: Move BitwiseCompare() to InlineData, and make it layout independent.
This removes layout specific details from InlineData from cord.h, making future platform specific internal layout changes easier to land. PiperOrigin-RevId: 477869206 Change-Id: I1d417af47d7f04e34a98ba7b93ae591ece8f9151
Diffstat (limited to 'absl/strings/internal/cord_internal.h')
-rw-r--r--absl/strings/internal/cord_internal.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/absl/strings/internal/cord_internal.h b/absl/strings/internal/cord_internal.h
index 601457ee..f32fd416 100644
--- a/absl/strings/internal/cord_internal.h
+++ b/absl/strings/internal/cord_internal.h
@@ -582,6 +582,29 @@ class InlineData {
tag() = static_cast<char>(size << 1);
}
+ // Compares 'this' inlined data with rhs. The comparison is a straightforward
+ // lexicographic comparison. `Compare()` returns values as follows:
+ //
+ // -1 'this' InlineData instance is smaller
+ // 0 the InlineData instances are equal
+ // 1 'this' InlineData instance larger
+ int Compare(const InlineData& rhs) const {
+ uint64_t x, y;
+ memcpy(&x, as_chars(), sizeof(x));
+ memcpy(&y, rhs.as_chars(), sizeof(y));
+ if (x == y) {
+ memcpy(&x, as_chars() + 7, sizeof(x));
+ memcpy(&y, rhs.as_chars() + 7, sizeof(y));
+ if (x == y) {
+ if (inline_size() == rhs.inline_size()) return 0;
+ return inline_size() < rhs.inline_size() ? -1 : 1;
+ }
+ }
+ x = absl::big_endian::FromHost64(x);
+ y = absl::big_endian::FromHost64(y);
+ return x < y ? -1 : 1;
+ }
+
private:
// See cordz_info_t for forced alignment and size of `cordz_info` details.
struct AsTree {