Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kazutakahirata
Copy link
Contributor

@kazutakahirata kazutakahirata commented May 5, 2024

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.

@llvmbot
Copy link
Member

llvmbot commented May 5, 2024

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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 --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); }

@MaskRay
Copy link
Member

MaskRay commented May 5, 2024

The title [ADT] Reimplement operator==(StringRef seems truncated.

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.
@kazutakahirata kazutakahirata force-pushed the cleanup_StringRef_equals_reimpl branch from fc0a97b to 2176796 Compare May 5, 2024 20:40
@kazutakahirata kazutakahirata changed the title [ADT] Reimplement operator==(StringRef [ADT] Reimplement operator==(StringRef, StringRef) (NFC) May 5, 2024
@kazutakahirata
Copy link
Contributor Author

I've fixed true/false as wells as the title/commit message. Please take a look. Thanks!

@kazutakahirata kazutakahirata merged commit 774b7eb into llvm:main May 6, 2024
@kazutakahirata kazutakahirata deleted the cleanup_StringRef_equals_reimpl branch May 6, 2024 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants