mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8268698: Use Objects.check{Index,FromToIndex,FromIndexSize} for java.base
Reviewed-by: mchung, rriggs
This commit is contained in:
parent
a4e5f08fef
commit
afe957cd97
40 changed files with 186 additions and 284 deletions
|
@ -32,6 +32,7 @@ import java.util.Spliterator;
|
|||
import java.util.stream.IntStream;
|
||||
import java.util.stream.StreamSupport;
|
||||
import jdk.internal.util.ArraysSupport;
|
||||
import jdk.internal.util.Preconditions;
|
||||
|
||||
import static java.lang.String.COMPACT_STRINGS;
|
||||
import static java.lang.String.UTF16;
|
||||
|
@ -409,9 +410,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
*/
|
||||
public int codePointBefore(int index) {
|
||||
int i = index - 1;
|
||||
if (i < 0 || i >= count) {
|
||||
throw new StringIndexOutOfBoundsException(index);
|
||||
}
|
||||
checkIndex(i, count);
|
||||
if (isLatin1()) {
|
||||
return value[i] & 0xff;
|
||||
}
|
||||
|
@ -505,9 +504,9 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
*/
|
||||
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
|
||||
{
|
||||
checkRangeSIOOBE(srcBegin, srcEnd, count); // compatible to old version
|
||||
Preconditions.checkFromToIndex(srcBegin, srcEnd, count, Preconditions.SIOOBE_FORMATTER); // compatible to old version
|
||||
int n = srcEnd - srcBegin;
|
||||
checkRange(dstBegin, dstBegin + n, dst.length);
|
||||
Preconditions.checkFromToIndex(dstBegin, dstBegin + n, dst.length, Preconditions.IOOBE_FORMATTER);
|
||||
if (isLatin1()) {
|
||||
StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
|
||||
} else {
|
||||
|
@ -677,7 +676,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
if (s == null) {
|
||||
s = "null";
|
||||
}
|
||||
checkRange(start, end, s.length());
|
||||
Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER);
|
||||
int len = end - start;
|
||||
ensureCapacityInternal(count + len);
|
||||
if (s instanceof String) {
|
||||
|
@ -736,7 +735,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
*/
|
||||
public AbstractStringBuilder append(char[] str, int offset, int len) {
|
||||
int end = offset + len;
|
||||
checkRange(offset, end, str.length);
|
||||
Preconditions.checkFromToIndex(offset, end, str.length, Preconditions.IOOBE_FORMATTER);
|
||||
ensureCapacityInternal(count + len);
|
||||
appendChars(str, offset, end);
|
||||
return this;
|
||||
|
@ -914,7 +913,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
if (end > count) {
|
||||
end = count;
|
||||
}
|
||||
checkRangeSIOOBE(start, end, count);
|
||||
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
|
||||
int len = end - start;
|
||||
if (len > 0) {
|
||||
shift(end, -len);
|
||||
|
@ -997,7 +996,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
if (end > count) {
|
||||
end = count;
|
||||
}
|
||||
checkRangeSIOOBE(start, end, count);
|
||||
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
|
||||
int len = str.length();
|
||||
int newCount = count + len - (end - start);
|
||||
ensureCapacityInternal(newCount);
|
||||
|
@ -1067,7 +1066,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
* greater than {@code end}.
|
||||
*/
|
||||
public String substring(int start, int end) {
|
||||
checkRangeSIOOBE(start, end, count);
|
||||
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
|
||||
if (isLatin1()) {
|
||||
return StringLatin1.newString(value, start, end - start);
|
||||
}
|
||||
|
@ -1104,7 +1103,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
int len)
|
||||
{
|
||||
checkOffset(index, count);
|
||||
checkRangeSIOOBE(offset, offset + len, str.length);
|
||||
Preconditions.checkFromToIndex(offset, offset + len, str.length, Preconditions.SIOOBE_FORMATTER);
|
||||
ensureCapacityInternal(count + len);
|
||||
shift(index, len);
|
||||
count += len;
|
||||
|
@ -1292,7 +1291,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
s = "null";
|
||||
}
|
||||
checkOffset(dstOffset, count);
|
||||
checkRange(start, end, s.length());
|
||||
Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER);
|
||||
int len = end - start;
|
||||
ensureCapacityInternal(count + len);
|
||||
shift(dstOffset, len);
|
||||
|
@ -1795,20 +1794,4 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
|
|||
}
|
||||
count += end - off;
|
||||
}
|
||||
|
||||
/* IndexOutOfBoundsException, if out of bounds */
|
||||
private static void checkRange(int start, int end, int len) {
|
||||
if (start < 0 || start > end || end > len) {
|
||||
throw new IndexOutOfBoundsException(
|
||||
"start " + start + ", end " + end + ", length " + len);
|
||||
}
|
||||
}
|
||||
|
||||
/* StringIndexOutOfBoundsException, if out of bounds */
|
||||
private static void checkRangeSIOOBE(int start, int end, int len) {
|
||||
if (start < 0 || start > end || end > len) {
|
||||
throw new StringIndexOutOfBoundsException(
|
||||
"start " + start + ", end " + end + ", length " + len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue