Skip to content

Conversation

@jurahul
Copy link
Contributor

@jurahul jurahul commented Jul 10, 2025

Change ascii_strncasecmp to use a range for loop and use StringRef parameters.

Change `ascii_strncasecmp` to use a range for loop and use
StringRef parameters.
@jurahul jurahul marked this pull request as ready for review July 10, 2025 18:36
@jurahul jurahul requested review from kazutakahirata and kuhar July 10, 2025 18:36
@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-llvm-support

Author: Rahul Joshi (jurahul)

Changes

Change ascii_strncasecmp to use a range for loop and use StringRef parameters.


Full diff: https://github.com/llvm/llvm-project/pull/147994.diff

1 Files Affected:

  • (modified) llvm/lib/Support/StringRef.cpp (+8-9)
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index 96829bd062a78..dc758785e40d5 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -24,10 +24,10 @@ constexpr size_t StringRef::npos;
 
 // strncasecmp() is not available on non-POSIX systems, so define an
 // alternative function here.
-static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
-  for (size_t I = 0; I < Length; ++I) {
-    unsigned char LHC = toLower(LHS[I]);
-    unsigned char RHC = toLower(RHS[I]);
+static int ascii_strncasecmp(StringRef LHS, StringRef RHS) {
+  for (auto [LC, RC] : zip_equal(LHS, RHS)) {
+    unsigned char LHC = toLower(LC);
+    unsigned char RHC = toLower(RC);
     if (LHC != RHC)
       return LHC < RHC ? -1 : 1;
   }
@@ -35,8 +35,8 @@ static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
 }
 
 int StringRef::compare_insensitive(StringRef RHS) const {
-  if (int Res =
-          ascii_strncasecmp(data(), RHS.data(), std::min(size(), RHS.size())))
+  size_t Min = std::min(size(), RHS.size());
+  if (int Res = ascii_strncasecmp(take_front(Min), RHS.take_front(Min)))
     return Res;
   if (size() == RHS.size())
     return 0;
@@ -45,13 +45,12 @@ int StringRef::compare_insensitive(StringRef RHS) const {
 
 bool StringRef::starts_with_insensitive(StringRef Prefix) const {
   return size() >= Prefix.size() &&
-         ascii_strncasecmp(data(), Prefix.data(), Prefix.size()) == 0;
+         ascii_strncasecmp(take_front(Prefix.size()), Prefix) == 0;
 }
 
 bool StringRef::ends_with_insensitive(StringRef Suffix) const {
   return size() >= Suffix.size() &&
-         ascii_strncasecmp(end() - Suffix.size(), Suffix.data(),
-                           Suffix.size()) == 0;
+         ascii_strncasecmp(take_back(Suffix.size()), Suffix) == 0;
 }
 
 size_t StringRef::find_insensitive(char C, size_t From) const {

@jurahul jurahul merged commit d8a2141 into llvm:main Jul 10, 2025
13 checks passed
@jurahul jurahul deleted the string_case_insensitive_cleanup branch July 10, 2025 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants