今天瀏覽了一下java裡的String類,發現一個靜態方法有點意思,就是我們常用的indexOf(String str)的底層實現,先看下程式碼呼叫鏈。
public int indexOf(String str) {
return indexOf(str, 0);
}
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
static int indexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
return indexOf(source, sourceOffset, sourceCount,
target.value, 0, target.value.length,
fromIndex);
}
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to 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);
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;
}
底層的字串匹配的邏輯比較簡單,就是普通的匹配模式:
- 查詢首字元,匹配target的第一個字元在source內的位置,若查詢到max位置還找到,則返回-1;
- 若在source匹配到了target的第一個字元,那麼在依次比較srouce和target後面的字元,一直到target的末尾;
- 如果target後面的字元與source都已經匹配,則返回在source上匹配到的第一個字元的相對下標,否則返回-1。
但是仔細讀程式碼會發現一個問題,就是這裡
int max = sourceOffset + (sourceCount - targetCount);
max的計算方式,max的作用是計算出最大的首字元匹配次數,取值範圍應該是”max <= sourceCount”。
所以target字串的長度是可以不用匹配的,故“sourceCount – targetCount”是沒問題的。
關鍵的地方是這裡加上了sourceOffset,sourceOffset是source字串的起始匹配偏移量,即從source的哪個字元開始匹配。
所以,根據程式碼裡的max計算方式,最終計算出來的max值是會有可能大於sourceCount。
看下測試程式碼:
package string;
/**
* string test
*/
public class StringTest {
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;
}
public static void main(String[] args) {
String source = "abcdefghigklmn";
String target = "n";
int sourceOffset = 5;
int targetOffset = 0;
int index = indexOf(source.toCharArray(), sourceOffset, source.length(), target.toCharArray(), targetOffset, target.length(), 0);
System.out.println(index);
}
}
如果target在source內可以匹配到返回正確結果8(結果8是相對於sourceOffset的結果,如果轉換成source內的位置則是13)。
但是如果target在source內匹配不到,則會丟擲java.lang.ArrayIndexOutOfBoundsException異常,如下:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14
at string.StringTest.indexOf(StringTest.java:27)
at string.StringTest.main(StringTest.java:52)
可見報出越界的下標是14,這就是由於max = sourceOffset + (sourceCount – targetCount)引起,計算出的max值為:17。
所以,個人認為max計算這裡是個潛在的BUG,應該改為 int max = sourceCount – targetCount;
不過這個方法是一個非public方法,只在String內部呼叫,同時也跟蹤了所有對該方法的呼叫鏈,都是傳入的預設0,在使用時不會出現陣列越界問題。
不知這是開發者故意為之,還是其它我未知用意,歡迎大家交流討論!!!