Skip to content

Commit d8a2141

Browse files
authored
[NFC][LLVM][ADT] Simplify StringRef case insensitive compare (#147994)
Change `ascii_strncasecmp` to use a range for loop and use StringRef parameters.
1 parent 0edc98c commit d8a2141

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

llvm/lib/Support/StringRef.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ constexpr size_t StringRef::npos;
2424

2525
// strncasecmp() is not available on non-POSIX systems, so define an
2626
// alternative function here.
27-
static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
28-
for (size_t I = 0; I < Length; ++I) {
29-
unsigned char LHC = toLower(LHS[I]);
30-
unsigned char RHC = toLower(RHS[I]);
27+
static int ascii_strncasecmp(StringRef LHS, StringRef RHS) {
28+
for (auto [LC, RC] : zip_equal(LHS, RHS)) {
29+
unsigned char LHC = toLower(LC);
30+
unsigned char RHC = toLower(RC);
3131
if (LHC != RHC)
3232
return LHC < RHC ? -1 : 1;
3333
}
3434
return 0;
3535
}
3636

3737
int StringRef::compare_insensitive(StringRef RHS) const {
38-
if (int Res =
39-
ascii_strncasecmp(data(), RHS.data(), std::min(size(), RHS.size())))
38+
size_t Min = std::min(size(), RHS.size());
39+
if (int Res = ascii_strncasecmp(take_front(Min), RHS.take_front(Min)))
4040
return Res;
4141
if (size() == RHS.size())
4242
return 0;
@@ -45,13 +45,12 @@ int StringRef::compare_insensitive(StringRef RHS) const {
4545

4646
bool StringRef::starts_with_insensitive(StringRef Prefix) const {
4747
return size() >= Prefix.size() &&
48-
ascii_strncasecmp(data(), Prefix.data(), Prefix.size()) == 0;
48+
ascii_strncasecmp(take_front(Prefix.size()), Prefix) == 0;
4949
}
5050

5151
bool StringRef::ends_with_insensitive(StringRef Suffix) const {
5252
return size() >= Suffix.size() &&
53-
ascii_strncasecmp(end() - Suffix.size(), Suffix.data(),
54-
Suffix.size()) == 0;
53+
ascii_strncasecmp(take_back(Suffix.size()), Suffix) == 0;
5554
}
5655

5756
size_t StringRef::find_insensitive(char C, size_t From) const {

0 commit comments

Comments
 (0)