6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation

It is not necessary to extract the value array with toCharArray. The value array can now be used directly.

Reviewed-by: alanb
This commit is contained in:
Mike Duigou 2012-11-29 14:09:45 -08:00
parent 1cf41521cc
commit 2a4b35660e
2 changed files with 41 additions and 8 deletions

View File

@ -177,11 +177,10 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
ensureCapacityInternal(newLength);
if (count < newLength) {
for (; count < newLength; count++)
value[count] = '\0';
} else {
count = newLength;
Arrays.fill(value, count, newLength, '\0');
}
count = newLength;
}
/**
@ -1308,8 +1307,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* {@code null}.
*/
public int indexOf(String str, int fromIndex) {
return String.indexOf(value, 0, count,
str.toCharArray(), 0, str.length(), fromIndex);
return String.indexOf(value, 0, count, str, fromIndex);
}
/**
@ -1352,8 +1350,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* {@code null}.
*/
public int lastIndexOf(String str, int fromIndex) {
return String.lastIndexOf(value, 0, count,
str.toCharArray(), 0, str.length(), fromIndex);
return String.lastIndexOf(value, 0, count, str, fromIndex);
}
/**

View File

@ -1705,6 +1705,24 @@ public final class String
str.value, 0, str.value.length, fromIndex);
}
/**
* Code shared by String and AbstractStringBuilder 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 fromIndex the index to begin searching from.
*/
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
@ -1796,6 +1814,24 @@ public final class String
str.value, 0, str.value.length, fromIndex);
}
/**
* Code shared by String and AbstractStringBuilder 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 fromIndex the index to begin searching from.
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
return lastIndexOf(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