@@ -83,7 +83,7 @@ trait Builder[-A, +To] extends Growable[A] { self =>
8383 * section on `StringBuilders` for more information.
8484 */
8585@ SerialVersionUID (3L )
86- class StringBuilder (private val sb : java.lang.StringBuilder ) extends AbstractSeq [Char ]
86+ class StringBuilder (private val underlying : java.lang.StringBuilder ) extends AbstractSeq [Char ]
8787 with Builder [Char , String ]
8888 with IndexedSeq [Char ]
8989 with IndexedSeqOps [Char , IndexedSeq , StringBuilder ]
@@ -92,41 +92,56 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
9292
9393 def this () = this (new java.lang.StringBuilder )
9494
95- def this (length : Int ) = this (new java.lang.StringBuilder (length))
95+ /** Constructs a string builder with no characters in it and an
96+ * initial capacity specified by the `capacity` argument.
97+ *
98+ * @param capacity the initial capacity.
99+ * @throws NegativeArraySizeException if capacity < 0.
100+ */
101+ def this (capacity : Int ) = this (new java.lang.StringBuilder (capacity))
96102
97- def this (s : String ) = this (new java.lang.StringBuilder (s))
103+ /** Constructs a string builder with initial characters
104+ * equal to characters of `str`.
105+ */
106+ def this (str : String ) = this (new java.lang.StringBuilder (str))
107+
108+ /** Constructs a string builder initialized with string value `initValue`
109+ * and with additional character capacity `initCapacity`.
110+ */
111+ def this (initCapacity : Int , initValue : String ) =
112+ this (new java.lang.StringBuilder (initValue.length + initCapacity) append initValue)
98113
99114 // Methods required to make this an IndexedSeq:
100- def apply (i : Int ): Char = sb .charAt(i)
115+ def apply (i : Int ): Char = underlying .charAt(i)
101116
102117 override protected def fromSpecificIterable (coll : scala.collection.Iterable [Char ]): StringBuilder =
103118 new StringBuilder () ++= coll
104119
105120 override protected def newSpecificBuilder : Builder [Char , StringBuilder ] =
106121 new GrowableBuilder (new StringBuilder ())
107122
108- def length : Int = sb .length()
123+ def length : Int = underlying .length()
109124
110- def length_= (n : Int ): Unit = sb .setLength(n)
125+ def length_= (n : Int ): Unit = underlying .setLength(n)
111126
112- def addOne (x : Char ) = { sb .append(x); this }
127+ def addOne (x : Char ) = { underlying .append(x); this }
113128
114- def clear () = sb .setLength(0 )
129+ def clear () = underlying .setLength(0 )
115130
116131 /** Overloaded version of `addAll` that takes a string */
117- def addAll (s : String ): this .type = { sb .append(s); this }
132+ def addAll (s : String ): this .type = { underlying .append(s); this }
118133
119134 /** Alias for `addAll` */
120135 def ++= (s : String ): this .type = addAll(s)
121136
122- def result () = sb .toString
137+ def result () = underlying .toString
123138
124139 override def toString = result()
125140
126141 // append* methods delegate to the underlying java.lang.StringBuilder:
127142
128143 def appendAll (xs : String ): StringBuilder = {
129- sb append xs
144+ underlying append xs
130145 this
131146 }
132147
@@ -137,7 +152,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
137152 * @return this StringBuilder.
138153 */
139154 def append (x : Any ): StringBuilder = {
140- sb append String .valueOf(x)
155+ underlying append String .valueOf(x)
141156 this
142157 }
143158
@@ -147,7 +162,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
147162 * @return this StringBuilder.
148163 */
149164 def append (s : String ): StringBuilder = {
150- sb append s
165+ underlying append s
151166 this
152167 }
153168
@@ -157,7 +172,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
157172 * @return
158173 */
159174 def append (s : StringBuilder ): StringBuilder = {
160- sb append s.sb
175+ underlying append s.underlying
161176 this
162177 }
163178
@@ -174,7 +189,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
174189 * @return a reference to this object.
175190 */
176191 def appendAll (xs : Array [Char ]): StringBuilder = {
177- sb append xs
192+ underlying append xs
178193 this
179194 }
180195
@@ -186,7 +201,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
186201 * @return this StringBuilder.
187202 */
188203 def appendAll (xs : Array [Char ], offset : Int , len : Int ): StringBuilder = {
189- sb .append(xs, offset, len)
204+ underlying .append(xs, offset, len)
190205 this
191206 }
192207
@@ -197,14 +212,14 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
197212 * @param x a primitive value
198213 * @return This StringBuilder.
199214 */
200- def append (x : Boolean ): StringBuilder = { sb append x ; this }
215+ def append (x : Boolean ): StringBuilder = { underlying append x ; this }
201216 def append (x : Byte ): StringBuilder = append(x.toInt)
202217 def append (x : Short ): StringBuilder = append(x.toInt)
203- def append (x : Int ): StringBuilder = { sb append x ; this }
204- def append (x : Long ): StringBuilder = { sb append x ; this }
205- def append (x : Float ): StringBuilder = { sb append x ; this }
206- def append (x : Double ): StringBuilder = { sb append x ; this }
207- def append (x : Char ): StringBuilder = { sb append x ; this }
218+ def append (x : Int ): StringBuilder = { underlying append x ; this }
219+ def append (x : Long ): StringBuilder = { underlying append x ; this }
220+ def append (x : Float ): StringBuilder = { underlying append x ; this }
221+ def append (x : Double ): StringBuilder = { underlying append x ; this }
222+ def append (x : Char ): StringBuilder = { underlying append x ; this }
208223
209224 /** Remove a subsequence of Chars from this sequence, starting at the
210225 * given start index (inclusive) and extending to the end index (exclusive)
@@ -216,7 +231,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
216231 * @throws StringIndexOutOfBoundsException if start < 0 || start > end
217232 */
218233 def delete (start : Int , end : Int ): StringBuilder = {
219- sb .delete(start, end)
234+ underlying .delete(start, end)
220235 this
221236 }
222237
@@ -230,7 +245,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
230245 * @throws StringIndexOutOfBoundsException if start < 0, start > length, or start > end
231246 */
232247 def replace (start : Int , end : Int , str : String ): StringBuilder = {
233- sb .replace(start, end, str)
248+ underlying .replace(start, end, str)
234249 this
235250 }
236251
@@ -247,7 +262,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
247262 * offset < 0, len < 0, or (offset + len) > str.length.
248263 */
249264 def insertAll (index : Int , str : Array [Char ], offset : Int , len : Int ): StringBuilder = {
250- sb .insert(index, str, offset, len)
265+ underlying .insert(index, str, offset, len)
251266 this
252267 }
253268
@@ -269,7 +284,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
269284 * @throws StringIndexOutOfBoundsException if the index is out of bounds.
270285 */
271286 def insert (index : Int , x : String ): StringBuilder = {
272- sb .insert(index, x)
287+ underlying .insert(index, x)
273288 this
274289 }
275290
@@ -291,7 +306,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
291306 * @throws StringIndexOutOfBoundsException if the index is out of bounds.
292307 */
293308 def insertAll (index : Int , xs : Array [Char ]): StringBuilder = {
294- sb .insert(index, xs)
309+ underlying .insert(index, xs)
295310 this
296311 }
297312
@@ -318,9 +333,9 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
318333 * @param len the new length
319334 * @throws IndexOutOfBoundsException if the argument is negative.
320335 */
321- def setLength (len : Int ): Unit = sb .setLength(len)
336+ def setLength (len : Int ): Unit = underlying .setLength(len)
322337
323- def update (idx : Int , elem : Char ): Unit = sb .setCharAt(idx, elem)
338+ def update (idx : Int , elem : Char ): Unit = underlying .setCharAt(idx, elem)
324339
325340
326341 /** Like reverse, but destructively updates the target StringBuilder.
@@ -335,7 +350,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
335350 * @return the reversed StringBuilder (same as the target StringBuilder)
336351 */
337352 def reverseInPlace (): this .type = {
338- sb .reverse()
353+ underlying .reverse()
339354 this
340355 }
341356
@@ -345,7 +360,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
345360 *
346361 * @return the capacity
347362 */
348- def capacity : Int = sb .capacity()
363+ def capacity : Int = underlying .capacity()
349364
350365 /** Ensure that the capacity is at least the given argument.
351366 * If the argument is greater than the current capacity, new
@@ -354,15 +369,15 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
354369 *
355370 * @param newCapacity the minimum desired capacity.
356371 */
357- def ensureCapacity (newCapacity : Int ): Unit = { sb .ensureCapacity(newCapacity) }
372+ def ensureCapacity (newCapacity : Int ): Unit = { underlying .ensureCapacity(newCapacity) }
358373
359374 /** Returns the Char at the specified index, counting from 0 as in Arrays.
360375 *
361376 * @param index the index to look up
362377 * @return the Char at the given index.
363378 * @throws IndexOutOfBoundsException if the index is out of bounds.
364379 */
365- def charAt (index : Int ): Char = sb .charAt(index)
380+ def charAt (index : Int ): Char = underlying .charAt(index)
366381
367382 /** Removes the Char at the specified index. The sequence is
368383 * shortened by one.
@@ -372,7 +387,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
372387 * @throws IndexOutOfBoundsException if the index is out of bounds.
373388 */
374389 def deleteCharAt (index : Int ): this .type = {
375- sb .deleteCharAt(index)
390+ underlying .deleteCharAt(index)
376391 this
377392 }
378393
@@ -383,7 +398,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
383398 * @throws IndexOutOfBoundsException if the index is out of bounds.
384399 */
385400 def setCharAt (index : Int , ch : Char ): this .type = {
386- sb .setCharAt(index, ch)
401+ underlying .setCharAt(index, ch)
387402 this
388403 }
389404
@@ -410,7 +425,7 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
410425 * @throws StringIndexOutOfBoundsException If either index is out of bounds,
411426 * or if start > end.
412427 */
413- def substring (start : Int , end : Int ): String = sb .substring(start, end)
428+ def substring (start : Int , end : Int ): String = underlying .substring(start, end)
414429
415430 /** For implementing CharSequence.
416431 */
@@ -422,30 +437,30 @@ class StringBuilder(private val sb: java.lang.StringBuilder) extends AbstractSeq
422437 * @param str the target string to search for
423438 * @return the first applicable index where target occurs, or -1 if not found.
424439 */
425- def indexOf (str : String ): Int = sb .indexOf(str)
440+ def indexOf (str : String ): Int = underlying .indexOf(str)
426441
427442 /** Finds the index of the first occurrence of the specified substring.
428443 *
429444 * @param str the target string to search for
430445 * @param fromIndex the smallest index in the source string to consider
431446 * @return the first applicable index where target occurs, or -1 if not found.
432447 */
433- def indexOf (str : String , fromIndex : Int ): Int = sb .indexOf(str, fromIndex)
448+ def indexOf (str : String , fromIndex : Int ): Int = underlying .indexOf(str, fromIndex)
434449
435450 /** Finds the index of the last occurrence of the specified substring.
436451 *
437452 * @param str the target string to search for
438453 * @return the last applicable index where target occurs, or -1 if not found.
439454 */
440- def lastIndexOf (str : String ): Int = sb .lastIndexOf(str)
455+ def lastIndexOf (str : String ): Int = underlying .lastIndexOf(str)
441456
442457 /** Finds the index of the last occurrence of the specified substring.
443458 *
444459 * @param str the target string to search for
445460 * @param fromIndex the smallest index in the source string to consider
446461 * @return the last applicable index where target occurs, or -1 if not found.
447462 */
448- def lastIndexOf (str : String , fromIndex : Int ): Int = sb .lastIndexOf(str, fromIndex)
463+ def lastIndexOf (str : String , fromIndex : Int ): Int = underlying .lastIndexOf(str, fromIndex)
449464
450465 override protected [this ] def writeReplace (): AnyRef = this
451466}
0 commit comments