< prev index next >
src/java.base/share/classes/java/lang/AbstractStringBuilder.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2003, 2016, 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
--- 1,7 ----
/*
! * 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,43 ****
--- 34,44 ----
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,314 ****
--- 306,317 ----
* @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,572 ****
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');
}
this.count = count;
return this;
}
--- 561,571 ----
val[count++] = 'n';
val[count++] = 'u';
val[count++] = 'l';
val[count++] = 'l';
} else {
! count = StringUTF16.putCharsAt(val, count, 'n', 'u', 'l', 'l');
}
this.count = count;
return this;
}
*** 693,714 ****
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');
}
}
this.count = count;
return this;
}
--- 692,704 ----
val[count++] = 's';
val[count++] = 'e';
}
} else {
if (b) {
! 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,772 ****
*
* @param i an {@code int}.
* @return a reference to this object.
*/
public AbstractStringBuilder append(int i) {
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);
}
! count = spaceNeeded;
return this;
}
/**
* Appends the string representation of the {@code long}
--- 743,761 ----
*
* @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 {
! StringUTF16.getChars(i, count, spaceNeeded, value);
}
! this.count = spaceNeeded;
return this;
}
/**
* Appends the string representation of the {@code long}
*** 779,798 ****
*
* @param l a {@code long}.
* @return a reference to this object.
*/
public AbstractStringBuilder append(long l) {
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);
}
! count = spaceNeeded;
return this;
}
/**
* Appends the string representation of the {@code float}
--- 768,786 ----
*
* @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 {
! StringUTF16.getChars(l, count, spaceNeeded, value);
}
! this.count = spaceNeeded;
return this;
}
/**
* Appends the string representation of the {@code float}
*** 841,858 ****
* @throws StringIndexOutOfBoundsException if {@code start}
* is negative, greater than {@code length()}, or
* greater than {@code end}.
*/
public AbstractStringBuilder delete(int start, int end) {
if (end > count) {
end = count;
}
checkRangeSIOOBE(start, end, count);
int len = end - start;
if (len > 0) {
shift(end, -len);
! count -= len;
}
return this;
}
/**
--- 829,847 ----
* @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);
! this.count = count - len;
}
return this;
}
/**
*** 923,941 ****
* @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) {
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;
putStringAt(start, str);
return this;
}
/**
--- 912,931 ----
* @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);
! this.count = newCount;
putStringAt(start, str);
return this;
}
/**
*** 1498,1541 ****
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);
! }
}
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
--- 1488,1502 ----
byte cj = val[j];
val[j] = val[k];
val[k] = cj;
}
} else {
! StringUTF16.reverse(val, count);
}
return this;
}
/**
* 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,1707 ****
}
str.getBytes(value, index, coder);
}
private final void appendChars(char[] s, int off, int end) {
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;
inflate();
StringUTF16.putCharsSB(this.value, j, s, i, end);
! count += end - i;
return;
}
}
} else {
StringUTF16.putCharsSB(this.value, count, s, off, end);
}
! count += end - off;
}
private final void appendChars(CharSequence s, int off, int end) {
if (isLatin1()) {
byte[] val = this.value;
--- 1641,1669 ----
}
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 {
! this.count = count = j;
inflate();
StringUTF16.putCharsSB(this.value, j, s, i, end);
! this.count = count + end - i;
return;
}
}
} else {
StringUTF16.putCharsSB(this.value, count, s, off, end);
}
! this.count = count + end - off;
}
private final void appendChars(CharSequence s, int off, int end) {
if (isLatin1()) {
byte[] val = this.value;
*** 1736,1741 ****
--- 1698,1704 ----
if (start < 0 || start > end || end > len) {
throw new StringIndexOutOfBoundsException(
"start " + start + ", end " + end + ", length " + len);
}
}
+
}
< prev index next >