< prev index next >

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

Print this page

        

*** 34,43 **** --- 34,44 ---- import static java.lang.String.COMPACT_STRINGS; import static java.lang.String.UTF16; import static java.lang.String.LATIN1; import static java.lang.String.checkIndex; import static java.lang.String.checkOffset; + import static java.lang.String.checkBoundsBeginEnd; /** * A mutable sequence of characters. * <p> * Implements a modifiable string. At any point in time it contains some
*** 305,314 **** --- 306,317 ---- * @exception IndexOutOfBoundsException if the {@code index} * argument is negative or not less than the length of this * sequence. */ public int codePointAt(int index) { + int count = this.count; + byte[] value = this.value; checkIndex(index, count); if (isLatin1()) { return value[index] & 0xff; } return StringUTF16.codePointAtSB(value, index, count);
*** 558,568 **** val[count++] = 'n'; val[count++] = 'u'; val[count++] = 'l'; val[count++] = 'l'; } else { ! checkOffset(count + 4, val.length >> 1); StringUTF16.putChar(val, count++, 'n'); StringUTF16.putChar(val, count++, 'u'); StringUTF16.putChar(val, count++, 'l'); StringUTF16.putChar(val, count++, 'l'); } --- 561,571 ---- val[count++] = 'n'; val[count++] = 'u'; val[count++] = 'l'; val[count++] = 'l'; } else { ! checkBoundsBeginEnd(count, count + 4, val.length >> 1); StringUTF16.putChar(val, count++, 'n'); StringUTF16.putChar(val, count++, 'u'); StringUTF16.putChar(val, count++, 'l'); StringUTF16.putChar(val, count++, 'l'); }
*** 693,709 **** val[count++] = 's'; val[count++] = 'e'; } } else { if (b) { ! checkOffset(count + 4, val.length >> 1); StringUTF16.putChar(val, count++, 't'); StringUTF16.putChar(val, count++, 'r'); StringUTF16.putChar(val, count++, 'u'); StringUTF16.putChar(val, count++, 'e'); } else { ! checkOffset(count + 5, val.length >> 1); StringUTF16.putChar(val, count++, 'f'); StringUTF16.putChar(val, count++, 'a'); StringUTF16.putChar(val, count++, 'l'); StringUTF16.putChar(val, count++, 's'); StringUTF16.putChar(val, count++, 'e'); --- 696,712 ---- val[count++] = 's'; val[count++] = 'e'; } } else { if (b) { ! checkBoundsBeginEnd(count, count + 4, val.length >> 1); StringUTF16.putChar(val, count++, 't'); StringUTF16.putChar(val, count++, 'r'); StringUTF16.putChar(val, count++, 'u'); StringUTF16.putChar(val, count++, 'e'); } else { ! checkBoundsBeginEnd(count, count + 5, val.length >> 1); StringUTF16.putChar(val, count++, 'f'); StringUTF16.putChar(val, count++, 'a'); StringUTF16.putChar(val, count++, 'l'); StringUTF16.putChar(val, count++, 's'); StringUTF16.putChar(val, count++, 'e');
*** 753,772 **** * * @param i an {@code int}. * @return a reference to this object. */ public AbstractStringBuilder append(int i) { int spaceNeeded = count + Integer.stringSize(i); ensureCapacityInternal(spaceNeeded); if (isLatin1()) { Integer.getChars(i, spaceNeeded, value); } else { byte[] val = this.value; ! checkOffset(spaceNeeded, val.length >> 1); Integer.getCharsUTF16(i, spaceNeeded, val); } ! count = spaceNeeded; return this; } /** * Appends the string representation of the {@code long} --- 756,776 ---- * * @param i an {@code int}. * @return a reference to this object. */ public AbstractStringBuilder append(int i) { + int count = this.count; int spaceNeeded = count + Integer.stringSize(i); ensureCapacityInternal(spaceNeeded); if (isLatin1()) { Integer.getChars(i, spaceNeeded, value); } else { byte[] val = this.value; ! checkBoundsBeginEnd(count, spaceNeeded, val.length >> 1); Integer.getCharsUTF16(i, spaceNeeded, val); } ! this.count = spaceNeeded; return this; } /** * Appends the string representation of the {@code long}
*** 779,798 **** * * @param l a {@code long}. * @return a reference to this object. */ public AbstractStringBuilder append(long l) { int spaceNeeded = count + Long.stringSize(l); ensureCapacityInternal(spaceNeeded); if (isLatin1()) { Long.getChars(l, spaceNeeded, value); } else { byte[] val = this.value; ! checkOffset(spaceNeeded, val.length >> 1); Long.getCharsUTF16(l, spaceNeeded, val); } ! count = spaceNeeded; return this; } /** * Appends the string representation of the {@code float} --- 783,803 ---- * * @param l a {@code long}. * @return a reference to this object. */ public AbstractStringBuilder append(long l) { + int count = this.count; int spaceNeeded = count + Long.stringSize(l); ensureCapacityInternal(spaceNeeded); if (isLatin1()) { Long.getChars(l, spaceNeeded, value); } else { byte[] val = this.value; ! checkBoundsBeginEnd(count, spaceNeeded, val.length >> 1); Long.getCharsUTF16(l, spaceNeeded, val); } ! this.count = spaceNeeded; return this; } /** * Appends the string representation of the {@code float}
*** 841,858 **** * @throws StringIndexOutOfBoundsException if {@code start} * is negative, greater than {@code length()}, or * greater than {@code end}. */ public AbstractStringBuilder delete(int start, int end) { if (end > count) { end = count; } checkRangeSIOOBE(start, end, count); int len = end - start; if (len > 0) { shift(end, -len); ! count -= len; } return this; } /** --- 846,864 ---- * @throws StringIndexOutOfBoundsException if {@code start} * is negative, greater than {@code length()}, or * greater than {@code end}. */ public AbstractStringBuilder delete(int start, int end) { + int count = this.count; if (end > count) { end = count; } checkRangeSIOOBE(start, end, count); int len = end - start; if (len > 0) { shift(end, -len); ! this.count = count - len; } return this; } /**
*** 923,941 **** * @throws StringIndexOutOfBoundsException if {@code start} * is negative, greater than {@code length()}, or * greater than {@code end}. */ public AbstractStringBuilder replace(int start, int end, String str) { if (end > count) { end = count; } checkRangeSIOOBE(start, end, count); int len = str.length(); int newCount = count + len - (end - start); ensureCapacityInternal(newCount); shift(end, newCount - count); ! count = newCount; putStringAt(start, str); return this; } /** --- 929,948 ---- * @throws StringIndexOutOfBoundsException if {@code start} * is negative, greater than {@code length()}, or * greater than {@code end}. */ public AbstractStringBuilder replace(int start, int end, String str) { + int count = this.count; if (end > count) { end = count; } checkRangeSIOOBE(start, end, count); int len = str.length(); int newCount = count + len - (end - start); ensureCapacityInternal(newCount); shift(end, newCount - count); ! this.count = newCount; putStringAt(start, str); return this; } /**
*** 1420,1429 **** --- 1427,1440 ---- * @return the index of the first occurrence of the specified substring, * starting at the specified index, * or {@code -1} if there is no such occurrence. */ public int indexOf(String str, int fromIndex) { + byte[] value = this.value; + int count = this.count; + byte coder = this.coder; + checkOffset(count, value.length >> coder); return String.indexOf(value, coder, count, str, fromIndex); } /** * Returns the index within this string of the last occurrence of the
*** 1460,1469 **** --- 1471,1484 ---- * @return the index of the last occurrence of the specified substring, * searching backward from the specified index, * or {@code -1} if there is no such occurrence. */ public int lastIndexOf(String str, int fromIndex) { + byte[] value = this.value; + int count = this.count; + byte coder = this.coder; + checkOffset(count, value.length >> coder); return String.lastIndexOf(value, coder, count, str, fromIndex); } /** * Causes this character sequence to be replaced by the reverse of
< prev index next >