|
32 | 32 | import java.util.stream.IntStream; |
33 | 33 | import java.util.stream.StreamSupport; |
34 | 34 | import jdk.internal.util.ArraysSupport; |
| 35 | +import jdk.internal.util.Preconditions; |
35 | 36 |
|
36 | 37 | import static java.lang.String.COMPACT_STRINGS; |
37 | 38 | import static java.lang.String.UTF16; |
@@ -409,9 +410,7 @@ public int codePointAt(int index) { |
409 | 410 | */ |
410 | 411 | public int codePointBefore(int index) { |
411 | 412 | int i = index - 1; |
412 | | - if (i < 0 || i >= count) { |
413 | | - throw new StringIndexOutOfBoundsException(index); |
414 | | - } |
| 413 | + checkIndex(i, count); |
415 | 414 | if (isLatin1()) { |
416 | 415 | return value[i] & 0xff; |
417 | 416 | } |
@@ -505,9 +504,9 @@ public int offsetByCodePoints(int index, int codePointOffset) { |
505 | 504 | */ |
506 | 505 | public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) |
507 | 506 | { |
508 | | - checkRangeSIOOBE(srcBegin, srcEnd, count); // compatible to old version |
| 507 | + Preconditions.checkFromToIndex(srcBegin, srcEnd, count, Preconditions.SIOOBE_FORMATTER); // compatible to old version |
509 | 508 | int n = srcEnd - srcBegin; |
510 | | - checkRange(dstBegin, dstBegin + n, dst.length); |
| 509 | + Preconditions.checkFromToIndex(dstBegin, dstBegin + n, dst.length, Preconditions.IOOBE_FORMATTER); |
511 | 510 | if (isLatin1()) { |
512 | 511 | StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin); |
513 | 512 | } else { |
@@ -677,7 +676,7 @@ public AbstractStringBuilder append(CharSequence s, int start, int end) { |
677 | 676 | if (s == null) { |
678 | 677 | s = "null"; |
679 | 678 | } |
680 | | - checkRange(start, end, s.length()); |
| 679 | + Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER); |
681 | 680 | int len = end - start; |
682 | 681 | ensureCapacityInternal(count + len); |
683 | 682 | if (s instanceof String) { |
@@ -736,7 +735,7 @@ public AbstractStringBuilder append(char[] str) { |
736 | 735 | */ |
737 | 736 | public AbstractStringBuilder append(char[] str, int offset, int len) { |
738 | 737 | int end = offset + len; |
739 | | - checkRange(offset, end, str.length); |
| 738 | + Preconditions.checkFromToIndex(offset, end, str.length, Preconditions.IOOBE_FORMATTER); |
740 | 739 | ensureCapacityInternal(count + len); |
741 | 740 | appendChars(str, offset, end); |
742 | 741 | return this; |
@@ -914,7 +913,7 @@ public AbstractStringBuilder delete(int start, int end) { |
914 | 913 | if (end > count) { |
915 | 914 | end = count; |
916 | 915 | } |
917 | | - checkRangeSIOOBE(start, end, count); |
| 916 | + Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER); |
918 | 917 | int len = end - start; |
919 | 918 | if (len > 0) { |
920 | 919 | shift(end, -len); |
@@ -997,7 +996,7 @@ public AbstractStringBuilder replace(int start, int end, String str) { |
997 | 996 | if (end > count) { |
998 | 997 | end = count; |
999 | 998 | } |
1000 | | - checkRangeSIOOBE(start, end, count); |
| 999 | + Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER); |
1001 | 1000 | int len = str.length(); |
1002 | 1001 | int newCount = count + len - (end - start); |
1003 | 1002 | ensureCapacityInternal(newCount); |
@@ -1067,7 +1066,7 @@ public CharSequence subSequence(int start, int end) { |
1067 | 1066 | * greater than {@code end}. |
1068 | 1067 | */ |
1069 | 1068 | public String substring(int start, int end) { |
1070 | | - checkRangeSIOOBE(start, end, count); |
| 1069 | + Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER); |
1071 | 1070 | if (isLatin1()) { |
1072 | 1071 | return StringLatin1.newString(value, start, end - start); |
1073 | 1072 | } |
@@ -1104,7 +1103,7 @@ public AbstractStringBuilder insert(int index, char[] str, int offset, |
1104 | 1103 | int len) |
1105 | 1104 | { |
1106 | 1105 | checkOffset(index, count); |
1107 | | - checkRangeSIOOBE(offset, offset + len, str.length); |
| 1106 | + Preconditions.checkFromToIndex(offset, offset + len, str.length, Preconditions.SIOOBE_FORMATTER); |
1108 | 1107 | ensureCapacityInternal(count + len); |
1109 | 1108 | shift(index, len); |
1110 | 1109 | count += len; |
@@ -1292,7 +1291,7 @@ public AbstractStringBuilder insert(int dstOffset, CharSequence s, |
1292 | 1291 | s = "null"; |
1293 | 1292 | } |
1294 | 1293 | checkOffset(dstOffset, count); |
1295 | | - checkRange(start, end, s.length()); |
| 1294 | + Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER); |
1296 | 1295 | int len = end - start; |
1297 | 1296 | ensureCapacityInternal(count + len); |
1298 | 1297 | shift(dstOffset, len); |
@@ -1795,20 +1794,4 @@ private final void appendChars(CharSequence s, int off, int end) { |
1795 | 1794 | } |
1796 | 1795 | count += end - off; |
1797 | 1796 | } |
1798 | | - |
1799 | | - /* IndexOutOfBoundsException, if out of bounds */ |
1800 | | - private static void checkRange(int start, int end, int len) { |
1801 | | - if (start < 0 || start > end || end > len) { |
1802 | | - throw new IndexOutOfBoundsException( |
1803 | | - "start " + start + ", end " + end + ", length " + len); |
1804 | | - } |
1805 | | - } |
1806 | | - |
1807 | | - /* StringIndexOutOfBoundsException, if out of bounds */ |
1808 | | - private static void checkRangeSIOOBE(int start, int end, int len) { |
1809 | | - if (start < 0 || start > end || end > len) { |
1810 | | - throw new StringIndexOutOfBoundsException( |
1811 | | - "start " + start + ", end " + end + ", length " + len); |
1812 | | - } |
1813 | | - } |
1814 | 1797 | } |
0 commit comments