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);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
例如:源字符串source=abcdeabcde,源字符串偏移量sourceOffset=1,
则源字符串长度sourceCount =10
子串target=eabd,子串偏移量为targetOffset=1,
子串长度targetCount=4
开始查找位置为fromIndex=2
这个方法是实现下面的功能吗?
我们需要从源字符串source 从索引位置fromIndex=2,在偏移sourceOffset=1
查找(子串target偏移量为targetOffset=1后的字符串)的位置