You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/05/18 17:50:39 UTC

[GitHub] [commons-lang] XenoAmess commented on a change in pull request #560: [LANG-1572] green implementation CharSequenceUtils.indexOf

XenoAmess commented on a change in pull request #560:
URL: https://github.com/apache/commons-lang/pull/560#discussion_r634620022



##########
File path: src/main/java/org/apache/commons/lang3/CharSequenceUtils.java
##########
@@ -133,24 +133,87 @@ static int indexOf(final CharSequence cs, final int searchChar, int start) {
      * @param start the start index
      * @return the index where the search sequence was found
      */
-    static int indexOf(final CharSequence cs, final CharSequence searchChar, final int start) {
-        if (cs instanceof String) {
-            return ((String) cs).indexOf(searchChar.toString(), start);
-        } else if (cs instanceof StringBuilder) {
-            return ((StringBuilder) cs).indexOf(searchChar.toString(), start);
-        } else if (cs instanceof StringBuffer) {
-            return ((StringBuffer) cs).indexOf(searchChar.toString(), start);
-        }
-        return cs.toString().indexOf(searchChar.toString(), start);
-//        if (cs instanceof String && searchChar instanceof String) {
-//            // TODO: Do we assume searchChar is usually relatively small;
-//            //       If so then calling toString() on it is better than reverting to
-//            //       the green implementation in the else block
-//            return ((String) cs).indexOf((String) searchChar, start);
-//        } else {
-//            // TODO: Implement rather than convert to String
-//            return cs.toString().indexOf(searchChar.toString(), start);
-//        }
+    static int indexOf(final CharSequence cs, final CharSequence searchChar, int start) {
+        // if searchChar is a String, and cs is some specific kind of AbstractString,
+        //   which both have a indexOf function and widely used by normal java codes,
+        // then we just invoke their indexOf function.
+        if (searchChar instanceof String) {
+            if (cs instanceof String) {
+                return ((String) cs).indexOf((String) searchChar, start);
+            } else if (cs instanceof StringBuilder) {
+                return ((StringBuilder) cs).indexOf((String) searchChar, start);
+            } else if (cs instanceof StringBuffer) {
+                return ((StringBuffer) cs).indexOf((String) searchChar, start);
+            }
+        }
+
+        int len1 = cs.length();
+        int len2 = searchChar.length();
+
+        // successful-result >= 0
+        if (start < 0) {
+            start = 0;
+        }
+
+
+        // successful-result <= len1
+        if (start > len1) {
+            start = len1;
+        }
+
+        // if len2 == 0 return directly.
+        if (len2 == 0) {
+            return start;
+        }
+
+        // limit means the largest possible value of successful-result
+        final int limit = len1 - len2;
+
+        // if start > limit, then have no enough length for a search,
+        //   thus cannot find a index, thus fail.
+        // if len2 < 0, then it is illegal for this function.
+        // if limit < 0, then it means len2 > len1, thus fail.
+        if (start > limit || len2 < 0 || limit < 0) {
+            return -1;
+        }
+
+        // notice that when we enter here, we make sure:
+        // start in [0, limit]
+        // limit in [0, len1-1]
+        // len2 in [1, len1]
+
+        // if len2 is small enough, and cs is some specific kind of AbstractString,
+        //   which both have a indexOf function and widely used by normal java codes,
+        // then we just invoke their indexOf function.
+        if (len2 <= TO_STRING_LIMIT) {
+            if (cs instanceof String) {
+                return ((String) cs).indexOf(searchChar.toString(), start);
+            } else if (cs instanceof StringBuilder) {
+                return ((StringBuilder) cs).indexOf(searchChar.toString(), start);
+            } else if (cs instanceof StringBuffer) {
+                return ((StringBuffer) cs).indexOf(searchChar.toString(), start);
+            }
+        }
+
+        char char0 = searchChar.charAt(0);
+
+        int i = start;
+        while (true) {

Review comment:
       @aherbert 
   I do think it will run faster the way I wrote it...
   However you can prove me wrong, means your version be faster, or equal speed in jmh, with most cases, then I'm glad to refine it.
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org