⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringbuilder.scala

📁 JAVA 语言的函数式编程扩展
💻 SCALA
📖 第 1 页 / 共 2 页
字号:
/*                     __                                               *\**     ________ ___   / /  ___     Scala API                            ****    / __/ __// _ | / /  / _ |    (c) 2006-2008, LAMP/EPFL             ****  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **** /____/\___/_/ |_/____/_/ | |                                         ****                          |/                                          **\*                                                                      */// $Id: StringBuilder.scala 14498 2008-04-04 12:12:27Z washburn $package scalaimport Predef._/** <p> *    A mutable sequence of characters.  This class provides an API compatible *    with <code>java.lang.StringBuilder</code>, but with no guarantee of *    synchronization. *  </p> * *  @author Stephane Micheloud *  @version 1.0 */final class StringBuilder(initCapacity: Int, private val initValue: String)extends (Int => Char) with Proxy {  if (initCapacity < 0) throw new IllegalArgumentException  if (initValue eq null) throw new NullPointerException  /** The value is used for character storage. */  private var value = new Array[Char](initCapacity + initValue.length)  /** The count is the number of characters used. */  private var count: Int = 0  /** Constructs a string builder with no characters in it and an    *  initial capacity of 16 characters.   */  def this() = this(16, "")  /** Constructs a string builder with no characters in it and an    *  initial capacity specified by the <code>capacity</code> argument.    *   *  @param  capacity  the initial capacity.   *  @throws NegativeArraySizeException  if the <code>capacity</code>   *                    argument is less than <code>0</code>.   */  def this(capacity: Int) = this(capacity, "")  def this(str: String) = this(16, str)  append(initValue)  def self = this  def toArray: Array[Char] = value  def length: Int = count  def length_=(n: Int) { setLength(n) }  /** Sets the length of the character sequence.   *   *  @param  newLength  the new length   *  @throws IndexOutOfBoundsException  if the <code>n</code> argument is negative.   */  def setLength(n: Int) {    if (n < 0)      throw new StringIndexOutOfBoundsException//(n)    if (n > value.length) expandCapacity(n)    if (count < n)      while (count < n) {        value(count) = '\0'; count += 1      }    else      count = n  }  /** Returns the current capacity. The capacity is the amount of storage    *  available for newly inserted characters, beyond which an allocation    *  will occur.   *   *  @return  the current capacity   */  def capacity: Int = value.length  /** Same as <code>ensureCapacity</code>. */  def capacity_=(n: Int) { ensureCapacity(n) }  /** <p>   *    Ensures that the capacity is at least equal to the specified minimum.   *    If the current capacity is less than the argument, then a new internal   *    array is allocated with greater capacity. The new capacity is the larger of:    *  </p>   *  <ul>   *    <li>The <code>n</code> argument.    *    <li>Twice the old capacity, plus <code>2</code>.    *  </ul>   *  <p>   *    If the <code>n</code> argument is non-positive, this   *    method takes no action and simply returns.   *  </p>   *   *  @param n the minimum desired capacity.   */  def ensureCapacity(n: Int) {    if (n > value.length) expandCapacity(n)  }  private def expandCapacity(n: Int) {    val newCapacity = (value.length + 1) * 2    value = StringBuilder.copyOf(      value,      if (newCapacity < 0) Math.MAX_INT else if (n > newCapacity) n else newCapacity    )  }  /** <p>   *    Returns the <code>Char</code> value in this sequence at the specified index.   *    The first <code>Char</code> value is at index <code>0</code>, the next at index   *    <code>1</code>, and so on, as in array indexing.   *  </p>   *  <p>   *    The index argument must be greater than or equal to   *    <code>0</code>, and less than the length of this sequence.   *  </p>   *   *  @param  index   the index of the desired <code>Char</code> value.   *  @return         the <code>Char</code> value at the specified index.   *  @throws IndexOutOfBoundsException  if <code>index</code> is    *                  negative or greater than or equal to <code>length()</code>.   */  def charAt(index: Int): Char = {    if (index < 0 || index >= count)      throw new StringIndexOutOfBoundsException//(index)    value(index)  }  /** Same as <code>charAt</code>. */  def apply(i: Int): Char = charAt(i)  /** <p>   *    Removes the <code>Char</code> at the specified position in this   *    sequence. This sequence is shortened by one <code>Char</code>.   *  </p>   *   *  @param  index  Index of <code>Char</code> to remove   *  @return        This object.   *  @throws StringIndexOutOfBoundsException  if the <code>index</code>   *	             is negative or greater than or equal to <code>length()</code>.   */  def deleteCharAt(index: Int): StringBuilder = {    if (index < 0 || index >= count)      throw new StringIndexOutOfBoundsException//(index)    compat.Platform.arraycopy(value, index + 1, value, index, count - index - 1)    count -= 1    this  }  /** <p>   *    The character at the specified index is set to <code>ch</code>. This    *    sequence is altered to represent a new character sequence that is    *    identical to the old character sequence, except that it contains the    *    character <code>ch</code> at position <code>index</code>.    *  </p>   *  <p>   *    The index argument must be greater than or equal to    *    <code>0</code>, and less than the length of this sequence.    *  </p>   *   *  @param  index   the index of the character to modify.   *  @param  ch      the new character.   *  @throws IndexOutOfBoundsException  if <code>index</code> is    *                  negative or greater than or equal to <code>length()</code>.   */  def setCharAt(index: Int, ch: Char) {    if (index < 0 || index >= count)      throw new StringIndexOutOfBoundsException//(index)    value(index) = ch  }  /** Same as <code>setCharAt</code>. */  def update(i: Int, c: Char) { setCharAt(i, c) }  /** Returns a new <code>String</code> that contains a subsequence of   *  characters currently contained in this character sequence. The    *  substring begins at the specified index and extends to the end of   *  this sequence.   *    *  @param  start  The beginning index, inclusive.   *  @return        The new string.   *  @throws StringIndexOutOfBoundsException  if <code>start</code> is   *                 less than zero, or greater than the length of this object.   */  def substring(start: Int): String = substring(start, count)  /** Returns a new <code>String</code> that contains a subsequence of   *  characters currently contained in this sequence. The    *  substring begins at the specified <code>start</code> and    *  extends to the character at index <code>end - 1</code>.   *   *  @param  start  The beginning index, inclusive.   *  @param  end    The ending index, exclusive.   *  @return The new string.   *  @throws StringIndexOutOfBoundsException  if <code>start</code>   *                 or <code>end</code> are negative or greater than   *		     <code>length()</code>, or <code>start</code> is   *		     greater than <code>end</code>.   */  def substring(start: Int, end: Int): String = {    if (start < 0)      throw new StringIndexOutOfBoundsException//(start)    if (end > count)      throw new StringIndexOutOfBoundsException//(end)    if (start > end)      throw new StringIndexOutOfBoundsException//(end - start)    new String(value, start, end - start)  }  /** <p>   *    Appends the string representation of the <code>Any</code>    *    argument.   *  </p>   *  <p>   *    The argument is converted to a string as if by the method    *    <code>System.Convert.ToString</code>, and the characters of   *    that string are then appended to this sequence.   *  </p>   *   *  @param  x   an <code>Any</code> object.   *  @return     a reference to this object.   */  def append(x: Any): StringBuilder =    append(System.Convert.ToString(x))  /** Appends the specified string to this character sequence.   *   *  @param  s   a string.   *  @return     a reference to this object.   */  def append(s: String): StringBuilder = {    val str = if (s == null) "null" else s    val len = str.length    if (len > 0) {      val newCount = count + len      if (newCount > value.length) expandCapacity(newCount)      compat.Platform.arraycopy(str.ToCharArray, 0, value, count, len)      count = newCount    }    this  }  /** Appends the specified string builder to this sequence.   *   *  @param sb   *  @return    */  def append(sb: StringBuilder): StringBuilder =    if (sb == null)      append("null")    else {      val len = sb.length      val newCount = count + len      if (newCount > value.length) expandCapacity(newCount)      compat.Platform.arraycopy(sb.toArray, 0, value, count, len)      count = newCount      this    }  /** <p>   *    Appends the string representation of the <code>Char</code> array    *    argument to this sequence.   *  </p>   *  <p>   *    The characters of the array argument are appended, in order, to    *    the contents of this sequence. The length of this sequence   *    increases by the length of the argument.   *  </p>   *   *  @param  x  the characters to be appended.   *  @return    a reference to this object.   */  def append(x: Array[Char]): StringBuilder =    append(x, 0, x.length)  /** <p>   *    Appends the string representation of a subarray of the   *    <code>char</code> array argument to this sequence.   *  </p>   *  <p>   *    Characters of the <code>Char</code> array <code>x</code>, starting at   *    index <code>offset</code>, are appended, in order, to the contents   *    of this sequence. The length of this sequence increases   *    by the value of <code>len</code>.   *  </p>   *   *  @param  x      the characters to be appended.   *  @param  offset the index of the first <code>Char</code> to append.   *  @param  len    the number of <code>Char</code>s to append.   *  @return        a reference to this object.   */  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>System.Convert.ToString</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: Char): StringBuilder = {    val newCount = count + 1    if (newCount > value.length) expandCapacity(newCount)    value(count) = x; count += 1    this  }  def append(x: Int): StringBuilder =    append(System.Convert.ToString(x))  def append(x: Long): StringBuilder =    append(System.Convert.ToString(x))

⌨️ 快捷键说明

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