[llvm] [ADT] Reimplement operator==(StringRef (PR #91139)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun May 5 12:50:26 PDT 2024
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/91139
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.
>From fc0a97b32bd9b510953b9fe26a76d4a1038a3cc7 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 3 May 2024 22:40:49 -0700
Subject: [PATCH] [ADT] Reimplement operator==(StringRef, 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.
---
llvm/include/llvm/ADT/StringRef.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
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); }
More information about the llvm-commits
mailing list