提交 e6487ad2 编写于 作者: M mduigou

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

Summary: It is not necessary to extract the value array with toCharArray. The value array can now be used directly.
Reviewed-by: alanb
上级 45330769
...@@ -177,11 +177,10 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { ...@@ -177,11 +177,10 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
ensureCapacityInternal(newLength); ensureCapacityInternal(newLength);
if (count < newLength) { if (count < newLength) {
for (; count < newLength; count++) Arrays.fill(value, count, newLength, '\0');
value[count] = '\0';
} else {
count = newLength;
} }
count = newLength;
} }
/** /**
...@@ -1308,8 +1307,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { ...@@ -1308,8 +1307,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* {@code null}. * {@code null}.
*/ */
public int indexOf(String str, int fromIndex) { public int indexOf(String str, int fromIndex) {
return String.indexOf(value, 0, count, return String.indexOf(value, 0, count, str, fromIndex);
str.toCharArray(), 0, str.length(), fromIndex);
} }
/** /**
...@@ -1352,8 +1350,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { ...@@ -1352,8 +1350,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* {@code null}. * {@code null}.
*/ */
public int lastIndexOf(String str, int fromIndex) { public int lastIndexOf(String str, int fromIndex) {
return String.lastIndexOf(value, 0, count, return String.lastIndexOf(value, 0, count, str, fromIndex);
str.toCharArray(), 0, str.length(), fromIndex);
} }
/** /**
......
...@@ -1705,6 +1705,24 @@ public final class String ...@@ -1705,6 +1705,24 @@ public final class String
str.value, 0, str.value.length, fromIndex); 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 * Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target * source is the character array being searched, and the target
...@@ -1796,6 +1814,24 @@ public final class String ...@@ -1796,6 +1814,24 @@ public final class String
str.value, 0, str.value.length, fromIndex); 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 * Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target * source is the character array being searched, and the target
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册