diff --git a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
index <HASH>..<HASH> 100644
--- a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -407,12 +407,10 @@ public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
         int answer = 0;
         for (int idx = 0; true; idx++) {
             idx = self.indexOf(text, idx);
-            if (idx >= 0) {
-                ++answer;
-                if (idx == self.length()) break;
-            } else {
-                break;
-            }
+            // break once idx goes to -1 or for case of empty string once
+            // we get to the end to avoid JDK library bug (see GROOVY-5858)
+            if (idx < answer) break;
+            ++answer;
         }
         return answer;
     }
GROOVY-<I>: 'string'.count('') fails (additional optimization)