[PATCH] D144787: [NFC][ADT] Tweaked some functions in StringRef

Shao-Ce SUN via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 25 08:33:13 PST 2023


sunshaoce created this revision.
sunshaoce added a reviewer: craig.topper.
Herald added a project: All.
sunshaoce requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144787

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


Index: llvm/include/llvm/ADT/StringRef.h
===================================================================
--- llvm/include/llvm/ADT/StringRef.h
+++ llvm/include/llvm/ADT/StringRef.h
@@ -343,12 +343,11 @@
     /// \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 @@
     /// 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 @@
       if (!starts_with(Prefix))
         return false;
 
-      *this = drop_front(Prefix.size());
+      *this = substr(Prefix.size());
       return true;
     }
 
@@ -634,7 +633,7 @@
       if (!startswith_insensitive(Prefix))
         return false;
 
-      *this = drop_front(Prefix.size());
+      *this = substr(Prefix.size());
       return true;
     }
 
@@ -644,7 +643,7 @@
       if (!ends_with(Suffix))
         return false;
 
-      *this = drop_back(Suffix.size());
+      *this = substr(0, size() - Suffix.size());
       return true;
     }
 
@@ -654,7 +653,7 @@
       if (!endswith_insensitive(Suffix))
         return false;
 
-      *this = drop_back(Suffix.size());
+      *this = substr(0, size() - Suffix.size());
       return true;
     }
 
@@ -671,7 +670,7 @@
     /// 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);
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144787.500433.patch
Type: text/x-patch
Size: 2126 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230225/2c6aa3f0/attachment.bin>


More information about the llvm-commits mailing list