This commit is contained in:
Alejandro Murillo 2015-12-03 22:30:21 -08:00
commit 8d7fe09e10
5 changed files with 16 additions and 35 deletions

View File

@ -177,7 +177,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
return; return;
} }
byte[] buf = StringUTF16.newBytesFor(value.length); byte[] buf = StringUTF16.newBytesFor(value.length);
StringLatin1.inflateSB(value, buf, 0, count); StringLatin1.inflate(value, 0, buf, 0, count);
this.value = buf; this.value = buf;
this.coder = UTF16; this.coder = UTF16;
} }
@ -414,9 +414,9 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
int n = srcEnd - srcBegin; int n = srcEnd - srcBegin;
checkRange(dstBegin, dstBegin + n, dst.length); checkRange(dstBegin, dstBegin + n, dst.length);
if (isLatin1()) { if (isLatin1()) {
StringLatin1.getCharsSB(value, srcBegin, srcEnd, dst, dstBegin); StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
} else { } else {
StringUTF16.getCharsSB(value, srcBegin, srcEnd, dst, dstBegin); StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin);
} }
} }
@ -980,7 +980,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (isLatin1()) { if (isLatin1()) {
return StringLatin1.newString(value, start, end - start); return StringLatin1.newString(value, start, end - start);
} }
return StringUTF16.newStringSB(value, start, end - start); return StringUTF16.newString(value, start, end - start);
} }
private void shift(int offset, int n) { private void shift(int offset, int n) {
@ -1576,7 +1576,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (this.coder == coder) { if (this.coder == coder) {
System.arraycopy(value, 0, dst, dstBegin << coder, count << coder); System.arraycopy(value, 0, dst, dstBegin << coder, count << coder);
} else { // this.coder == LATIN && coder == UTF16 } else { // this.coder == LATIN && coder == UTF16
StringLatin1.inflateSB(value, dst, dstBegin, count); StringLatin1.inflate(value, 0, dst, dstBegin, count);
} }
} }
@ -1641,10 +1641,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (getCoder() != str.coder()) { if (getCoder() != str.coder()) {
inflate(); inflate();
} }
byte[] val = this.value; str.getBytes(value, index, coder);
byte coder = this.coder;
checkOffset(index + str.length(), val.length >> coder);
str.getBytes(val, index, coder);
} }
private final void appendChars(char[] s, int off, int end) { private final void appendChars(char[] s, int off, int end) {

View File

@ -1720,7 +1720,6 @@ public final class String
*/ */
static int indexOf(byte[] src, byte srcCoder, int srcCount, static int indexOf(byte[] src, byte srcCoder, int srcCount,
String tgtStr, int fromIndex) { String tgtStr, int fromIndex) {
byte[] tgt = tgtStr.value; byte[] tgt = tgtStr.value;
byte tgtCoder = tgtStr.coder(); byte tgtCoder = tgtStr.coder();
int tgtCount = tgtStr.length(); int tgtCount = tgtStr.length();
@ -3103,7 +3102,7 @@ public final class String
* If {@code offset} is negative, {@code count} is negative, * If {@code offset} is negative, {@code count} is negative,
* or {@code offset} is greater than {@code length - count} * or {@code offset} is greater than {@code length - count}
*/ */
private static void checkBoundsOffCount(int offset, int count, int length) { static void checkBoundsOffCount(int offset, int count, int length) {
if (offset < 0 || count < 0 || offset > length - count) { if (offset < 0 || count < 0 || offset > length - count) {
throw new StringIndexOutOfBoundsException( throw new StringIndexOutOfBoundsException(
"offset " + offset + ", count " + count + ", length " + length); "offset " + offset + ", count " + count + ", length " + length);

View File

@ -413,7 +413,7 @@ public final class StringBuilder
public String toString() { public String toString() {
// Create a copy, don't share the array // Create a copy, don't share the array
return isLatin1() ? StringLatin1.newString(value, 0, count) return isLatin1() ? StringLatin1.newString(value, 0, count)
: StringUTF16.newStringSB(value, 0, count); : StringUTF16.newString(value, 0, count);
} }
/** /**

View File

@ -36,6 +36,7 @@ import jdk.internal.HotSpotIntrinsicCandidate;
import static java.lang.String.LATIN1; import static java.lang.String.LATIN1;
import static java.lang.String.UTF16; import static java.lang.String.UTF16;
import static java.lang.String.checkOffset; import static java.lang.String.checkOffset;
import static java.lang.String.checkBoundsOffCount;
final class StringLatin1 { final class StringLatin1 {
@ -523,6 +524,8 @@ final class StringLatin1 {
// inflatedCopy byte[] -> byte[] // inflatedCopy byte[] -> byte[]
@HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate
public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) { public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
// We need a range check here because 'putChar' has no checks
checkBoundsOffCount(dstOff, len, dst.length);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
StringUTF16.putChar(dst, dstOff++, src[srcOff++] & 0xff); StringUTF16.putChar(dst, dstOff++, src[srcOff++] & 0xff);
} }
@ -584,17 +587,4 @@ final class StringLatin1 {
return cs; return cs;
} }
} }
////////////////////////////////////////////////////////////////
public static void getCharsSB(byte[] val, int srcBegin, int srcEnd, char dst[], int dstBegin) {
checkOffset(srcEnd, val.length);
getChars(val, srcBegin, srcEnd, dst, dstBegin);
}
public static void inflateSB(byte[] val, byte[] dst, int dstOff, int count) {
checkOffset(count, val.length);
checkOffset(dstOff + count, dst.length >> 1); // dst is utf16
inflate(val, 0, dst, dstOff, count);
}
} }

View File

@ -35,6 +35,7 @@ import static java.lang.String.UTF16;
import static java.lang.String.LATIN1; import static java.lang.String.LATIN1;
import static java.lang.String.checkIndex; import static java.lang.String.checkIndex;
import static java.lang.String.checkOffset; import static java.lang.String.checkOffset;
import static java.lang.String.checkBoundsOffCount;
final class StringUTF16 { final class StringUTF16 {
@ -156,6 +157,8 @@ final class StringUTF16 {
// compressedCopy byte[] -> byte[] // compressedCopy byte[] -> byte[]
@HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate
public static int compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len) { public static int compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
// We need a range check here because 'getChar' has no checks
checkBoundsOffCount(srcOff, len, src.length);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
int c = getChar(src, srcOff++); int c = getChar(src, srcOff++);
if (c >>> 8 != 0) { if (c >>> 8 != 0) {
@ -200,6 +203,8 @@ final class StringUTF16 {
@HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate
public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) { public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) {
// We need a range check here because 'getChar' has no checks
checkBoundsOffCount(srcBegin, srcEnd - srcBegin, value.length);
for (int i = srcBegin; i < srcEnd; i++) { for (int i = srcBegin; i < srcEnd; i++) {
dst[dstBegin++] = getChar(value, i); dst[dstBegin++] = getChar(value, i);
} }
@ -909,11 +914,6 @@ final class StringUTF16 {
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
public static void getCharsSB(byte[] val, int srcBegin, int srcEnd, char dst[], int dstBegin) {
checkOffset(srcEnd, val.length >> 1);
getChars(val, srcBegin, srcEnd, dst, dstBegin);
}
public static void putCharSB(byte[] val, int index, int c) { public static void putCharSB(byte[] val, int index, int c) {
checkIndex(index, val.length >> 1); checkIndex(index, val.length >> 1);
putChar(val, index, c); putChar(val, index, c);
@ -946,11 +946,6 @@ final class StringUTF16 {
return codePointCount(val, beginIndex, endIndex); return codePointCount(val, beginIndex, endIndex);
} }
public static String newStringSB(byte[] val, int index, int len) {
checkOffset(index + len, val.length >> 1);
return newString(val, index, len);
}
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
private static native boolean isBigEndian(); private static native boolean isBigEndian();