[llvm] 56686ec - [NFC][ADT] Tweaked some functions in StringRef

Shao-Ce SUN via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 16 10:45:33 PDT 2023


Author: Shao-Ce SUN
Date: 2023-03-17T01:45:24+08:00
New Revision: 56686ec47ee0cc9baf45c0149f63b3ec8ecdb940

URL: https://github.com/llvm/llvm-project/commit/56686ec47ee0cc9baf45c0149f63b3ec8ecdb940
DIFF: https://github.com/llvm/llvm-project/commit/56686ec47ee0cc9baf45c0149f63b3ec8ecdb940.diff

LOG: [NFC][ADT] Tweaked some functions in StringRef

Reduced the usage of variables in the `rfind` and `count` functions.

Replaced calls to `drop_front` with direct use of `substr` in `consume_front`,
since `starts_with` already performs length check on the string. Did the same
for other functions.

Using the `std::clamp` in `slice` makes the code more straightforward.

Reviewed By: bkramer

Differential Revision: https://reviews.llvm.org/D144787

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringRef.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 2d2f0bedfe1f8..868722e19001c 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -343,12 +343,11 @@ namespace llvm {
     /// \returns The index of the last occurrence of \p C, or npos if not
     /// found.
     [[nodiscard]] size_t rfind(char C, size_t From = npos) const {
-      From = std::min(From, Length);
-      size_t i = From;
-      while (i != 0) {
-        --i;
-        if (Data[i] == C)
-          return i;
+      size_t I = std::min(From, Length);
+      while (I) {
+        --I;
+        if (Data[I] == C)
+          return I;
       }
       return npos;
     }
@@ -449,8 +448,8 @@ namespace llvm {
     /// Return the number of occurrences of \p C in the string.
     [[nodiscard]] size_t count(char C) const {
       size_t Count = 0;
-      for (size_t i = 0, e = Length; i != e; ++i)
-        if (Data[i] == C)
+      for (size_t I = 0; I != Length; ++I)
+        if (Data[I] == C)
           ++Count;
       return Count;
     }
@@ -624,7 +623,7 @@ namespace llvm {
       if (!starts_with(Prefix))
         return false;
 
-      *this = drop_front(Prefix.size());
+      *this = substr(Prefix.size());
       return true;
     }
 
@@ -634,7 +633,7 @@ namespace llvm {
       if (!startswith_insensitive(Prefix))
         return false;
 
-      *this = drop_front(Prefix.size());
+      *this = substr(Prefix.size());
       return true;
     }
 
@@ -644,7 +643,7 @@ namespace llvm {
       if (!ends_with(Suffix))
         return false;
 
-      *this = drop_back(Suffix.size());
+      *this = substr(0, size() - Suffix.size());
       return true;
     }
 
@@ -654,7 +653,7 @@ namespace llvm {
       if (!endswith_insensitive(Suffix))
         return false;
 
-      *this = drop_back(Suffix.size());
+      *this = substr(0, size() - Suffix.size());
       return true;
     }
 
@@ -671,7 +670,7 @@ namespace llvm {
     /// be returned.
     [[nodiscard]] StringRef slice(size_t Start, size_t End) const {
       Start = std::min(Start, Length);
-      End = std::min(std::max(Start, End), Length);
+      End = std::clamp(End, Start, Length);
       return StringRef(Data + Start, End - Start);
     }
 


        


More information about the llvm-commits mailing list