📄 stringbuilder.scala
字号:
def append(x: Float): StringBuilder = append(System.Convert.ToString(x)) def append(x: Double): StringBuilder = append(System.Convert.ToString(x)) /** Removes the characters in a substring of this sequence. * The substring begins at the specified <code>start</code> and extends to * the character at index <code>end - 1</code> or to the end of the * sequence if no such character exists. If * <code>start</code> is equal to <code>end</code>, no changes are made. * * @param start The beginning index, inclusive. * @param end The ending index, exclusive. * @return This object. * @throws StringIndexOutOfBoundsException if <code>start</code> * is negative, greater than <code>length()</code>, or * greater than <code>end</code>. */ def delete(start: Int, end: Int): StringBuilder = { if (start < 0 || start > end) throw new StringIndexOutOfBoundsException//(start) val end0 = if (end > count) count else end val len = end0 - start if (len > 0) { compat.Platform.arraycopy(value, start + len, value, start, count - end0) count -= len } this } /** Replaces the characters in a substring of this sequence * with characters in the specified <code>String</code>. The substring * begins at the specified <code>start</code> and extends to the character * at index <code>end - 1</code> or to the end of the sequence if no such * character exists. First the characters in the substring are removed and * then the specified <code>String</code> is inserted at <code>start</code>. * * @param start The beginning index, inclusive. * @param end The ending index, exclusive. * @param str String that will replace previous contents. * @return This object. * @throws StringIndexOutOfBoundsException if <code>start</code> * is negative, greater than <code>length()</code>, or * greater than <code>end</code>. */ def replace(start: Int, end: Int, str: String) { if (start < 0 || start > count || start > end) throw new StringIndexOutOfBoundsException//(start) val end0 = if (end > count) count else end val len = str.length() val newCount = count + len - (end0 - start) if (newCount > value.length) expandCapacity(newCount) compat.Platform.arraycopy(value, end, value, start + len, count - end) compat.Platform.arraycopy(str.ToCharArray, 0, value, start, len) count = newCount this } /** Inserts the string representation of a subarray of the <code>str</code> * array argument into this sequence. The subarray begins at the specified * <code>offset</code> and extends <code>len</code> <code>char</code>s. * The characters of the subarray are inserted into this sequence at * the position indicated by <code>index</code>. The length of this * sequence increases by <code>len</code> <code>Char</code>s. * * @param index position at which to insert subarray. * @param str a <code>Char</code> array. * @param offset the index of the first <code>char</code> in subarray to * be inserted. * @param len the number of <code>Char</code>s in the subarray to * be inserted. * @return This object * @throws StringIndexOutOfBoundsException if <code>index</code> * is negative or greater than <code>length()</code>, or * <code>offset</code> or <code>len</code> are negative, or * <code>(offset+len)</code> is greater than * <code>str.length</code>. */ def insert(index: Int, str: Array[Char], offset: Int, len: Int): StringBuilder = { if (index < 0 || index > count) throw new StringIndexOutOfBoundsException//(index) if (offset < 0 || len < 0 || offset > str.length - len) throw new StringIndexOutOfBoundsException/*( "offset " + offset + ", len " + len + ", str.length " + str.length)*/ val newCount = count + len if (newCount > value.length) expandCapacity(newCount) compat.Platform.arraycopy(value, index, value, index + len, count - index) compat.Platform.arraycopy(str, offset, value, index, len) count = newCount this } /** <p> * Inserts the string representation of the <code>Any</code> * argument into this character sequence. * </p> * <p> * The second argument is converted to a string as if by the method * <code>System.Convert.ToString</code>, and the characters of that * string are then inserted into this sequence at the indicated * offset. * </p> * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * sequence. * </p> * * @param offset the offset. * @param x an <code>Any</code> value. * @return a reference to this object. * @throws StringIndexOutOfBoundsException if the offset is invalid. */ def insert(at: Int, x: Any): StringBuilder = insert(at, System.Convert.ToString(x)) /** Inserts the string into this character sequence. * * @param at the offset position. * @param x a string. * @return a reference to this object. * @throws StringIndexOutOfBoundsException if the offset is invalid. */ def insert(at: Int, x: String): StringBuilder = { if (at < 0 || at > count) throw new StringIndexOutOfBoundsException//(at) val str = if (x == null) "null" else x val len = str.length val newCount = count + len if (newCount > value.length) expandCapacity(newCount) compat.Platform.arraycopy(value, at, value, at + len, count - at) compat.Platform.arraycopy(str.ToCharArray, 0, value, at, len) count = newCount this } /** Inserts the string representation of the <code>Char</code> array * argument into this sequence. * * @param at the offset position. * @param x a character array. * @return a reference to this object. * @throws StringIndexOutOfBoundsException if the offset is invalid. */ def insert(at: Int, x: Array[Char]): StringBuilder = { if (at < 0 || at > count) throw new StringIndexOutOfBoundsException//(at) val len = x.length val newCount = count + len if (newCount > value.length) expandCapacity(newCount) compat.Platform.arraycopy(value, at, value, at + len, count - at) compat.Platform.arraycopy(x, 0, value, at, len) count = newCount this } def insert(at: Int, x: Boolean): StringBuilder = insert(at, System.Convert.ToString(x)) def insert(at: Int, x: Char): StringBuilder = { if (at < 0 || at > count) throw new StringIndexOutOfBoundsException//(at) val newCount = count + 1 if (newCount > value.length) expandCapacity(newCount) compat.Platform.arraycopy(value, at, value, at + 1, count - at) value(at) = x count = newCount this } def insert(at: Int, x: Int): StringBuilder = insert(at, System.Convert.ToString(x)) def insert(at: Int, x: Long): StringBuilder = insert(at, System.Convert.ToString(x)) def insert(at: Int, x: Float): StringBuilder = insert(at, System.Convert.ToString(x)) def insert(at: Int, x: Double): StringBuilder = insert(at, System.Convert.ToString(x)) /** Returns the index within this string of the first occurrence of the * specified substring. The integer returned is the smallest value * <i>k</i> such that: * <blockquote><pre> * this.toString().startsWith(str, <i>k</i>) * </pre></blockquote> * is <code>true</code>. * * @param str any string. * @return if the string argument occurs as a substring within this * object, then the index of the first character of the first * such substring is returned; if it does not occur as a * substring, <code>-1</code> is returned. * @throws NullPointerException if <code>str</code> is <code>null</code>. */ def indexOf(str: String): Int = indexOf(str, 0) def indexOf(str: String, fromIndex: Int): Int = StringBuilder.indexOf(value, 0, count, str.ToCharArray, 0, str.length(), fromIndex) /** Returns the index within this string of the rightmost occurrence * of the specified substring. The rightmost empty string "" is * considered to occur at the index value <code>this.length()</code>. * The returned index is the largest value <i>k</i> such that * <blockquote><pre> * this.toString().startsWith(str, k) * </pre></blockquote> * is true. * * @param str the substring to search for. * @return if the string argument occurs one or more times as a substring * within this object, then the index of the first character of * the last such substring is returned. If it does not occur as * a substring, <code>-1</code> is returned. * @throws NullPointerException if <code>str</code> is <code>null</code>. */ def lastIndexOf(str: String): Int = lastIndexOf(str, count) def lastIndexOf(str: String, fromIndex: Int): Int = StringBuilder.lastIndexOf(value, 0, count, str.ToCharArray, 0, str.length(), fromIndex) /** <p> * Causes this character sequence to be replaced by the reverse of the * sequence. If there are any surrogate pairs included in the sequence, * these are treated as single characters for the reverse operation. * Thus, the order of the high-low surrogates is never reversed. * </p> * <p> * Let <i>n</i> be the character length of this character sequence * (not the length in <code>Char</code> values) just prior to * execution of the <code>reverse</code> method. Then the * character at index <i>k</i> in the new character sequence is * equal to the character at index <i>n-k-1</i> in the old * character sequence. * </p> * * @return a reference to this object. */ def reverse(): StringBuilder = { val n = count - 1 var j = (n-1) >> 1 while (j >= 0) { val temp = value(j) val temp2 = value(n - j) value(j) = temp2 value(n - j) = temp j -= 1 } this } /** Returns a string representing the data in this sequence. * A new <code>String</code> object is allocated and initialized to * contain the character sequence currently represented by this * object. This <code>String</code> is then returned. Subsequent * changes to this sequence do not affect the contents of the * <code>String</code>. * * @return a string representation of this sequence of characters. */ override def toString(): String = new String(value, 0, count)}object StringBuilder { // method <code>java.util.Arrays.copyOf</code> exists since 1.6 private def copyOf(src: Array[Char], newLength: Int): Array[Char] = { val dest = new Array[Char](newLength) compat.Platform.arraycopy(src, 0, dest, 0, Math.min(src.length, newLength)) dest } private def indexOf(source: Array[Char], sourceOffset: Int, sourceCount: Int, target: Array[Char], targetOffset: Int, targetCount: Int, fromIndex: Int): Int = if (fromIndex >= sourceCount) if (targetCount == 0) sourceCount else -1 else { val inx = if (fromIndex < 0) 0 else fromIndex if (targetCount == 0) inx else { val first = target(targetOffset) val max = sourceOffset + (sourceCount - targetCount) var i = sourceOffset + inx while (i <= max) { /* Look for first character. */ if (source(i) != first) { i += 1 while (i <= max && source(i) != first) i += 1 } /* Found first character, now look at the rest of v2 */ if (i <= max) { var j = i + 1 val end = j + targetCount - 1 var k = targetOffset + 1 while (j < end && source(j) == target(k)) { j += 1 k += 1 } if (j == end) { /* Found whole string. */ return i - sourceOffset } } // if i += 1 } // while -1 } } private def lastIndexOf(source: Array[Char], sourceOffset: Int, sourceCount: Int, target: Array[Char], targetOffset: Int, targetCount: Int, fromIndex: Int): Int = { val rightIndex = sourceCount - targetCount if (fromIndex < 0) return -1 val inx = if (fromIndex > rightIndex) rightIndex else fromIndex // Empty string always matches if (targetCount == 0) return inx val strLastIndex = targetOffset + targetCount - 1 val strLastChar = target(strLastIndex) val min = sourceOffset + targetCount - 1 var i = min + fromIndex while (true) { while (i >= min && source(i) != strLastChar) i -= 1 if (i < min) return -1 var j = i - 1 val start = j - (targetCount - 1) var k = strLastIndex - 1 var outerWhile = false while (j > start && !outerWhile) { if (source(j) != target(k)) { j -= 1 k -= 1 i -= 1 outerWhile = true } } if (!outerWhile) return start - sourceOffset + 1 } -1 }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -