8254082: AbstractStringBuilder.insert(int dstOffset, CharSequence s, int start, int end) is missing fast-path for String

Reviewed-by: redestad
This commit is contained in:
Sergey Tsypanov 2020-11-30 17:43:19 +00:00 committed by Claes Redestad
parent 4c86e46d75
commit 6eb25d7cb4
2 changed files with 37 additions and 13 deletions

View File

@ -157,8 +157,8 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
return 0;
}
byte val1[] = value;
byte val2[] = another.value;
byte[] val1 = value;
byte[] val2 = another.value;
int count1 = this.count;
int count2 = another.count;
@ -734,7 +734,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* if {@code offset < 0} or {@code len < 0}
* or {@code offset+len > str.length}
*/
public AbstractStringBuilder append(char str[], int offset, int len) {
public AbstractStringBuilder append(char[] str, int offset, int len) {
int end = offset + len;
checkRange(offset, end, str.length);
ensureCapacityInternal(count + len);
@ -1238,9 +1238,6 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (s == null) {
s = "null";
}
if (s instanceof String) {
return this.insert(dstOffset, (String)s);
}
return this.insert(dstOffset, s, 0, s.length());
}
@ -1300,7 +1297,11 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
ensureCapacityInternal(count + len);
shift(dstOffset, len);
count += len;
putCharsAt(dstOffset, s, start, end);
if (s instanceof String) {
putStringAt(dstOffset, (String) s, start, end);
} else {
putCharsAt(dstOffset, s, start, end);
}
return this;
}
@ -1558,9 +1559,8 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
public AbstractStringBuilder reverse() {
byte[] val = this.value;
int count = this.count;
int coder = this.coder;
int n = count - 1;
if (COMPACT_STRINGS && coder == LATIN1) {
if (isLatin1()) {
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
byte cj = val[j];
@ -1648,7 +1648,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* @param dstBegin the char index, not offset of byte[]
* @param coder the coder of dst[]
*/
void getBytes(byte dst[], int dstBegin, byte coder) {
void getBytes(byte[] dst, int dstBegin, byte coder) {
if (this.coder == coder) {
System.arraycopy(value, 0, dst, dstBegin << coder, count << coder);
} else { // this.coder == LATIN && coder == UTF16
@ -1713,11 +1713,15 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
}
}
private final void putStringAt(int index, String str) {
private void putStringAt(int index, String str, int off, int end) {
if (getCoder() != str.coder()) {
inflate();
}
str.getBytes(value, index, coder);
str.getBytes(value, off, index, coder, end);
}
private void putStringAt(int index, String str) {
putStringAt(index, str, 0, str.length());
}
private final void appendChars(char[] s, int off, int end) {

View File

@ -3599,7 +3599,7 @@ public final class String
* @param dstBegin the char index, not offset of byte[]
* @param coder the coder of dst[]
*/
void getBytes(byte dst[], int dstBegin, byte coder) {
void getBytes(byte[] dst, int dstBegin, byte coder) {
if (coder() == coder) {
System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
} else { // this.coder == LATIN && coder == UTF16
@ -3607,6 +3607,26 @@ public final class String
}
}
/**
* Copy character bytes from this string into dst starting at dstBegin.
* This method doesn't perform any range checking.
*
* Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
* coders are different, and dst is big enough (range check)
*
* @param srcPos the char index, not offset of byte[]
* @param dstBegin the char index to start from
* @param coder the coder of dst[]
* @param length the amount of copied chars
*/
void getBytes(byte[] dst, int srcPos, int dstBegin, byte coder, int length) {
if (coder() == coder) {
System.arraycopy(value, srcPos, dst, dstBegin << coder, length << coder());
} else { // this.coder == LATIN && coder == UTF16
StringLatin1.inflate(value, srcPos, dst, dstBegin, length);
}
}
/*
* Package private constructor. Trailing Void argument is there for
* disambiguating it against other (public) constructors.