[llvm] [llvm] Prefer StringRef::substr to StringRef::slice (NFC) (PR #106190)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 26 23:16:39 PDT 2024
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/106190
S.substr(N, M) is simpler than S.slice(N, N + M). Also, substr is
probably better recognizable than slice thanks to
std::string_view::substr.
>From 015ec225e5c7f2798c5dc29ca3a21447a80a7ede Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 15 Aug 2024 10:07:05 -0700
Subject: [PATCH] [llvm] Prefer StringRef::substr to StringRef::slice (NFC)
S.substr(N, M) is simpler than S.slice(N, N + M). Also, substr is
probably better recognizable than slice thanks to
std::string_view::substr.
---
llvm/lib/MC/MCParser/AsmParser.cpp | 2 +-
llvm/lib/MC/MCParser/MasmParser.cpp | 2 +-
llvm/lib/Object/COFFObjectFile.cpp | 2 +-
llvm/lib/Object/MachOObjectFile.cpp | 18 ++++++++----------
llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp | 2 +-
llvm/utils/TableGen/AsmMatcherEmitter.cpp | 4 ++--
6 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index af09f2a0ee0cd4..66e52fe2d08f8d 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -5784,7 +5784,7 @@ bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
: A[0][0].getString();
for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
MCAsmMacroArgument Arg;
- Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
+ Arg.emplace_back(AsmToken::Identifier, Values.substr(I, 1));
// Note that the AtPseudoVariable is enabled for instantiations of .irpc.
// This is undocumented, but GAS seems to support it.
diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index f64b7f62d61d09..9f619c5018b509 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -7125,7 +7125,7 @@ bool MasmParser::parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive) {
StringRef Values(Argument);
for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
MCAsmMacroArgument Arg;
- Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
+ Arg.emplace_back(AsmToken::Identifier, Values.substr(I, 1));
if (expandMacro(OS, M->Body, Parameter, Arg, M->Locals, getTok().getLoc()))
return true;
diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp
index ff7129ba178cf5..5fdf3baf8c02cc 100644
--- a/llvm/lib/Object/COFFObjectFile.cpp
+++ b/llvm/lib/Object/COFFObjectFile.cpp
@@ -2369,7 +2369,7 @@ ResourceSectionRef::getContents(const coff_resource_data_entry &Entry) {
Expected<StringRef> Contents = S.getContents();
if (!Contents)
return Contents.takeError();
- return Contents->slice(Offset, Offset + Entry.DataSize);
+ return Contents->substr(Offset, Entry.DataSize);
}
}
return createStringError(object_error::parse_failed,
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp
index 6f3dd4d8b51801..8fa3f67ea00f3f 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -2099,7 +2099,7 @@ ArrayRef<uint8_t> getSegmentContents(const MachOObjectFile &Obj,
}
auto &Segment = SegmentOrErr.get();
return arrayRefFromStringRef(
- Obj.getData().slice(Segment.fileoff, Segment.fileoff + Segment.filesize));
+ Obj.getData().substr(Segment.fileoff, Segment.filesize));
}
} // namespace
@@ -2454,9 +2454,8 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
Idx = 0;
else
Idx = b+1;
- F = Name.slice(Idx, Idx + Foo.size());
- DotFramework = Name.slice(Idx + Foo.size(),
- Idx + Foo.size() + sizeof(".framework/")-1);
+ F = Name.substr(Idx, Foo.size());
+ DotFramework = Name.substr(Idx + Foo.size(), sizeof(".framework/") - 1);
if (F == Foo && DotFramework == ".framework/") {
isFramework = true;
return Foo;
@@ -2476,9 +2475,8 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
Idx = 0;
else
Idx = d+1;
- F = Name.slice(Idx, Idx + Foo.size());
- DotFramework = Name.slice(Idx + Foo.size(),
- Idx + Foo.size() + sizeof(".framework/")-1);
+ F = Name.substr(Idx, Foo.size());
+ DotFramework = Name.substr(Idx + Foo.size(), sizeof(".framework/") - 1);
if (F == Foo && DotFramework == ".framework/") {
isFramework = true;
return Foo;
@@ -2495,7 +2493,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
// First pull off the version letter for the form Foo.A.dylib if any.
if (a >= 3) {
- Dot = Name.slice(a-2, a-1);
+ Dot = Name.substr(a - 2, 1);
if (Dot == ".")
a = a - 2;
}
@@ -2520,7 +2518,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
// There are incorrect library names of the form:
// libATS.A_profile.dylib so check for these.
if (Lib.size() >= 3) {
- Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
+ Dot = Lib.substr(Lib.size() - 2, 1);
if (Dot == ".")
Lib = Lib.slice(0, Lib.size()-2);
}
@@ -2537,7 +2535,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
Lib = Name.slice(b+1, a);
// There are library names of the form: QT.A.qtx so check for these.
if (Lib.size() >= 3) {
- Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
+ Dot = Lib.substr(Lib.size() - 2, 1);
if (Dot == ".")
Lib = Lib.slice(0, Lib.size()-2);
}
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index eda3c9fd50bf56..864b7d8e769ab1 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -1956,7 +1956,7 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
if (DotOffset != StringRef::npos) {
consumeToken();
StringRef LHS = Identifier.slice(0, DotOffset);
- StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1);
+ StringRef Dot = Identifier.substr(DotOffset, 1);
StringRef RHS = Identifier.substr(DotOffset + 1);
if (!RHS.empty()) {
getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS));
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index 3b630e3cf014e7..f351087ad212f7 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -1013,7 +1013,7 @@ void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
InTok = false;
IsIsolatedToken = false;
}
- addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
+ addAsmOperand(String.substr(i, 1), IsIsolatedToken);
Prev = i + 1;
IsIsolatedToken = true;
continue;
@@ -1037,7 +1037,7 @@ void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
}
++i;
assert(i != String.size() && "Invalid quoted character");
- addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
+ addAsmOperand(String.substr(i, 1), IsIsolatedToken);
Prev = i + 1;
IsIsolatedToken = false;
break;
More information about the llvm-commits
mailing list