Fix documentation and method names in InternalPreconditions.
Makes InternalPreconditions.checkStringBounds() critical.
String.getChars() now checks bounds and is reused by StringBuilder.
Change-Id: Id70520c244a3196f4af4ecd323603a1a4c948fd6
diff --git a/user/super/com/google/gwt/emul/java/lang/AbstractStringBuilder.java b/user/super/com/google/gwt/emul/java/lang/AbstractStringBuilder.java
index 40c3819..4aec949 100644
--- a/user/super/com/google/gwt/emul/java/lang/AbstractStringBuilder.java
+++ b/user/super/com/google/gwt/emul/java/lang/AbstractStringBuilder.java
@@ -15,8 +15,6 @@
*/
package java.lang;
-import static javaemul.internal.InternalPreconditions.checkStringBounds;
-
/**
* A base class to share implementation between {@link StringBuffer} and {@link StringBuilder}.
* <p>
@@ -63,11 +61,7 @@
}
public void getChars(int srcStart, int srcEnd, char[] dst, int dstStart) {
- checkStringBounds(srcStart, srcEnd, length());
- checkStringBounds(dstStart, dstStart + (srcEnd - srcStart), dst.length);
- while (srcStart < srcEnd) {
- dst[dstStart++] = string.charAt(srcStart++);
- }
+ string.getChars(srcStart, srcEnd, dst, dstStart);
}
/**
diff --git a/user/super/com/google/gwt/emul/java/lang/String.java b/user/super/com/google/gwt/emul/java/lang/String.java
index 18e1a5b..3b28b4e 100644
--- a/user/super/com/google/gwt/emul/java/lang/String.java
+++ b/user/super/com/google/gwt/emul/java/lang/String.java
@@ -16,7 +16,7 @@
package java.lang;
-import static javaemul.internal.InternalPreconditions.checkStringBounds;
+import static javaemul.internal.InternalPreconditions.checkCriticalStringBounds;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
@@ -137,7 +137,7 @@
public static String valueOf(char x[], int offset, int count) {
int end = offset + count;
- checkStringBounds(offset, end, x.length);
+ checkCriticalStringBounds(offset, end, x.length);
// Work around function.prototype.apply call stack size limits:
// https://code.google.com/p/v8/issues/detail?id=2896
// Performance: http://jsperf.com/string-fromcharcode-test/13
@@ -448,8 +448,11 @@
}
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
- for (int srcIdx = srcBegin; srcIdx < srcEnd; ++srcIdx) {
- dst[dstBegin++] = charAt(srcIdx);
+ checkCriticalStringBounds(srcBegin, srcEnd, length());
+ checkCriticalStringBounds(dstBegin, dstBegin + (srcEnd - srcBegin), dst.length);
+
+ while (srcBegin < srcEnd) {
+ dst[dstBegin++] = charAt(srcBegin++);
}
}
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/InternalPreconditions.java b/user/super/com/google/gwt/emul/javaemul/internal/InternalPreconditions.java
index ff5f30b..f47a491 100644
--- a/user/super/com/google/gwt/emul/javaemul/internal/InternalPreconditions.java
+++ b/user/super/com/google/gwt/emul/javaemul/internal/InternalPreconditions.java
@@ -441,7 +441,7 @@
}
/**
- * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
+ * Ensures that {@code index} specifies a valid <i>element</i> in a list or string of size
* {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
*/
public static void checkElementIndex(int index, int size) {
@@ -463,7 +463,7 @@
}
/**
- * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of
+ * Ensures that {@code index} specifies a valid <i>position</i> in a list of
* size {@code size}. A position index may range from zero to {@code size}, inclusive.
*/
public static void checkPositionIndex(int index, int size) {
@@ -485,8 +485,8 @@
}
/**
- * Ensures that {@code start} and {@code end} specify a valid <i>positions</i> in an array, list
- * or string of size {@code size}, and are in order. A position index may range from zero to
+ * Ensures that {@code start} and {@code end} specify a valid <i>positions</i> in a list
+ * of size {@code size}, and are in order. A position index may range from zero to
* {@code size}, inclusive.
*/
public static void checkPositionIndexes(int start, int end, int size) {
@@ -502,16 +502,14 @@
}
/**
- * Ensures that {@code start} and {@code end} specify a valid <i>positions</i> in an array, list
- * or string of size {@code size}, and are in order. A position index may range from zero to
+ * Ensures that {@code start} and {@code end} specify a valid <i>positions</i> in a list
+ * of size {@code size}, and are in order. A position index may range from zero to
* {@code size}, inclusive.
*/
public static void checkCriticalPositionIndexes(int start, int end, int size) {
- if (start < 0) {
- throw new IndexOutOfBoundsException("fromIndex: " + start + " < 0");
- }
- if (end > size) {
- throw new IndexOutOfBoundsException("toIndex: " + end + " > size " + size);
+ if (start < 0 || end > size) {
+ throw new IndexOutOfBoundsException(
+ "fromIndex: " + start + ", toIndex: " + end + ", size: " + size);
}
if (start > end) {
throw new IllegalArgumentException("fromIndex: " + start + " > toIndex: " + end);
@@ -519,36 +517,30 @@
}
/**
- * Checks that bounds are correct.
+ * Checks that array bounds are correct.
*
+ * @throws IllegalArgumentException if {@code start > end}
* @throws ArrayIndexOutOfBoundsException if the range is not legal
*/
public static void checkCriticalArrayBounds(int start, int end, int length) {
if (start > end) {
throw new IllegalArgumentException("fromIndex: " + start + " > toIndex: " + end);
}
- if (start < 0) {
- throw new ArrayIndexOutOfBoundsException("fromIndex: " + start + " < 0");
- }
- if (end > length) {
- throw new ArrayIndexOutOfBoundsException("toIndex: " + end + " > length " + length);
+ if (start < 0 || end > length) {
+ throw new ArrayIndexOutOfBoundsException(
+ "fromIndex: " + start + ", toIndex: " + end + ", length: " + length);
}
}
/**
- * Checks that bounds are correct.
+ * Checks that string bounds are correct.
*
* @throws StringIndexOutOfBoundsException if the range is not legal
*/
- public static void checkStringBounds(int start, int end, int size) {
- if (start < 0) {
- throw new StringIndexOutOfBoundsException("fromIndex: " + start + " < 0");
- }
- if (end > size) {
- throw new StringIndexOutOfBoundsException("toIndex: " + end + " > size " + size);
- }
- if (end < start) {
- throw new StringIndexOutOfBoundsException("fromIndex: " + start + " > toIndex: " + end);
+ public static void checkCriticalStringBounds(int start, int end, int length) {
+ if (start < 0 || end > length || end < start) {
+ throw new StringIndexOutOfBoundsException(
+ "fromIndex: " + start + ", toIndex: " + end + ", length: " + length);
}
}