< prev index next >
src/java.base/share/classes/java/lang/AbstractStringBuilder.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -34,10 +34,11 @@
import static java.lang.String.COMPACT_STRINGS;
import static java.lang.String.UTF16;
import static java.lang.String.LATIN1;
import static java.lang.String.checkIndex;
import static java.lang.String.checkOffset;
+import static java.lang.String.checkBoundsBeginEnd;
/**
* A mutable sequence of characters.
* <p>
* Implements a modifiable string. At any point in time it contains some
@@ -305,10 +306,12 @@
* @exception IndexOutOfBoundsException if the {@code index}
* argument is negative or not less than the length of this
* sequence.
*/
public int codePointAt(int index) {
+ int count = this.count;
+ byte[] value = this.value;
checkIndex(index, count);
if (isLatin1()) {
return value[index] & 0xff;
}
return StringUTF16.codePointAtSB(value, index, count);
@@ -558,15 +561,11 @@
val[count++] = 'n';
val[count++] = 'u';
val[count++] = 'l';
val[count++] = 'l';
} else {
- checkOffset(count + 4, val.length >> 1);
- StringUTF16.putChar(val, count++, 'n');
- StringUTF16.putChar(val, count++, 'u');
- StringUTF16.putChar(val, count++, 'l');
- StringUTF16.putChar(val, count++, 'l');
+ count = StringUTF16.putCharsAt(val, count, 'n', 'u', 'l', 'l');
}
this.count = count;
return this;
}
@@ -693,22 +692,13 @@
val[count++] = 's';
val[count++] = 'e';
}
} else {
if (b) {
- checkOffset(count + 4, val.length >> 1);
- StringUTF16.putChar(val, count++, 't');
- StringUTF16.putChar(val, count++, 'r');
- StringUTF16.putChar(val, count++, 'u');
- StringUTF16.putChar(val, count++, 'e');
- } else {
- checkOffset(count + 5, val.length >> 1);
- StringUTF16.putChar(val, count++, 'f');
- StringUTF16.putChar(val, count++, 'a');
- StringUTF16.putChar(val, count++, 'l');
- StringUTF16.putChar(val, count++, 's');
- StringUTF16.putChar(val, count++, 'e');
+ count = StringUTF16.putCharsAt(val, count, 't', 'r', 'u', 'e');
+ } else {
+ count = StringUTF16.putCharsAt(val, count, 'f', 'a', 'l', 's', 'e');
}
}
this.count = count;
return this;
}
@@ -753,20 +743,19 @@
*
* @param i an {@code int}.
* @return a reference to this object.
*/
public AbstractStringBuilder append(int i) {
+ int count = this.count;
int spaceNeeded = count + Integer.stringSize(i);
ensureCapacityInternal(spaceNeeded);
if (isLatin1()) {
Integer.getChars(i, spaceNeeded, value);
} else {
- byte[] val = this.value;
- checkOffset(spaceNeeded, val.length >> 1);
- Integer.getCharsUTF16(i, spaceNeeded, val);
+ StringUTF16.getChars(i, count, spaceNeeded, value);
}
- count = spaceNeeded;
+ this.count = spaceNeeded;
return this;
}
/**
* Appends the string representation of the {@code long}
@@ -779,20 +768,19 @@
*
* @param l a {@code long}.
* @return a reference to this object.
*/
public AbstractStringBuilder append(long l) {
+ int count = this.count;
int spaceNeeded = count + Long.stringSize(l);
ensureCapacityInternal(spaceNeeded);
if (isLatin1()) {
Long.getChars(l, spaceNeeded, value);
} else {
- byte[] val = this.value;
- checkOffset(spaceNeeded, val.length >> 1);
- Long.getCharsUTF16(l, spaceNeeded, val);
+ StringUTF16.getChars(l, count, spaceNeeded, value);
}
- count = spaceNeeded;
+ this.count = spaceNeeded;
return this;
}
/**
* Appends the string representation of the {@code float}
@@ -841,18 +829,19 @@
* @throws StringIndexOutOfBoundsException if {@code start}
* is negative, greater than {@code length()}, or
* greater than {@code end}.
*/
public AbstractStringBuilder delete(int start, int end) {
+ int count = this.count;
if (end > count) {
end = count;
}
checkRangeSIOOBE(start, end, count);
int len = end - start;
if (len > 0) {
shift(end, -len);
- count -= len;
+ this.count = count - len;
}
return this;
}
/**
@@ -923,19 +912,20 @@
* @throws StringIndexOutOfBoundsException if {@code start}
* is negative, greater than {@code length()}, or
* greater than {@code end}.
*/
public AbstractStringBuilder replace(int start, int end, String str) {
+ int count = this.count;
if (end > count) {
end = count;
}
checkRangeSIOOBE(start, end, count);
int len = str.length();
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
shift(end, newCount - count);
- count = newCount;
+ this.count = newCount;
putStringAt(start, str);
return this;
}
/**
@@ -1498,44 +1488,15 @@
byte cj = val[j];
val[j] = val[k];
val[k] = cj;
}
} else {
- checkOffset(count, val.length >> 1);
- boolean hasSurrogates = false;
- for (int j = (n-1) >> 1; j >= 0; j--) {
- int k = n - j;
- char cj = StringUTF16.getChar(val, j);
- char ck = StringUTF16.getChar(val, k);
- StringUTF16.putChar(val, j, ck);
- StringUTF16.putChar(val, k, cj);
- if (Character.isSurrogate(cj) ||
- Character.isSurrogate(ck)) {
- hasSurrogates = true;
- }
- }
- if (hasSurrogates) {
- reverseAllValidSurrogatePairs(val, count);
- }
+ StringUTF16.reverse(val, count);
}
return this;
}
- /** Outlined helper method for reverse() */
- private void reverseAllValidSurrogatePairs(byte[] val, int count) {
- for (int i = 0; i < count - 1; i++) {
- char c2 = StringUTF16.getChar(val, i);
- if (Character.isLowSurrogate(c2)) {
- char c1 = StringUTF16.getChar(val, i + 1);
- if (Character.isHighSurrogate(c1)) {
- StringUTF16.putChar(val, i++, c1);
- StringUTF16.putChar(val, i, c2);
- }
- }
- }
- }
-
/**
* Returns a string representing the data in this sequence.
* A new {@code String} object is allocated and initialized to
* contain the character sequence currently represented by this
* object. This {@code String} is then returned. Subsequent
@@ -1680,28 +1641,29 @@
}
str.getBytes(value, index, coder);
}
private final void appendChars(char[] s, int off, int end) {
+ int count = this.count;
if (isLatin1()) {
byte[] val = this.value;
for (int i = off, j = count; i < end; i++) {
char c = s[i];
if (StringLatin1.canEncode(c)) {
val[j++] = (byte)c;
} else {
- count = j;
+ this.count = count = j;
inflate();
StringUTF16.putCharsSB(this.value, j, s, i, end);
- count += end - i;
+ this.count = count + end - i;
return;
}
}
} else {
StringUTF16.putCharsSB(this.value, count, s, off, end);
}
- count += end - off;
+ this.count = count + end - off;
}
private final void appendChars(CharSequence s, int off, int end) {
if (isLatin1()) {
byte[] val = this.value;
@@ -1736,6 +1698,7 @@
if (start < 0 || start > end || end > len) {
throw new StringIndexOutOfBoundsException(
"start " + start + ", end " + end + ", length " + len);
}
}
+
}
< prev index next >