< prev index next >
src/java.base/share/classes/java/lang/String.java
Print this page
rev 11896 : [mq]: 8071571-String-substring-to-slow-path
@@ -325,11 +325,11 @@
* @see #String(byte[])
*/
@Deprecated
public String(byte ascii[], int hibyte, int offset, int count) {
checkBounds(ascii, offset, count);
- char value[] = new char[count];
+ char[] value = new char[count];
if (hibyte == 0) {
for (int i = count; i-- > 0;) {
value[i] = (char)(ascii[i + offset] & 0xff);
}
@@ -981,15 +981,14 @@
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
- String anotherString = (String)anObject;
- int n = value.length;
- if (n == anotherString.value.length) {
- char v1[] = value;
- char v2[] = anotherString.value;
+ char[] v1 = value;
+ char[] v2 = ((String)anObject).value;
+ int n = v1.length;
+ if (n == v2.length) {
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
@@ -1018,12 +1017,12 @@
public boolean contentEquals(StringBuffer sb) {
return contentEquals((CharSequence)sb);
}
private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
- char v1[] = value;
- char v2[] = sb.getValue();
+ char[] v1 = value;
+ char[] v2 = sb.getValue();
int n = v1.length;
if (n != sb.length()) {
return false;
}
for (int i = 0; i < n; i++) {
@@ -1064,11 +1063,11 @@
// Argument is a String
if (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequence
- char v1[] = value;
+ char[] v1 = value;
int n = v1.length;
if (n != cs.length()) {
return false;
}
for (int i = 0; i < n; i++) {
@@ -1154,24 +1153,22 @@
* is lexicographically less than the string argument; and a
* value greater than {@code 0} if this string is
* lexicographically greater than the string argument.
*/
public int compareTo(String anotherString) {
- int len1 = value.length;
- int len2 = anotherString.value.length;
+ char[] v1 = value;
+ char[] v2 = anotherString.value;
+ int len1 = v1.length;
+ int len2 = v2.length;
int lim = Math.min(len1, len2);
- char v1[] = value;
- char v2[] = anotherString.value;
- int k = 0;
- while (k < lim) {
+ for (int k = 0; k < lim; k++) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
- k++;
}
return len1 - len2;
}
/**
@@ -1276,18 +1273,18 @@
* exactly matches the specified subregion of the string argument;
* {@code false} otherwise.
*/
public boolean regionMatches(int toffset, String other, int ooffset,
int len) {
- char ta[] = value;
+ char[] ta = value;
int to = toffset;
- char pa[] = other.value;
+ char[] pa = other.value;
int po = ooffset;
// Note: toffset, ooffset, or len might be near -1>>>1.
if ((ooffset < 0) || (toffset < 0)
- || (toffset > (long)value.length - len)
- || (ooffset > (long)other.value.length - len)) {
+ || (toffset > (long)ta.length - len)
+ || (ooffset > (long)pa.length - len)) {
return false;
}
while (len-- > 0) {
if (ta[to++] != pa[po++]) {
return false;
@@ -1346,18 +1343,18 @@
* or case insensitive depends on the {@code ignoreCase}
* argument.
*/
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len) {
- char ta[] = value;
+ char[] ta = value;
int to = toffset;
- char pa[] = other.value;
+ char[] pa = other.value;
int po = ooffset;
// Note: toffset, ooffset, or len might be near -1>>>1.
if ((ooffset < 0) || (toffset < 0)
- || (toffset > (long)value.length - len)
- || (ooffset > (long)other.value.length - len)) {
+ || (toffset > (long)ta.length - len)
+ || (ooffset > (long)pa.length - len)) {
return false;
}
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
@@ -1403,17 +1400,17 @@
* <pre>
* this.substring(toffset).startsWith(prefix)
* </pre>
*/
public boolean startsWith(String prefix, int toffset) {
- char ta[] = value;
+ char[] ta = value;
int to = toffset;
- char pa[] = prefix.value;
+ char[] pa = prefix.value;
int po = 0;
- int pc = prefix.value.length;
+ int pc = pa.length;
// Note: toffset might be near -1>>>1.
- if ((toffset < 0) || (toffset > value.length - pc)) {
+ if ((toffset < 0) || (toffset > ta.length - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
@@ -1926,18 +1923,21 @@
* @exception IndexOutOfBoundsException if
* {@code beginIndex} is negative or larger than the
* length of this {@code String} object.
*/
public String substring(int beginIndex) {
+ if (beginIndex <= 0) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
+ return this;
+ }
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
- return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
+ return new String(value, beginIndex, subLen);
}
/**
* Returns a string that is a substring of this string. The
* substring begins at the specified {@code beginIndex} and
@@ -1959,22 +1959,26 @@
* this {@code String} object, or
* {@code beginIndex} is larger than
* {@code endIndex}.
*/
public String substring(int beginIndex, int endIndex) {
+ if (beginIndex <= 0) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
+ if (endIndex == value.length) {
+ return this;
+ }
+ }
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
- return ((beginIndex == 0) && (endIndex == value.length)) ? this
- : new String(value, beginIndex, subLen);
+ return new String(value, beginIndex, subLen);
}
/**
* Returns a character sequence that is a subsequence of this sequence.
*
@@ -2032,11 +2036,11 @@
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
- char buf[] = Arrays.copyOf(value, len + otherLen);
+ char[] buf = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
/**
@@ -2068,21 +2072,21 @@
* @return a string derived from this string by replacing every
* occurrence of {@code oldChar} with {@code newChar}.
*/
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
- int len = value.length;
- int i = -1;
char[] val = value; /* avoid getfield opcode */
+ int len = val.length;
+ int i = -1;
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
- char buf[] = new char[len];
+ char[] buf = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
@@ -2874,21 +2878,21 @@
* @return A string whose value is this string, with any leading and trailing white
* space removed, or this string if it has no leading or
* trailing white space.
*/
public String trim() {
- int len = value.length;
- int st = 0;
char[] val = value; /* avoid getfield opcode */
+ int end = val.length;
+ int beg = 0;
- while ((st < len) && (val[st] <= ' ')) {
- st++;
+ while ((beg < end) && (val[beg] <= ' ')) {
+ beg++;
}
- while ((st < len) && (val[len - 1] <= ' ')) {
- len--;
+ while ((beg < end) && (val[end - 1] <= ' ')) {
+ end--;
}
- return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
+ return substring(beg, end);
}
/**
* This object (which is already a string!) is itself returned.
*
@@ -3079,11 +3083,11 @@
* of this string and whose contents are initialized to contain
* the character sequence represented by this string.
*/
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
- char result[] = new char[value.length];
+ char[] result = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
/**
@@ -3264,12 +3268,11 @@
* @param c a {@code char}.
* @return a string of length {@code 1} containing
* as its single character the argument {@code c}.
*/
public static String valueOf(char c) {
- char data[] = {c};
- return new String(data, true);
+ return new String(new char[]{c}, true);
}
/**
* Returns the string representation of the {@code int} argument.
* <p>
< prev index next >