[PATCH] D70585: [Support] Fix behavior of StringRef::count with overlapping occurrences, add tests

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 24 16:31:58 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rG9f6b13e5cce9: [Support] Fix behavior of StringRef::count with overlapping occurrences, add… (authored by jdoerfert).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D70585

Files:
  llvm/lib/Support/StringRef.cpp
  llvm/unittests/ADT/StringRefTest.cpp


Index: llvm/unittests/ADT/StringRefTest.cpp
===================================================================
--- llvm/unittests/ADT/StringRefTest.cpp
+++ llvm/unittests/ADT/StringRefTest.cpp
@@ -509,6 +509,13 @@
   EXPECT_EQ(1U, Str.count("hello"));
   EXPECT_EQ(1U, Str.count("ello"));
   EXPECT_EQ(0U, Str.count("zz"));
+
+  StringRef OverlappingAbba("abbabba");
+  EXPECT_EQ(1U, OverlappingAbba.count("abba"));
+  StringRef NonOverlappingAbba("abbaabba");
+  EXPECT_EQ(2U, NonOverlappingAbba.count("abba"));
+  StringRef ComplexAbba("abbabbaxyzabbaxyz");
+  EXPECT_EQ(2U, ComplexAbba.count("abba"));
 }
 
 TEST(StringRefTest, EditDistance) {
Index: llvm/lib/Support/StringRef.cpp
===================================================================
--- llvm/lib/Support/StringRef.cpp
+++ llvm/lib/Support/StringRef.cpp
@@ -374,9 +374,14 @@
   size_t N = Str.size();
   if (N > Length)
     return 0;
-  for (size_t i = 0, e = Length - N + 1; i != e; ++i)
-    if (substr(i, N).equals(Str))
+  for (size_t i = 0, e = Length - N + 1; i < e;) {
+    if (substr(i, N).equals(Str)) {
       ++Count;
+      i += N;
+    }
+    else
+      ++i;
+  }
   return Count;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70585.235237.patch
Type: text/x-patch
Size: 1171 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191225/264f573f/attachment.bin>


More information about the llvm-commits mailing list