Skip to content

Commit bcb685e

Browse files
committed
[Support] Use StringRef::starts_with/ends_with instead of startswith/endswith. NFC.
startswith/endswith wrap starts_with/ends_with and will eventually go away (to more closely match string_view)
1 parent f7cd619 commit bcb685e

16 files changed

+41
-41
lines changed

llvm/include/llvm/Support/TypeName.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ inline StringRef getTypeName() {
3333
assert(!Name.empty() && "Unable to find the template parameter!");
3434
Name = Name.drop_front(Key.size());
3535

36-
assert(Name.endswith("]") && "Name doesn't end in the substitution key!");
36+
assert(Name.ends_with("]") && "Name doesn't end in the substitution key!");
3737
return Name.drop_back(1);
3838
#elif defined(_MSC_VER)
3939
StringRef Name = __FUNCSIG__;
@@ -44,7 +44,7 @@ inline StringRef getTypeName() {
4444
Name = Name.drop_front(Key.size());
4545

4646
for (StringRef Prefix : {"class ", "struct ", "union ", "enum "})
47-
if (Name.startswith(Prefix)) {
47+
if (Name.starts_with(Prefix)) {
4848
Name = Name.drop_front(Prefix.size());
4949
break;
5050
}

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,11 @@ inline bool isNumeric(StringRef S) {
584584
// Section 10.3.2 Tag Resolution
585585
// YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with
586586
// [-+], so S should be used instead of Tail.
587-
if (S.startswith("0o"))
587+
if (S.starts_with("0o"))
588588
return S.size() > 2 &&
589589
S.drop_front(2).find_first_not_of("01234567") == StringRef::npos;
590590

591-
if (S.startswith("0x"))
591+
if (S.starts_with("0x"))
592592
return S.size() > 2 && S.drop_front(2).find_first_not_of(
593593
"0123456789abcdefABCDEF") == StringRef::npos;
594594

@@ -598,12 +598,12 @@ inline bool isNumeric(StringRef S) {
598598
// Handle cases when the number starts with '.' and hence needs at least one
599599
// digit after dot (as opposed by number which has digits before the dot), but
600600
// doesn't have one.
601-
if (S.startswith(".") &&
601+
if (S.starts_with(".") &&
602602
(S.equals(".") ||
603603
(S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr)))
604604
return false;
605605

606-
if (S.startswith("E") || S.startswith("e"))
606+
if (S.starts_with("E") || S.starts_with("e"))
607607
return false;
608608

609609
enum ParseState {

llvm/lib/Support/APFloat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3148,7 +3148,7 @@ bool IEEEFloat::convertFromStringSpecials(StringRef str) {
31483148
return false;
31493149
}
31503150

3151-
if (str.startswith("nan") || str.startswith("NaN")) {
3151+
if (str.starts_with("nan") || str.starts_with("NaN")) {
31523152
str = str.drop_front(3);
31533153

31543154
// A NaN without payload.

llvm/lib/Support/CachePruning.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy,
218218
// This acts as a safeguard against data loss if the user specifies the
219219
// wrong directory as their cache directory.
220220
StringRef filename = sys::path::filename(File->path());
221-
if (!filename.startswith("llvmcache-") && !filename.startswith("Thin-"))
221+
if (!filename.starts_with("llvmcache-") && !filename.starts_with("Thin-"))
222222
continue;
223223

224224
// Look at this file. If we can't stat it, there's nothing interesting

llvm/lib/Support/DebugCounter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void DebugCounter::push_back(const std::string &Val) {
100100
}
101101
// Now we need to see if this is the skip or the count, remove the suffix, and
102102
// add it to the counter values.
103-
if (CounterPair.first.endswith("-skip")) {
103+
if (CounterPair.first.ends_with("-skip")) {
104104
auto CounterName = CounterPair.first.drop_back(5);
105105
unsigned CounterID = getCounterId(std::string(CounterName));
106106
if (!CounterID) {
@@ -113,7 +113,7 @@ void DebugCounter::push_back(const std::string &Val) {
113113
CounterInfo &Counter = Counters[CounterID];
114114
Counter.Skip = CounterVal;
115115
Counter.IsSet = true;
116-
} else if (CounterPair.first.endswith("-count")) {
116+
} else if (CounterPair.first.ends_with("-count")) {
117117
auto CounterName = CounterPair.first.drop_back(6);
118118
unsigned CounterID = getCounterId(std::string(CounterName));
119119
if (!CounterID) {

llvm/lib/Support/ELFAttributes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ StringRef ELFAttrs::attrTypeAsString(unsigned attr, TagNameMap tagNameMap,
2323

2424
std::optional<unsigned> ELFAttrs::attrTypeFromString(StringRef tag,
2525
TagNameMap tagNameMap) {
26-
bool hasTagPrefix = tag.startswith("Tag_");
26+
bool hasTagPrefix = tag.starts_with("Tag_");
2727
auto tagNameIt =
2828
find_if(tagNameMap, [tag, hasTagPrefix](const TagNameItem item) {
2929
return item.tagName.drop_front(hasTagPrefix ? 0 : 4) == tag;

llvm/lib/Support/Path.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ const_iterator &const_iterator::operator++() {
263263
// Root dir.
264264
if (was_net ||
265265
// c:/
266-
(is_style_windows(S) && Component.endswith(":"))) {
266+
(is_style_windows(S) && Component.ends_with(":"))) {
267267
Component = Path.substr(Position, 1);
268268
return *this;
269269
}
@@ -352,7 +352,7 @@ StringRef root_path(StringRef path, Style style) {
352352
if (b != e) {
353353
bool has_net =
354354
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
355-
bool has_drive = is_style_windows(style) && b->endswith(":");
355+
bool has_drive = is_style_windows(style) && b->ends_with(":");
356356

357357
if (has_net || has_drive) {
358358
if ((++pos != e) && is_separator((*pos)[0], style)) {
@@ -377,7 +377,7 @@ StringRef root_name(StringRef path, Style style) {
377377
if (b != e) {
378378
bool has_net =
379379
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
380-
bool has_drive = is_style_windows(style) && b->endswith(":");
380+
bool has_drive = is_style_windows(style) && b->ends_with(":");
381381

382382
if (has_net || has_drive) {
383383
// just {C:,//net}, return the first component.
@@ -394,7 +394,7 @@ StringRef root_directory(StringRef path, Style style) {
394394
if (b != e) {
395395
bool has_net =
396396
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
397-
bool has_drive = is_style_windows(style) && b->endswith(":");
397+
bool has_drive = is_style_windows(style) && b->ends_with(":");
398398

399399
if ((has_net || has_drive) &&
400400
// {C:,//net}, skip to the next component.
@@ -514,7 +514,7 @@ static bool starts_with(StringRef Path, StringRef Prefix,
514514
}
515515
return true;
516516
}
517-
return Path.startswith(Prefix);
517+
return Path.starts_with(Prefix);
518518
}
519519

520520
bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,21 +311,21 @@ void RISCVISAInfo::addExtension(StringRef ExtName, unsigned MajorVersion,
311311
}
312312

313313
static StringRef getExtensionTypeDesc(StringRef Ext) {
314-
if (Ext.startswith("s"))
314+
if (Ext.starts_with("s"))
315315
return "standard supervisor-level extension";
316-
if (Ext.startswith("x"))
316+
if (Ext.starts_with("x"))
317317
return "non-standard user-level extension";
318-
if (Ext.startswith("z"))
318+
if (Ext.starts_with("z"))
319319
return "standard user-level extension";
320320
return StringRef();
321321
}
322322

323323
static StringRef getExtensionType(StringRef Ext) {
324-
if (Ext.startswith("s"))
324+
if (Ext.starts_with("s"))
325325
return "s";
326-
if (Ext.startswith("x"))
326+
if (Ext.starts_with("x"))
327327
return "x";
328-
if (Ext.startswith("z"))
328+
if (Ext.starts_with("z"))
329329
return "z";
330330
return StringRef();
331331
}
@@ -641,9 +641,9 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
641641
}
642642
// Must start with a valid base ISA name.
643643
unsigned XLen;
644-
if (Arch.startswith("rv32i") || Arch.startswith("rv32e"))
644+
if (Arch.starts_with("rv32i") || Arch.starts_with("rv32e"))
645645
XLen = 32;
646-
else if (Arch.startswith("rv64i") || Arch.startswith("rv64e"))
646+
else if (Arch.starts_with("rv64i") || Arch.starts_with("rv64e"))
647647
XLen = 64;
648648
else
649649
return createStringError(errc::invalid_argument,
@@ -704,9 +704,9 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
704704
"string must be lowercase");
705705
}
706706

707-
bool HasRV64 = Arch.startswith("rv64");
707+
bool HasRV64 = Arch.starts_with("rv64");
708708
// ISA string must begin with rv32 or rv64.
709-
if (!(Arch.startswith("rv32") || HasRV64) || (Arch.size() < 5)) {
709+
if (!(Arch.starts_with("rv32") || HasRV64) || (Arch.size() < 5)) {
710710
return createStringError(
711711
errc::invalid_argument,
712712
"string must begin with rv32{i,e,g} or rv64{i,e,g}");

llvm/lib/Support/Signals.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace,
238238
if (FunctionName.empty())
239239
break;
240240
PrintLineHeader();
241-
if (!FunctionName.startswith("??"))
241+
if (!FunctionName.starts_with("??"))
242242
OS << FunctionName << ' ';
243243
if (CurLine == Lines.end())
244244
return false;
245245
StringRef FileLineInfo = *CurLine++;
246-
if (!FileLineInfo.startswith("??"))
246+
if (!FileLineInfo.starts_with("??"))
247247
OS << FileLineInfo;
248248
else
249249
OS << "(" << Modules[i] << '+' << format_hex(Offsets[i], 0) << ")";

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
156156
// regexes. If "#!special-case-list-v2" is the first line of the file, then
157157
// we will use the new behavior using globs. For more details, see
158158
// https://discourse.llvm.org/t/use-glob-instead-of-regex-for-specialcaselists/71666
159-
bool UseGlobs = MB->getBuffer().startswith("#!special-case-list-v2\n");
159+
bool UseGlobs = MB->getBuffer().starts_with("#!special-case-list-v2\n");
160160

161161
for (line_iterator LineIt(*MB, /*SkipBlanks=*/true, /*CommentMarker=*/'#');
162162
!LineIt.is_at_eof(); LineIt++) {
@@ -166,8 +166,8 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
166166
continue;
167167

168168
// Save section names
169-
if (Line.startswith("[")) {
170-
if (!Line.endswith("]")) {
169+
if (Line.starts_with("[")) {
170+
if (!Line.ends_with("]")) {
171171
Error =
172172
("malformed section header on line " + Twine(LineNo) + ": " + Line)
173173
.str();

llvm/lib/Support/StringRef.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,17 @@ static unsigned GetAutoSenseRadix(StringRef &Str) {
388388
if (Str.empty())
389389
return 10;
390390

391-
if (Str.startswith("0x") || Str.startswith("0X")) {
391+
if (Str.starts_with("0x") || Str.starts_with("0X")) {
392392
Str = Str.substr(2);
393393
return 16;
394394
}
395395

396-
if (Str.startswith("0b") || Str.startswith("0B")) {
396+
if (Str.starts_with("0b") || Str.starts_with("0B")) {
397397
Str = Str.substr(2);
398398
return 2;
399399
}
400400

401-
if (Str.startswith("0o")) {
401+
if (Str.starts_with("0o")) {
402402
Str = Str.substr(2);
403403
return 8;
404404
}

llvm/lib/Support/UnicodeNameToCodepoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static bool startsWith(StringRef Name, StringRef Needle, bool Strict,
123123

124124
Consummed = 0;
125125
if (Strict) {
126-
if (!Name.startswith(Needle))
126+
if (!Name.starts_with(Needle))
127127
return false;
128128
Consummed = Needle.size();
129129
return true;

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ RedirectingFileSystem::makeAbsolute(StringRef WorkingDir,
13851385

13861386
std::string Result = std::string(WorkingDir);
13871387
StringRef Dir(Result);
1388-
if (!Dir.endswith(sys::path::get_separator(style))) {
1388+
if (!Dir.ends_with(sys::path::get_separator(style))) {
13891389
Result += sys::path::get_separator(style);
13901390
}
13911391
// backslashes '\' are legit path charactors under POSIX. Windows APIs

llvm/lib/Support/Windows/Path.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ static bool isReservedName(StringRef path) {
666666

667667
// First, check to see if this is a device namespace, which always
668668
// starts with \\.\, since device namespaces are not legal file paths.
669-
if (path.startswith("\\\\.\\"))
669+
if (path.starts_with("\\\\.\\"))
670670
return true;
671671

672672
// Then compare against the list of ancient reserved names.
@@ -940,10 +940,10 @@ static bool hasFlushBufferKernelBug() {
940940

941941
static bool isEXE(StringRef Magic) {
942942
static const char PEMagic[] = {'P', 'E', '\0', '\0'};
943-
if (Magic.startswith(StringRef("MZ")) && Magic.size() >= 0x3c + 4) {
943+
if (Magic.starts_with(StringRef("MZ")) && Magic.size() >= 0x3c + 4) {
944944
uint32_t off = read32le(Magic.data() + 0x3c);
945945
// PE/COFF file, either EXE or DLL.
946-
if (Magic.substr(off).startswith(StringRef(PEMagic, sizeof(PEMagic))))
946+
if (Magic.substr(off).starts_with(StringRef(PEMagic, sizeof(PEMagic))))
947947
return true;
948948
}
949949
return false;

llvm/lib/Support/Windows/Process.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static std::error_code WildcardExpand(StringRef Arg,
158158
// option. Paths that start with \\?\ are absolute paths, and aren't
159159
// expected to be used with wildcard expressions.
160160
if (Arg.find_first_of("*?") == StringRef::npos || Arg == "/?" ||
161-
Arg == "-?" || Arg.startswith("\\\\?\\")) {
161+
Arg == "-?" || Arg.starts_with("\\\\?\\")) {
162162
Args.push_back(Arg.data());
163163
return EC;
164164
}

llvm/lib/Support/YAMLParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,7 @@ std::string Node::getVerbatimTag() const {
19681968
Ret = std::string(Doc->getTagMap().find("!")->second);
19691969
Ret += Raw.substr(1);
19701970
return Ret;
1971-
} else if (Raw.startswith("!!")) {
1971+
} else if (Raw.starts_with("!!")) {
19721972
Ret = std::string(Doc->getTagMap().find("!!")->second);
19731973
Ret += Raw.substr(2);
19741974
return Ret;

0 commit comments

Comments
 (0)