-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[llvm] Prefer StringRef::substr to StringRef::slice (NFC) #106190
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
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_001_StringRef_slice_offset
Aug 27, 2024
Merged
[llvm] Prefer StringRef::substr to StringRef::slice (NFC) #106190
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_001_StringRef_slice_offset
Aug 27, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/pr-subscribers-backend-x86 @llvm/pr-subscribers-llvm-binary-utilities Author: Kazu Hirata (kazutakahirata) ChangesS.substr(N, M) is simpler than S.slice(N, N + M). Also, substr is Full diff: https://github.com/llvm/llvm-project/pull/106190.diff 6 Files Affected:
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;
|
d0k
approved these changes
Aug 27, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.