java - What substring search algorithm is used by different JREs? -


java.lang.string javadoc says nothing default indexof(string) substring search algorithm. question - substring algorithms used different jres?

there's src.zip in jdk shows implementation:

/**  * code shared string , stringbuffer searches.  * source character array being searched, , target  * string being searched for.  *  * @param   source       characters being searched.  * @param   sourceoffset offset of source string.  * @param   sourcecount  count of source string.  * @param   target       characters being searched for.  * @param   targetoffset offset of target string.  * @param   targetcount  count of target string.  * @param   fromindex    index begin searching from.  */ static int indexof(char[] source, int sourceoffset, int sourcecount,                    char[] target, int targetoffset, int targetcount,                    int fromindex) { if (fromindex >= sourcecount) {         return (targetcount == 0 ? sourcecount : -1); }     if (fromindex < 0) {         fromindex = 0;     } if (targetcount == 0) {     return fromindex; }      char first  = target[targetoffset];     int max = sourceoffset + (sourcecount - targetcount);      (int = sourceoffset + fromindex; <= max; i++) {         /* first character. */         if (source[i] != first) {             while (++i <= max && source[i] != first);         }          /* found first character, @ rest of v2 */         if (i <= max) {             int j = + 1;             int end = j + targetcount - 1;             (int k = targetoffset + 1; j < end && source[j] ==                      target[k]; j++, k++);              if (j == end) {                 /* found whole string. */                 return - sourceoffset;             }         }     }     return -1; } 

Comments