Skip to content

Commit

Permalink
[llvm] Prefer StringRef::substr to StringRef::slice (NFC) (llvm#105943)
Browse files Browse the repository at this point in the history
S.substr(N) is simpler than S.slice(N, StringRef::npos) and
S.slice(N, S.size()). Also, substr is probably better recognizable
than slice thanks to std::string_view::substr.
  • Loading branch information
kazutakahirata authored Aug 25, 2024
1 parent 33f3ebc commit 33e7cd6
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions llvm/include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ namespace llvm {
size_t Idx = find(Separator);
if (Idx == npos)
return std::make_pair(*this, StringRef());
return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
}

/// Split into two substrings around the last occurrence of a separator
Expand All @@ -731,7 +731,7 @@ namespace llvm {
size_t Idx = rfind(Separator);
if (Idx == npos)
return std::make_pair(*this, StringRef());
return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
}

/// Split into substrings around the occurrences of a separator string.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/MIRParser/MIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,

void MIParser::lex(unsigned SkipChar) {
CurrentSource = lexMIToken(
CurrentSource.slice(SkipChar, StringRef::npos), Token,
CurrentSource.substr(SkipChar), Token,
[this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
}

Expand Down Expand Up @@ -2306,7 +2306,7 @@ bool MIParser::parseDIExpression(MDNode *&Expr) {
Expr = llvm::parseDIExpressionBodyAtBeginning(
CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
&PFS.IRSlots);
CurrentSource = CurrentSource.slice(Read, StringRef::npos);
CurrentSource = CurrentSource.substr(Read);
lex();
if (!Expr)
return error(Error.getMessage());
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MC/MCAsmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ void MCAsmStreamer::addExplicitComment(const Twine &T) {
ExplicitCommentToEmit.append("\t");
ExplicitCommentToEmit.append(MAI->getCommentString());
// drop //
ExplicitCommentToEmit.append(c.slice(2, c.size()).str());
ExplicitCommentToEmit.append(c.substr(2).str());
} else if (c.starts_with(StringRef("/*"))) {
size_t p = 2, len = c.size() - 2;
// emit each line in comment as separate newline.
Expand All @@ -512,7 +512,7 @@ void MCAsmStreamer::addExplicitComment(const Twine &T) {

ExplicitCommentToEmit.append("\t");
ExplicitCommentToEmit.append(MAI->getCommentString());
ExplicitCommentToEmit.append(c.slice(1, c.size()).str());
ExplicitCommentToEmit.append(c.substr(1).str());
} else
assert(false && "Unexpected Assembly Comment");
// full line comments immediately output
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2436,12 +2436,12 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
a = Name.rfind('/');
if (a == Name.npos || a == 0)
goto guess_library;
Foo = Name.slice(a+1, Name.npos);
Foo = Name.substr(a + 1);

// Look for a suffix starting with a '_'
Idx = Foo.rfind('_');
if (Idx != Foo.npos && Foo.size() >= 2) {
Suffix = Foo.slice(Idx, Foo.npos);
Suffix = Foo.substr(Idx);
if (Suffix != "_debug" && Suffix != "_profile")
Suffix = StringRef();
else
Expand All @@ -2468,7 +2468,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
c = Name.rfind('/', b);
if (c == Name.npos || c == 0)
goto guess_library;
V = Name.slice(c+1, Name.npos);
V = Name.substr(c + 1);
if (!V.starts_with("Versions/"))
goto guess_library;
d = Name.rfind('/', c);
Expand All @@ -2489,7 +2489,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
a = Name.rfind('.');
if (a == Name.npos || a == 0)
return StringRef();
Dylib = Name.slice(a, Name.npos);
Dylib = Name.substr(a);
if (Dylib != ".dylib")
goto guess_qtx;

Expand Down Expand Up @@ -2527,7 +2527,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
return Lib;

guess_qtx:
Qtx = Name.slice(a, Name.npos);
Qtx = Name.substr(a);
if (Qtx != ".qtx")
return StringRef();
b = Name.rfind('/', a);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Support/StringRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A,
A.push_back(S.slice(0, Idx));

// Jump forward.
S = S.slice(Idx + Separator.size(), npos);
S = S.substr(Idx + Separator.size());
}

// Push the tail.
Expand All @@ -354,7 +354,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
A.push_back(S.slice(0, Idx));

// Jump forward.
S = S.slice(Idx + 1, npos);
S = S.substr(Idx + 1);
}

// Push the tail.
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Support/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,7 @@ bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
assert(!Parent.empty());
assert(containedIn(Parent, Path));
return Path.slice(Parent.size() + 1, StringRef::npos);
return Path.substr(Parent.size() + 1);
}

void JSONWriter::startDirectory(StringRef Path) {
Expand Down Expand Up @@ -2846,7 +2846,7 @@ void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
if (UseOverlayRelative) {
assert(RPath.starts_with(OverlayDir) &&
"Overlay dir must be contained in RPath");
RPath = RPath.slice(OverlayDir.size(), RPath.size());
RPath = RPath.substr(OverlayDir.size());
}

bool IsCurrentDirEmpty = true;
Expand Down Expand Up @@ -2879,7 +2879,7 @@ void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
if (UseOverlayRelative) {
assert(RPath.starts_with(OverlayDir) &&
"Overlay dir must be contained in RPath");
RPath = RPath.slice(OverlayDir.size(), RPath.size());
RPath = RPath.substr(OverlayDir.size());
}
if (!Entry.IsDirectory) {
writeEntry(path::filename(Entry.VPath), RPath);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4183,7 +4183,7 @@ ParseStatus AArch64AsmParser::tryParseVectorRegister(MCRegister &Reg,

if (RegNum) {
if (Next != StringRef::npos) {
Kind = Name.slice(Next, StringRef::npos);
Kind = Name.substr(Next);
if (!isValidVectorKind(Kind, MatchKind))
return TokError("invalid vector kind qualifier");
}
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5251,7 +5251,7 @@ ParseStatus ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
StringRef Flags = "";
std::string SpecReg = Mask.slice(Start, Next).lower();
if (Next != StringRef::npos)
Flags = Mask.slice(Next+1, Mask.size());
Flags = Mask.substr(Next + 1);

// FlagsVal contains the complete mask:
// 3-0: Mask
Expand Down Expand Up @@ -6648,15 +6648,15 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,

// The "it" instruction has the condition mask on the end of the mnemonic.
if (Mnemonic.starts_with("it")) {
ITMask = Mnemonic.slice(2, Mnemonic.size());
ITMask = Mnemonic.substr(2);
Mnemonic = Mnemonic.slice(0, 2);
}

if (Mnemonic.starts_with("vpst")) {
ITMask = Mnemonic.slice(4, Mnemonic.size());
ITMask = Mnemonic.substr(4);
Mnemonic = Mnemonic.slice(0, 4);
} else if (Mnemonic.starts_with("vpt")) {
ITMask = Mnemonic.slice(3, Mnemonic.size());
ITMask = Mnemonic.substr(3);
Mnemonic = Mnemonic.slice(0, 3);
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64()));
if (Dot != StringRef::npos) {
SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot);
StringRef DotStr = Name.slice(Dot, StringRef::npos);
StringRef DotStr = Name.substr(Dot);
if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
Operands.push_back(
PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64()));
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
consumeToken();
StringRef LHS = Identifier.slice(0, DotOffset);
StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1);
StringRef RHS = Identifier.slice(DotOffset + 1, StringRef::npos);
StringRef RHS = Identifier.substr(DotOffset + 1);
if (!RHS.empty()) {
getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS));
}
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/TargetParser/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {

size_t Idx = Arch.find('_');
StringRef Ext = Arch.slice(0, Idx);
Arch = Arch.slice(Idx, StringRef::npos);
Arch = Arch.substr(Idx);

StringRef Prefix, MinorVersionStr;
std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
Expand All @@ -533,7 +533,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
return getError("missing extension name");

StringRef ExtName = Prefix.slice(0, VersionStart);
StringRef MajorVersionStr = Prefix.slice(VersionStart, StringRef::npos);
StringRef MajorVersionStr = Prefix.substr(VersionStart);
if (MajorVersionStr.getAsInteger(10, MajorVersion))
return getError("failed to parse major version number");

Expand Down Expand Up @@ -662,7 +662,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,

size_t Idx = Arch.find('_');
StringRef Ext = Arch.slice(0, Idx);
Arch = Arch.slice(Idx, StringRef::npos);
Arch = Arch.substr(Idx);

do {
StringRef Name, Vers, Desc;
Expand Down
4 changes: 2 additions & 2 deletions llvm/utils/TableGen/AsmMatcherEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,14 @@ parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
size_t start = Ops.first.find_first_of('$');
if (start == std::string::npos)
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
Ops.first = Ops.first.slice(start + 1, std::string::npos);
Ops.first = Ops.first.substr(start + 1);
size_t end = Ops.first.find_last_of(" \t");
Ops.first = Ops.first.slice(0, end);
// Now the second operand.
start = Ops.second.find_first_of('$');
if (start == std::string::npos)
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
Ops.second = Ops.second.slice(start + 1, std::string::npos);
Ops.second = Ops.second.substr(start + 1);
end = Ops.second.find_last_of(" \t");
Ops.first = Ops.first.slice(0, end);
return Ops;
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/OptParserEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static MarshallingInfo createMarshallingInfo(const Record &R) {
break;
if (Idx > 0)
Ret.Values.push_back(ValuesStr.slice(0, Idx));
ValuesStr = ValuesStr.slice(Idx + 1, StringRef::npos);
ValuesStr = ValuesStr.substr(Idx + 1);
}
if (!ValuesStr.empty())
Ret.Values.push_back(ValuesStr);
Expand Down

0 comments on commit 33e7cd6

Please sign in to comment.