stringbuilder.scala

来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 954 行 · 第 1/3 页

SCALA
954
字号
  def append(x: Array[Char], offset: Int, len: Int): StringBuilder = {    val newCount = count + len    if (newCount > value.length) expandCapacity(newCount)    compat.Platform.arraycopy(x, offset, value, count, len)    count = newCount    this  }  /** <p>   *    Appends the string representation of the <code>Boolean</code>    *    argument to the sequence.   *  </p>   *  <p>   *    The argument is converted to a string as if by the method    *    <code>String.valueOf</code>, and the characters of that    *   string are then appended to this sequence.    *  </p>   *   *   @param  x  a <code>Boolean</code>.   *   @return    a reference to this object.   */  def append(x: Boolean): StringBuilder = {    if (x) {      val newCount = count + 4      if (newCount > value.length) expandCapacity(newCount)      value(count) = 't'; count += 1      value(count) = 'r'; count += 1      value(count) = 'u'; count += 1      value(count) = 'e'; count += 1    } else {      val newCount = count + 5      if (newCount > value.length) expandCapacity(newCount)      value(count) = 'f'; count += 1      value(count) = 'a'; count += 1      value(count) = 'l'; count += 1      value(count) = 's'; count += 1      value(count) = 'e'; count += 1    }    this  }  def append(x: Byte): StringBuilder =    append(String.valueOf(x))  def append(x: Char): StringBuilder = {    val newCount = count + 1    if (newCount > value.length) expandCapacity(newCount)    value(count) = x; count += 1    this  }  def append(x: Short): StringBuilder =    append(String.valueOf(x))  def append(x: Int): StringBuilder =    append(String.valueOf(x))  def append(x: Long): StringBuilder =    append(String.valueOf(x))  def append(x: Float): StringBuilder =    append(String.valueOf(x))  def append(x: Double): StringBuilder =     append(String.valueOf(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>String.valueOf</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, String.valueOf(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> sequence    *  argument into this sequence.   *   *  @param  at  the offset position.   *  @param  x   a character sequence.   *  @return     a reference to this object.   *  @throws StringIndexOutOfBoundsException  if the offset is invalid.   */  def insert(at: Int, x: Seq[Char]): StringBuilder =    insert(at, x.toArray)  /** 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  }  /** <p>   *    Inserts the string representation of the <code>Boolean</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>Boolean</code> value.   *  @return     a reference to this object.   */  def insert(at: Int, x: Boolean): StringBuilder =    insert(at, String.valueOf(x))  /** <p>   *    Inserts the string representation of the <code>Byte</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>Byte</code> value.   *  @return     a reference to this object.   */  def insert(at: Int, x: Byte): StringBuilder =    insert(at, String.valueOf(x))  /** <p>   *    Inserts the string representation of the <code>Char</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>Char</code> value.   *  @return     a reference to this object.   */  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  }  /** <p>   *    Inserts the string representation of the <code>Short</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>Short</code> value.   *  @return     a reference to this object.   */  def insert(at: Int, x: Short): StringBuilder =    insert(at, String.valueOf(x))  /** <p>   *    Inserts the string representation of the <code>Int</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>Int</code> value.   *  @return     a reference to this object.   */  def insert(at: Int, x: Int): StringBuilder =    insert(at, String.valueOf(x))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?