stringbuilder.scala
来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 954 行 · 第 1/3 页
SCALA
954 行
/** <p> * Inserts the string representation of the <code>Long</code> argument * into this sequence. * </p> * <p> * The offset argument must be greater than or equal to 0, and less than * or equal to the length of this sequence. * </p> * * @param at the offset position. * @param x a <code>Long</code> value. * @return a reference to this object. */ def insert(at: Int, x: Long): StringBuilder = insert(at, String.valueOf(x)) /** <p> * Inserts the string representation of the <code>Float</code> argument * into this sequence. * </p> * <p> * The offset argument must be greater than or equal to 0, and less than * or equal to the length of this sequence. * </p> * * @param at the offset position. * @param x a <code>Float</code> value. * @return a reference to this object. */ def insert(at: Int, x: Float): StringBuilder = insert(at, String.valueOf(x)) /** <p> * Inserts the string representation of the <code>Double</code> argument * into this sequence. * </p> * <p> * The offset argument must be greater than or equal to 0, and less than * or equal to the length of this sequence. * </p> * * @param at the offset position. * @param x a <code>Double</code> value. * @return a reference to this object. */ def insert(at: Int, x: Double): StringBuilder = insert(at, String.valueOf(x)) /** <p> * 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: * </p> * <blockquote><pre> * this.toString().startsWith(str, <i>k</i>)</pre> * </blockquote> * <p> * is <code>true</code>. * </p> * * @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) /** <p> * Returns the index within this string of the first occurrence of the * specified substring, starting at the specified index. The integer * returned is the smallest value <code>k</code> for which: * </p><pre> * k >= Math.min(fromIndex, str.length()) && * this.toString().startsWith(str, k)</pre> * <p> * If no such value of <code>k</code> exists, then <code>-1</code> * is returned. * </p> * * @param str the substring for which to search. * @param fromIndex the index from which to start the search. * @return the index within this string of the first occurrence * of the specified substring, starting at the specified index. */ def indexOf(str: String, fromIndex: Int): Int = StringBuilder.indexOf(value, 0, count, str.toCharArray, 0, str.length(), fromIndex) /** <p> * 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 * </p> * <blockquote><pre> * this.toString().startsWith(str, k)</pre> * </blockquote> * <p> * is true. * </p> * * @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) /** <p> * Returns the index within this string of the last occurrence of the * specified substring. The integer returned is the largest value * <code>k</code> such that: * </p><pre> * k <= Math.min(fromIndex, str.length()) && * this.toString().startsWith(str, k)</pre> * <p> * If no such value of <code>k</code> exists, then <code>-1</code> * is returned. * </p> * * @param str the substring to search for. * @param fromIndex the index to start the search from. * @return the index within this sequence of the last occurrence * of the specified substring. */ 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 = { var hasSurrogate = false val n = count - 1 var j = (n-1) >> 1 while (j >= 0) { val temp = value(j) val temp2 = value(n - j) if (!hasSurrogate) hasSurrogate = (temp >= StringBuilder.MIN_SURROGATE && temp <= StringBuilder.MAX_SURROGATE) || (temp2 >= StringBuilder.MIN_SURROGATE && temp2 <= StringBuilder.MAX_SURROGATE) value(j) = temp2 value(n - j) = temp j -= 1 } if (hasSurrogate) { // Reverse back all valid surrogate pairs var i = 0 while (i < count - 1) { val c2 = value(i) if (StringBuilder.isLowSurrogate(c2)) { val c1 = value(i + 1) if (StringBuilder.isHighSurrogate(c1)) { value(i) = c1; i += 1 value(i) = c2 } } i += 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) @throws(classOf[java.io.IOException]) private def writeObject(s: java.io.ObjectOutputStream) { s.defaultWriteObject() s.writeInt(count) s.writeObject(value) } @throws(classOf[java.io.IOException]) private def readObject(s: java.io.ObjectInputStream ) { s.defaultReadObject() count = s.readInt() value = s.readObject().asInstanceOf[Array[Char]] }}object StringBuilder { private val MIN_HIGH_SURROGATE = '\uD800' private val MAX_HIGH_SURROGATE = '\uDBFF' private val MIN_LOW_SURROGATE = '\uDC00' private val MAX_LOW_SURROGATE = '\uDFFF' // constants <code>java.langCharacter.MIN-/MAX_SURROGATE</code> exist since 1.5 private val MIN_SURROGATE = MIN_HIGH_SURROGATE private val MAX_SURROGATE = MAX_LOW_SURROGATE // methods <code>java.langCharacter.isLow-/isHighSurrogate</code> exist since 1.5 private def isLowSurrogate(ch: Char): Boolean = MIN_LOW_SURROGATE <= ch && ch <= MAX_LOW_SURROGATE private def isHighSurrogate(ch: Char): Boolean = MIN_HIGH_SURROGATE <= ch && ch <= MAX_HIGH_SURROGATE // 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 } implicit def toCharSequence(sb: StringBuilder): java.lang.CharSequence = new java.lang.CharSequence { def length: Int = sb.length def charAt(index: Int): Char = sb.charAt(index) def subSequence(start: Int, end: Int): java.lang.CharSequence = sb.substring(start, end) override def toString: String = sb.toString }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?