Skip to content

Commit

Permalink
8254082: AbstractStringBuilder.insert(int dstOffset, CharSequence s, …
Browse files Browse the repository at this point in the history
…int start, int end) is missing fast-path for String

Reviewed-by: redestad
  • Loading branch information
stsypanov authored and cl4es committed Nov 30, 2020
1 parent 4c86e46 commit 6eb25d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/java.base/share/classes/java/lang/AbstractStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ int compareTo(AbstractStringBuilder another) {
return 0;
}

byte val1[] = value;
byte val2[] = another.value;
byte[] val1 = value;
byte[] val2 = another.value;
int count1 = this.count;
int count2 = another.count;

Expand Down Expand Up @@ -734,7 +734,7 @@ public AbstractStringBuilder append(char[] str) {
* 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);
Expand Down Expand Up @@ -1238,9 +1238,6 @@ public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
if (s == null) {
s = "null";
}
if (s instanceof String) {
return this.insert(dstOffset, (String)s);
}
return this.insert(dstOffset, s, 0, s.length());
}

Expand Down Expand Up @@ -1300,7 +1297,11 @@ public AbstractStringBuilder insert(int dstOffset, CharSequence s,
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;
}

Expand Down Expand Up @@ -1558,9 +1559,8 @@ public int lastIndexOf(String str, int fromIndex) {
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];
Expand Down Expand Up @@ -1648,7 +1648,7 @@ final byte[] getValue() {
* @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
Expand Down Expand Up @@ -1713,11 +1713,15 @@ private final void putCharsAt(int index, CharSequence s, int off, int end) {
}
}

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) {
Expand Down
22 changes: 21 additions & 1 deletion src/java.base/share/classes/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -3599,14 +3599,34 @@ public String repeat(int count) {
* @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
StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
}
}

/**
* 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.
Expand Down

0 comments on commit 6eb25d7

Please sign in to comment.