[llvm] 774b7eb - [ADT] Reimplement operator==(StringRef, StringRef) (NFC) (#91139)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 5 20:08:11 PDT 2024


Author: Kazu Hirata
Date: 2024-05-05T20:08:06-07:00
New Revision: 774b7eb7bacceaca3986fc73a236c3cd44f28599

URL: https://github.com/llvm/llvm-project/commit/774b7eb7bacceaca3986fc73a236c3cd44f28599
DIFF: https://github.com/llvm/llvm-project/commit/774b7eb7bacceaca3986fc73a236c3cd44f28599.diff

LOG: [ADT] Reimplement operator==(StringRef, StringRef) (NFC) (#91139)

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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringRef.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 04496c76e07201..8ed8e424cfe13c 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 false;
+    if (LHS.empty())
+      return true;
+    return ::memcmp(LHS.data(), RHS.data(), LHS.size()) == 0;
   }
 
   inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }


        


More information about the llvm-commits mailing list