Skip to content

Commit 332ef15

Browse files
authored
Merge pull request scala#6903 from Prithvirajbilla/10991
Adds missing constructor on mutable.StringBuilder
2 parents 14304ce + 26e3e90 commit 332ef15

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

src/library/scala/collection/mutable/Builder.scala

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/junit/scala/SerializationStabilityTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object SerializationStability {
8181
}
8282
}
8383

84-
// Generated on 20180701-21:01:46 with Scala version 2.13.0-20180701-205044-6e3f96b)
84+
// Generated on 20180709-21:06:19 with Scala version 2.13.0-20180710-010238-d0a74ab)
8585
def main(args: Array[String]): Unit = {
8686
overwrite.foreach(updateComment)
8787
def g = Thread.currentThread.getStackTrace
@@ -208,7 +208,7 @@ object SerializationStability {
208208

209209
// TODO scala/bug#8576 unstable under -Xcheckinit
210210
// check(g)(mutable.ListBuffer(1, 2, 3))( "rO0ABXNyACNzY2FsYS5jb2xsZWN0aW9uLm11dGFibGUuTGlzdEJ1ZmZlci9y9I7QyWzGAwAEWgAIZXhwb3J0ZWRJAANsZW5MAAVsYXN0MHQAKUxzY2FsYS9jb2xsZWN0aW9uL2ltbXV0YWJsZS8kY29sb24kY29sb247TAAqc2NhbGEkY29sbGVjdGlvbiRtdXRhYmxlJExpc3RCdWZmZXIkJHN0YXJ0dAAhTHNjYWxhL2NvbGxlY3Rpb24vaW1tdXRhYmxlL0xpc3Q7eHBzcgARamF2YS5sYW5nLkludGVnZXIS4qCk94GHOAIAAUkABXZhbHVleHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhwAAAAAXNxAH4ABAAAAAJzcQB+AAQAAAADc3IALHNjYWxhLmNvbGxlY3Rpb24uaW1tdXRhYmxlLkxpc3RTZXJpYWxpemVFbmQkilxjW/dTC20CAAB4cHcFAAAAAAN4")
211-
check(g)(new mutable.StringBuilder(new java.lang.StringBuilder("123")))( "rO0ABXNyACZzY2FsYS5jb2xsZWN0aW9uLm11dGFibGUuU3RyaW5nQnVpbGRlcgAAAAAAAAADAgABTAACc2J0ABlMamF2YS9sYW5nL1N0cmluZ0J1aWxkZXI7eHIAJHNjYWxhLmNvbGxlY3Rpb24ubXV0YWJsZS5BYnN0cmFjdFNlcQAAAAAAAAADAgAAeHIAHHNjYWxhLmNvbGxlY3Rpb24uQWJzdHJhY3RTZXEAAAAAAAAAAwIAAHhyACFzY2FsYS5jb2xsZWN0aW9uLkFic3RyYWN0SXRlcmFibGUAAAAAAAAAAwIAAHhwc3IAF2phdmEubGFuZy5TdHJpbmdCdWlsZGVyPNX7FFpMassDAAB4cHcEAAAAA3VyAAJbQ7AmZrDiXYSsAgAAeHAAAAATADEAMgAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4")
211+
check(g)(new mutable.StringBuilder(new java.lang.StringBuilder("123")))( "rO0ABXNyACZzY2FsYS5jb2xsZWN0aW9uLm11dGFibGUuU3RyaW5nQnVpbGRlcgAAAAAAAAADAgABTAAKdW5kZXJseWluZ3QAGUxqYXZhL2xhbmcvU3RyaW5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