Skip to content

Commit 71eebe9

Browse files
[llvm] Prefer StringRef::substr to StringRef::slice (NFC) (#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.
1 parent 78505ad commit 71eebe9

File tree

6 files changed

+14
-16
lines changed

6 files changed

+14
-16
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5784,7 +5784,7 @@ bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
57845784
: A[0][0].getString();
57855785
for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
57865786
MCAsmMacroArgument Arg;
5787-
Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
5787+
Arg.emplace_back(AsmToken::Identifier, Values.substr(I, 1));
57885788

57895789
// Note that the AtPseudoVariable is enabled for instantiations of .irpc.
57905790
// This is undocumented, but GAS seems to support it.

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7125,7 +7125,7 @@ bool MasmParser::parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive) {
71257125
StringRef Values(Argument);
71267126
for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
71277127
MCAsmMacroArgument Arg;
7128-
Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
7128+
Arg.emplace_back(AsmToken::Identifier, Values.substr(I, 1));
71297129

71307130
if (expandMacro(OS, M->Body, Parameter, Arg, M->Locals, getTok().getLoc()))
71317131
return true;

llvm/lib/Object/COFFObjectFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ ResourceSectionRef::getContents(const coff_resource_data_entry &Entry) {
23692369
Expected<StringRef> Contents = S.getContents();
23702370
if (!Contents)
23712371
return Contents.takeError();
2372-
return Contents->slice(Offset, Offset + Entry.DataSize);
2372+
return Contents->substr(Offset, Entry.DataSize);
23732373
}
23742374
}
23752375
return createStringError(object_error::parse_failed,

llvm/lib/Object/MachOObjectFile.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ ArrayRef<uint8_t> getSegmentContents(const MachOObjectFile &Obj,
20992099
}
21002100
auto &Segment = SegmentOrErr.get();
21012101
return arrayRefFromStringRef(
2102-
Obj.getData().slice(Segment.fileoff, Segment.fileoff + Segment.filesize));
2102+
Obj.getData().substr(Segment.fileoff, Segment.filesize));
21032103
}
21042104
} // namespace
21052105

@@ -2454,9 +2454,8 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
24542454
Idx = 0;
24552455
else
24562456
Idx = b+1;
2457-
F = Name.slice(Idx, Idx + Foo.size());
2458-
DotFramework = Name.slice(Idx + Foo.size(),
2459-
Idx + Foo.size() + sizeof(".framework/")-1);
2457+
F = Name.substr(Idx, Foo.size());
2458+
DotFramework = Name.substr(Idx + Foo.size(), sizeof(".framework/") - 1);
24602459
if (F == Foo && DotFramework == ".framework/") {
24612460
isFramework = true;
24622461
return Foo;
@@ -2476,9 +2475,8 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
24762475
Idx = 0;
24772476
else
24782477
Idx = d+1;
2479-
F = Name.slice(Idx, Idx + Foo.size());
2480-
DotFramework = Name.slice(Idx + Foo.size(),
2481-
Idx + Foo.size() + sizeof(".framework/")-1);
2478+
F = Name.substr(Idx, Foo.size());
2479+
DotFramework = Name.substr(Idx + Foo.size(), sizeof(".framework/") - 1);
24822480
if (F == Foo && DotFramework == ".framework/") {
24832481
isFramework = true;
24842482
return Foo;
@@ -2495,7 +2493,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
24952493

24962494
// First pull off the version letter for the form Foo.A.dylib if any.
24972495
if (a >= 3) {
2498-
Dot = Name.slice(a-2, a-1);
2496+
Dot = Name.substr(a - 2, 1);
24992497
if (Dot == ".")
25002498
a = a - 2;
25012499
}
@@ -2520,7 +2518,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
25202518
// There are incorrect library names of the form:
25212519
// libATS.A_profile.dylib so check for these.
25222520
if (Lib.size() >= 3) {
2523-
Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
2521+
Dot = Lib.substr(Lib.size() - 2, 1);
25242522
if (Dot == ".")
25252523
Lib = Lib.slice(0, Lib.size()-2);
25262524
}
@@ -2537,7 +2535,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
25372535
Lib = Name.slice(b+1, a);
25382536
// There are library names of the form: QT.A.qtx so check for these.
25392537
if (Lib.size() >= 3) {
2540-
Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
2538+
Dot = Lib.substr(Lib.size() - 2, 1);
25412539
if (Dot == ".")
25422540
Lib = Lib.slice(0, Lib.size()-2);
25432541
}

llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
19561956
if (DotOffset != StringRef::npos) {
19571957
consumeToken();
19581958
StringRef LHS = Identifier.slice(0, DotOffset);
1959-
StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1);
1959+
StringRef Dot = Identifier.substr(DotOffset, 1);
19601960
StringRef RHS = Identifier.substr(DotOffset + 1);
19611961
if (!RHS.empty()) {
19621962
getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS));

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
10131013
InTok = false;
10141014
IsIsolatedToken = false;
10151015
}
1016-
addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
1016+
addAsmOperand(String.substr(i, 1), IsIsolatedToken);
10171017
Prev = i + 1;
10181018
IsIsolatedToken = true;
10191019
continue;
@@ -1037,7 +1037,7 @@ void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
10371037
}
10381038
++i;
10391039
assert(i != String.size() && "Invalid quoted character");
1040-
addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
1040+
addAsmOperand(String.substr(i, 1), IsIsolatedToken);
10411041
Prev = i + 1;
10421042
IsIsolatedToken = false;
10431043
break;

0 commit comments

Comments
 (0)