Skip to content

Commit 7ee6288

Browse files
[Support] Use StringRef::operator== instead of StringRef::equals (NFC) (#91042)
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator== outnumbers StringRef::equals by a factor of 25 under llvm/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
1 parent 76aa042 commit 7ee6288

File tree

8 files changed

+17
-18
lines changed

8 files changed

+17
-18
lines changed

llvm/include/llvm/Support/VirtualFileSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ class RedirectingFileSystem
961961
// that, other than the root, path components should not contain slashes or
962962
// backslashes.
963963
bool pathComponentMatches(llvm::StringRef lhs, llvm::StringRef rhs) const {
964-
if ((CaseSensitive ? lhs.equals(rhs) : lhs.equals_insensitive(rhs)))
964+
if ((CaseSensitive ? lhs == rhs : lhs.equals_insensitive(rhs)))
965965
return true;
966966
return (lhs == "/" && rhs == "\\") || (lhs == "\\" && rhs == "/");
967967
}

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,18 @@ inline bool isNumeric(StringRef S) {
567567

568568
// Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls
569569
// safe.
570-
if (S.empty() || S.equals("+") || S.equals("-"))
570+
if (S.empty() || S == "+" || S == "-")
571571
return false;
572572

573-
if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
573+
if (S == ".nan" || S == ".NaN" || S == ".NAN")
574574
return true;
575575

576576
// Infinity and decimal numbers can be prefixed with sign.
577577
StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S;
578578

579579
// Check for infinity first, because checking for hex and oct numbers is more
580580
// expensive.
581-
if (Tail.equals(".inf") || Tail.equals(".Inf") || Tail.equals(".INF"))
581+
if (Tail == ".inf" || Tail == ".Inf" || Tail == ".INF")
582582
return true;
583583

584584
// Section 10.3.2 Tag Resolution
@@ -599,7 +599,7 @@ inline bool isNumeric(StringRef S) {
599599
// digit after dot (as opposed by number which has digits before the dot), but
600600
// doesn't have one.
601601
if (S.starts_with(".") &&
602-
(S.equals(".") ||
602+
(S == "." ||
603603
(S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr)))
604604
return false;
605605

@@ -656,14 +656,13 @@ inline bool isNumeric(StringRef S) {
656656
}
657657

658658
inline bool isNull(StringRef S) {
659-
return S.equals("null") || S.equals("Null") || S.equals("NULL") ||
660-
S.equals("~");
659+
return S == "null" || S == "Null" || S == "NULL" || S == "~";
661660
}
662661

663662
inline bool isBool(StringRef S) {
664663
// FIXME: using parseBool is causing multiple tests to fail.
665-
return S.equals("true") || S.equals("True") || S.equals("TRUE") ||
666-
S.equals("false") || S.equals("False") || S.equals("FALSE");
664+
return S == "true" || S == "True" || S == "TRUE" || S == "false" ||
665+
S == "False" || S == "FALSE";
667666
}
668667

669668
// 5.1. Character Set

llvm/lib/Support/APFloat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,7 +3123,7 @@ bool IEEEFloat::convertFromStringSpecials(StringRef str) {
31233123
if (str.size() < MIN_NAME_SIZE)
31243124
return false;
31253125

3126-
if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) {
3126+
if (str == "inf" || str == "INFINITY" || str == "+Inf") {
31273127
makeInf(false);
31283128
return true;
31293129
}
@@ -3134,7 +3134,7 @@ bool IEEEFloat::convertFromStringSpecials(StringRef str) {
31343134
if (str.size() < MIN_NAME_SIZE)
31353135
return false;
31363136

3137-
if (str.equals("inf") || str.equals("INFINITY") || str.equals("Inf")) {
3137+
if (str == "inf" || str == "INFINITY" || str == "Inf") {
31383138
makeInf(true);
31393139
return true;
31403140
}

llvm/lib/Support/CodeGenCoverage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
5353
if (CurPtr == Buffer.getBufferEnd())
5454
return false; // Data is invalid, expected rule id's to follow.
5555

56-
bool IsForThisBackend = BackendName.equals(LexedBackendName);
56+
bool IsForThisBackend = BackendName == LexedBackendName;
5757
while (CurPtr != Buffer.getBufferEnd()) {
5858
if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
5959
return false; // Data is invalid. Not enough bytes for another rule id.

llvm/lib/Support/FileCollector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static bool isCaseSensitivePath(StringRef Path) {
4444
// sensitive in the absence of real_path, since this is the YAMLVFSWriter
4545
// default.
4646
UpperDest = Path.upper();
47-
if (!sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
47+
if (!sys::fs::real_path(UpperDest, RealDest) && Path == RealDest)
4848
return false;
4949
return true;
5050
}

llvm/lib/Support/JSON.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void Path::Root::printErrorContext(const Value &R, raw_ostream &OS) const {
336336
JOS.object([&] {
337337
for (const auto *KV : sortedElements(*O)) {
338338
JOS.attributeBegin(KV->first);
339-
if (FieldName.equals(KV->first))
339+
if (FieldName == StringRef(KV->first))
340340
Recurse(KV->second, Path.drop_back(), Recurse);
341341
else
342342
abbreviate(KV->second, JOS);

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void FileSystem::dump() const { print(dbgs(), PrintType::RecursiveContents); }
157157

158158
#ifndef NDEBUG
159159
static bool isTraversalComponent(StringRef Component) {
160-
return Component.equals("..") || Component.equals(".");
160+
return Component == ".." || Component == ".";
161161
}
162162

163163
static bool pathHasTraversal(StringRef Path) {

llvm/lib/Support/YAMLTraits.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bool Input::mapTag(StringRef Tag, bool Default) {
120120
return Default;
121121
}
122122
// Return true iff found tag matches supplied tag.
123-
return Tag.equals(foundTag);
123+
return Tag == foundTag;
124124
}
125125

126126
void Input::beginMapping() {
@@ -271,7 +271,7 @@ bool Input::matchEnumScalar(const char *Str, bool) {
271271
if (ScalarMatchFound)
272272
return false;
273273
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
274-
if (SN->value().equals(Str)) {
274+
if (SN->value() == Str) {
275275
ScalarMatchFound = true;
276276
return true;
277277
}
@@ -310,7 +310,7 @@ bool Input::bitSetMatch(const char *Str, bool) {
310310
unsigned Index = 0;
311311
for (auto &N : SQ->Entries) {
312312
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
313-
if (SN->value().equals(Str)) {
313+
if (SN->value() == Str) {
314314
BitValuesUsed[Index] = true;
315315
return true;
316316
}

0 commit comments

Comments
 (0)