[PATCH] D133658: [Support] Optimize StringRef::count,find.

Tatsuyuki Ishi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 11 00:57:50 PDT 2022


ishitatsuyuki updated this revision to Diff 459331.
ishitatsuyuki added a comment.

Split revision; this becomes the first revision (StringRef::count).


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
@@ -370,16 +370,11 @@
 /// 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)
-    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.459331.patch
Type: text/x-patch
Size: 616 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220911/79cde2ac/attachment.bin>


More information about the llvm-commits mailing list