[PATCH] D133658: [Support] Use find() for faster StringRef::count.

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 27 01:22:34 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG826f38534816: [Support] Use find() for faster StringRef::count (NFC) (authored by ishitatsuyuki, committed by nikic).

Changed prior to commit:
  https://reviews.llvm.org/D133658?vs=470804&id=471054#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133658/new/

https://reviews.llvm.org/D133658

Files:
  llvm/lib/Support/StringRef.cpp


Index: llvm/lib/Support/StringRef.cpp
===================================================================
--- llvm/lib/Support/StringRef.cpp
+++ llvm/lib/Support/StringRef.cpp
@@ -382,16 +382,16 @@
 /// the string.
 size_t StringRef::count(StringRef Str) const {
   size_t Count = 0;
+  size_t Pos = 0;
   size_t N = Str.size();
-  if (!N || N > Length)
+  // TODO: For an empty `Str` we return 0 for legacy reasons. Consider changing
+  //       this to `Length + 1` which is more in-line with the function
+  //       description.
+  if (!N)
     return 0;
-  for (size_t i = 0, e = Length - N + 1; i < e;) {
-    if (substr(i, N).equals(Str)) {
-      ++Count;
-      i += N;
-    }
-    else
-      ++i;
+  while ((Pos = find(Str, Pos)) != npos) {
+    ++Count;
+    Pos += N;
   }
   return Count;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133658.471054.patch
Type: text/x-patch
Size: 806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221027/fad7b55f/attachment.bin>


More information about the llvm-commits mailing list