< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page

        

*** 325,335 **** * @see #String(byte[]) */ @Deprecated public String(byte ascii[], int hibyte, int offset, int count) { checkBounds(ascii, offset, count); ! char value[] = new char[count]; if (hibyte == 0) { for (int i = count; i-- > 0;) { value[i] = (char)(ascii[i + offset] & 0xff); } --- 325,335 ---- * @see #String(byte[]) */ @Deprecated public String(byte ascii[], int hibyte, int offset, int count) { checkBounds(ascii, offset, count); ! char[] value = new char[count]; if (hibyte == 0) { for (int i = count; i-- > 0;) { value[i] = (char)(ascii[i + offset] & 0xff); }
*** 981,995 **** 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; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; --- 981,994 ---- public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { ! 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,1031 **** public boolean contentEquals(StringBuffer sb) { return contentEquals((CharSequence)sb); } private boolean nonSyncContentEquals(AbstractStringBuilder sb) { ! char v1[] = value; ! char v2[] = sb.getValue(); int n = v1.length; ! if (n != sb.length()) { return false; } for (int i = 0; i < n; i++) { if (v1[i] != v2[i]) { return false; --- 1017,1030 ---- public boolean contentEquals(StringBuffer sb) { return contentEquals((CharSequence)sb); } private boolean nonSyncContentEquals(AbstractStringBuilder sb) { ! char[] v1 = value; ! char[] v2 = sb.getValue(); int n = v1.length; ! if (n != v2.length) { return false; } for (int i = 0; i < n; i++) { if (v1[i] != v2[i]) { return false;
*** 1064,1074 **** // Argument is a String if (cs instanceof String) { return equals(cs); } // Argument is a generic CharSequence ! char v1[] = value; int n = v1.length; if (n != cs.length()) { return false; } for (int i = 0; i < n; i++) { --- 1063,1073 ---- // Argument is a String if (cs instanceof String) { return equals(cs); } // Argument is a generic CharSequence ! char[] v1 = value; int n = v1.length; if (n != cs.length()) { return false; } for (int i = 0; i < n; i++) {
*** 1154,1177 **** * 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; int lim = Math.min(len1, len2); - char v1[] = value; - char v2[] = anotherString.value; ! int k = 0; ! while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } - k++; } return len1 - len2; } /** --- 1153,1174 ---- * 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) { ! char[] v1 = value; ! char[] v2 = anotherString.value; ! int len1 = v1.length; ! int len2 = v2.length; int lim = Math.min(len1, len2); ! for (int k = 0; k < lim; k++) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } } return len1 - len2; } /**
*** 1276,1293 **** * 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; int to = toffset; ! 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)) { return false; } while (len-- > 0) { if (ta[to++] != pa[po++]) { return false; --- 1273,1290 ---- * 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; int to = toffset; ! char[] pa = other.value; int po = ooffset; // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) ! || (toffset > (long)ta.length - len) ! || (ooffset > (long)pa.length - len)) { return false; } while (len-- > 0) { if (ta[to++] != pa[po++]) { return false;
*** 1346,1363 **** * 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; int to = toffset; ! 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)) { return false; } while (len-- > 0) { char c1 = ta[to++]; char c2 = pa[po++]; --- 1343,1360 ---- * 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; int to = toffset; ! char[] pa = other.value; int po = ooffset; // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) ! || (toffset > (long)ta.length - len) ! || (ooffset > (long)pa.length - len)) { return false; } while (len-- > 0) { char c1 = ta[to++]; char c2 = pa[po++];
*** 1403,1419 **** * <pre> * this.substring(toffset).startsWith(prefix) * </pre> */ public boolean startsWith(String prefix, int toffset) { ! char ta[] = value; int to = toffset; ! char pa[] = prefix.value; int po = 0; ! int pc = prefix.value.length; // Note: toffset might be near -1>>>1. ! if ((toffset < 0) || (toffset > value.length - pc)) { return false; } while (--pc >= 0) { if (ta[to++] != pa[po++]) { return false; --- 1400,1416 ---- * <pre> * this.substring(toffset).startsWith(prefix) * </pre> */ public boolean startsWith(String prefix, int toffset) { ! char[] ta = value; int to = toffset; ! char[] pa = prefix.value; int po = 0; ! int pc = pa.length; // Note: toffset might be near -1>>>1. ! if ((toffset < 0) || (toffset > ta.length - pc)) { return false; } while (--pc >= 0) { if (ta[to++] != pa[po++]) { return false;
*** 1926,1943 **** * @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) { throw new StringIndexOutOfBoundsException(beginIndex); } int subLen = value.length - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } ! return (beginIndex == 0) ? this : new String(value, beginIndex, subLen); } /** * Returns a string that is a substring of this string. The * substring begins at the specified {@code beginIndex} and --- 1923,1943 ---- * @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 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,1980 **** * this {@code String} object, or * {@code beginIndex} is larger than * {@code endIndex}. */ public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } 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); } /** * Returns a character sequence that is a subsequence of this sequence. * --- 1959,1984 ---- * 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 new String(value, beginIndex, subLen); } /** * Returns a character sequence that is a subsequence of this sequence. *
*** 2032,2042 **** int otherLen = str.length(); if (otherLen == 0) { return this; } int len = value.length; ! char buf[] = Arrays.copyOf(value, len + otherLen); str.getChars(buf, len); return new String(buf, true); } /** --- 2036,2046 ---- int otherLen = str.length(); if (otherLen == 0) { return this; } int len = value.length; ! char[] buf = Arrays.copyOf(value, len + otherLen); str.getChars(buf, len); return new String(buf, true); } /**
*** 2068,2088 **** * @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 */ while (++i < len) { if (val[i] == oldChar) { break; } } if (i < len) { ! char buf[] = new char[len]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i]; --- 2072,2092 ---- * @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) { 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]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i];
*** 2874,2894 **** * @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 */ ! while ((st < len) && (val[st] <= ' ')) { ! st++; } ! while ((st < len) && (val[len - 1] <= ' ')) { ! len--; } ! return ((st > 0) || (len < value.length)) ? substring(st, len) : this; } /** * This object (which is already a string!) is itself returned. * --- 2878,2898 ---- * @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() { char[] val = value; /* avoid getfield opcode */ + int end = val.length; + int beg = 0; ! while ((beg < end) && (val[beg] <= ' ')) { ! beg++; } ! while ((beg < end) && (val[end - 1] <= ' ')) { ! end--; } ! return substring(beg, end); } /** * This object (which is already a string!) is itself returned. *
*** 3079,3089 **** * 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]; System.arraycopy(value, 0, result, 0, value.length); return result; } /** --- 3083,3093 ---- * 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]; System.arraycopy(value, 0, result, 0, value.length); return result; } /**
*** 3264,3275 **** * @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); } /** * Returns the string representation of the {@code int} argument. * <p> --- 3268,3278 ---- * @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) { ! return new String(new char[]{c}, true); } /** * Returns the string representation of the {@code int} argument. * <p>
< prev index next >