[llvm] [ADT] Reimplement operator==(StringRef (PR #91139)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 5 12:50:56 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

 StringRef) (NFC),I'm planning to deprecate and eventually remove StringRef::equals in
favor of operator==.  This patch reimplements operator== without using
StringRef::equals.

I'm not sure if there is a good way to make StringRef::compareMemory
available to operator==, which is not a member function.  "friend"
works to some extent but breaks corner cases, which is why I've chosen
to "inline" compareMemory.

---
Full diff: https://github.com/llvm/llvm-project/pull/91139.diff


1 Files Affected:

- (modified) llvm/include/llvm/ADT/StringRef.h (+5-1) 


``````````diff
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 04496c76e07201..1424a526e678fc 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -871,7 +871,11 @@ namespace llvm {
   /// @{
 
   inline bool operator==(StringRef LHS, StringRef RHS) {
-    return LHS.equals(RHS);
+    if (LHS.size() != RHS.size())
+      return 0;
+    if (LHS.empty())
+      return 1;
+    return ::memcmp(LHS.data(), RHS.data(), LHS.size()) == 0;
   }
 
   inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }

``````````

</details>


https://github.com/llvm/llvm-project/pull/91139


More information about the llvm-commits mailing list