Add missing support for `riegeli::EstimateMemory()` and `riegeli::Debug()`
to `OptionalCompactString`.

PiperOrigin-RevId: 917057597
diff --git a/riegeli/base/compact_string.h b/riegeli/base/compact_string.h
index a212c97..37e7a94 100644
--- a/riegeli/base/compact_string.h
+++ b/riegeli/base/compact_string.h
@@ -300,6 +300,13 @@
     return HashState::combine(std::move(hash_state), absl::string_view(self));
   }
 
+  // Supports `MemoryEstimator`.
+  template <typename MemoryEstimator>
+  friend void RiegeliRegisterSubobjects(const CompactString* self,
+                                        MemoryEstimator& memory_estimator) {
+    RegisterSubobjects(self->repr_, memory_estimator);
+  }
+
   // Default stringification by `absl::StrCat()` etc.
   template <typename Sink>
   friend void AbslStringify(Sink& dest, const CompactString& src) {
@@ -347,14 +354,9 @@
     self->DumpStructure(substr, dest);
   }
 
-  // Supports `MemoryEstimator`.
-  template <typename MemoryEstimator>
-  friend void RiegeliRegisterSubobjects(const CompactString* self,
-                                        MemoryEstimator& memory_estimator) {
-    self->RegisterSubobjects(memory_estimator);
-  }
-
  private:
+  friend class OptionalCompactString;  // For `RegisterSubobjects()`.
+
   struct FromReprTag {
     explicit FromReprTag() = default;
   };
@@ -537,7 +539,8 @@
 
   void DumpStructure(absl::string_view substr, std::ostream& dest) const;
   template <typename MemoryEstimator>
-  void RegisterSubobjects(MemoryEstimator& memory_estimator) const;
+  static void RegisterSubobjects(uintptr_t repr,
+                                 MemoryEstimator& memory_estimator);
 
   uintptr_t repr_ = kInlineTag;
 };
@@ -878,12 +881,13 @@
 
 template <typename MemoryEstimator>
 inline void CompactString::RegisterSubobjects(
-    MemoryEstimator& memory_estimator) const {
-  const uintptr_t tag = repr_ & kTagMask;
+    uintptr_t repr, MemoryEstimator& memory_estimator) {
+  const uintptr_t tag = repr & kTagMask;
   if (tag == kInlineTag) return;
   const size_t offset = tag == 0 ? 2 * sizeof(size_t) : IntCast<size_t>(tag);
   memory_estimator.RegisterDynamicMemory(
-      allocated_data() - offset, offset + allocated_capacity_for_tag(tag));
+      allocated_data(repr) - offset,
+      offset + allocated_capacity_for_tag(tag, repr));
 }
 
 }  // namespace riegeli
diff --git a/riegeli/base/optional_compact_string.h b/riegeli/base/optional_compact_string.h
index be7ab5e..30775c8 100644
--- a/riegeli/base/optional_compact_string.h
+++ b/riegeli/base/optional_compact_string.h
@@ -190,6 +190,25 @@
     return riegeli::Compare(*a, b);
   }
 
+  // Supports `MemoryEstimator`.
+  template <typename MemoryEstimator>
+  friend void RiegeliRegisterSubobjects(const OptionalCompactString* self,
+                                        MemoryEstimator& memory_estimator) {
+    if (self->repr_ == kNullRepr) return;
+    CompactString::RegisterSubobjects(self->repr_, memory_estimator);
+  }
+
+  // Supports `riegeli::Debug()`.
+  template <typename DebugStream>
+  friend void RiegeliDebug(const OptionalCompactString& src,
+                           DebugStream& dest) {
+    if (src == nullptr) {
+      dest.Debug(nullptr);
+    } else {
+      dest.Debug(*src);
+    }
+  }
+
  private:
   static constexpr uintptr_t kNullRepr = 0;