Skip to content

Commit bb397b6

Browse files
Abduqodiri Qurbonzodaminamoto79
authored andcommitted
Promote common StringBuilder to stable
(cherry picked from commit 920e48217c320a35e246b827109d214693d954b5)
1 parent 36538cf commit bb397b6

File tree

1 file changed

+64
-69
lines changed

1 file changed

+64
-69
lines changed

runtime/src/main/kotlin/kotlin/text/StringBuilder.kt

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ actual class StringBuilder private constructor (
5858
return append(toAppend, 0, toAppend.length)
5959
}
6060

61-
@OptIn(ExperimentalStdlibApi::class)
62-
actual override fun append(value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder = this.appendRange(value, startIndex, endIndex)
61+
actual override fun append(value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder =
62+
this.appendRange(value ?: "null", startIndex, endIndex)
6363

6464
/**
6565
* Reverses the contents of this string builder and returns this instance.
@@ -212,8 +212,8 @@ actual class StringBuilder private constructor (
212212
*
213213
* Returns `-1` if the specified [string] does not occur in this string builder.
214214
*/
215-
@SinceKotlin("1.3")
216-
@ExperimentalStdlibApi
215+
@SinceKotlin("1.4")
216+
@WasExperimental(ExperimentalStdlibApi::class)
217217
actual fun indexOf(string: String): Int {
218218
return (this as CharSequence).indexOf(string, startIndex = 0, ignoreCase = false)
219219
}
@@ -224,8 +224,8 @@ actual class StringBuilder private constructor (
224224
*
225225
* Returns `-1` if the specified [string] does not occur in this string builder starting at the specified [startIndex].
226226
*/
227-
@SinceKotlin("1.3")
228-
@ExperimentalStdlibApi
227+
@SinceKotlin("1.4")
228+
@WasExperimental(ExperimentalStdlibApi::class)
229229
actual fun indexOf(string: String, startIndex: Int): Int {
230230
if (string.isEmpty() && startIndex >= _length) return _length
231231
return (this as CharSequence).indexOf(string, startIndex, ignoreCase = false)
@@ -237,8 +237,8 @@ actual class StringBuilder private constructor (
237237
*
238238
* Returns `-1` if the specified [string] does not occur in this string builder.
239239
*/
240-
@SinceKotlin("1.3")
241-
@ExperimentalStdlibApi
240+
@SinceKotlin("1.4")
241+
@WasExperimental(ExperimentalStdlibApi::class)
242242
actual fun lastIndexOf(string: String): Int {
243243
if (string.isEmpty()) return _length
244244
return (this as CharSequence).lastIndexOf(string, startIndex = lastIndex, ignoreCase = false)
@@ -250,8 +250,8 @@ actual class StringBuilder private constructor (
250250
*
251251
* Returns `-1` if the specified [string] does not occur in this string builder starting at the specified [startIndex].
252252
*/
253-
@SinceKotlin("1.3")
254-
@ExperimentalStdlibApi
253+
@SinceKotlin("1.4")
254+
@WasExperimental(ExperimentalStdlibApi::class)
255255
actual fun lastIndexOf(string: String, startIndex: Int): Int {
256256
if (string.isEmpty() && startIndex >= _length) return _length
257257
return (this as CharSequence).lastIndexOf(string, startIndex, ignoreCase = false)
@@ -319,7 +319,6 @@ actual class StringBuilder private constructor (
319319
*
320320
* @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
321321
*/
322-
@OptIn(ExperimentalStdlibApi::class)
323322
actual fun insert(index: Int, value: CharSequence?): StringBuilder {
324323
// Kotlin/JVM inserts the "null" string if the argument is null.
325324
val toInsert = value ?: "null"
@@ -386,8 +385,8 @@ actual class StringBuilder private constructor (
386385
*
387386
* @throws IndexOutOfBoundsException if [startIndex] is less than zero or greater than the length of this string builder.
388387
*/
389-
@SinceKotlin("1.3")
390-
@ExperimentalStdlibApi
388+
@SinceKotlin("1.4")
389+
@WasExperimental(ExperimentalStdlibApi::class)
391390
actual fun substring(startIndex: Int): String {
392391
return substring(startIndex, _length)
393392
}
@@ -425,8 +424,8 @@ actual class StringBuilder private constructor (
425424
*
426425
* @throws IndexOutOfBoundsException or [IllegalArgumentException] if [startIndex] is less than zero, greater than the length of this string builder, or `startIndex > endIndex`.
427426
*/
428-
@SinceKotlin("1.3")
429-
@ExperimentalStdlibApi
427+
@SinceKotlin("1.4")
428+
@WasExperimental(ExperimentalStdlibApi::class)
430429
fun setRange(startIndex: Int, endIndex: Int, value: String): StringBuilder {
431430
checkReplaceRange(startIndex, endIndex, _length)
432431

@@ -450,8 +449,8 @@ actual class StringBuilder private constructor (
450449
*
451450
* @throws IndexOutOfBoundsException if [index] is out of bounds of this string builder.
452451
*/
453-
@SinceKotlin("1.3")
454-
@ExperimentalStdlibApi
452+
@SinceKotlin("1.4")
453+
@WasExperimental(ExperimentalStdlibApi::class)
455454
fun deleteAt(index: Int): StringBuilder {
456455
checkIndex(index)
457456
array.copyInto(array, startIndex = index + 1, endIndex = _length, destinationOffset = index)
@@ -467,8 +466,8 @@ actual class StringBuilder private constructor (
467466
*
468467
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] is out of range of this string builder indices or when `startIndex > endIndex`.
469468
*/
470-
@SinceKotlin("1.3")
471-
@ExperimentalStdlibApi
469+
@SinceKotlin("1.4")
470+
@WasExperimental(ExperimentalStdlibApi::class)
472471
fun deleteRange(startIndex: Int, endIndex: Int): StringBuilder {
473472
checkReplaceRange(startIndex, endIndex, _length)
474473

@@ -490,8 +489,8 @@ actual class StringBuilder private constructor (
490489
* @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationOffset],
491490
* or when that index is out of the [destination] array indices range.
492491
*/
493-
@SinceKotlin("1.3")
494-
@ExperimentalStdlibApi
492+
@SinceKotlin("1.4")
493+
@WasExperimental(ExperimentalStdlibApi::class)
495494
fun toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = this.length) {
496495
checkBoundsIndexes(startIndex, endIndex, _length)
497496
checkBoundsIndexes(destinationOffset, destinationOffset + endIndex - startIndex, destination.size)
@@ -510,8 +509,8 @@ actual class StringBuilder private constructor (
510509
*
511510
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] array indices or when `startIndex > endIndex`.
512511
*/
513-
@SinceKotlin("1.3")
514-
@ExperimentalStdlibApi
512+
@SinceKotlin("1.4")
513+
@WasExperimental(ExperimentalStdlibApi::class)
515514
fun appendRange(value: CharArray, startIndex: Int, endIndex: Int): StringBuilder {
516515
checkBoundsIndexes(startIndex, endIndex, value.size)
517516
val extraLength = endIndex - startIndex
@@ -524,28 +523,25 @@ actual class StringBuilder private constructor (
524523
/**
525524
* Appends a subsequence of the specified character sequence [value] to this string builder and returns this instance.
526525
*
527-
* @param value the character sequence from which a subsequence is appended. If [value] is `null`,
528-
* then characters are appended as if [value] contained the four characters `"null"`.
526+
* @param value the character sequence from which a subsequence is appended.
529527
* @param startIndex the beginning (inclusive) of the subsequence to append.
530528
* @param endIndex the end (exclusive) of the subsequence to append.
531529
*
532530
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] character sequence indices or when `startIndex > endIndex`.
533531
*/
534-
@SinceKotlin("1.3")
535-
@ExperimentalStdlibApi
536-
fun appendRange(value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder {
537-
// Kotlin/JVM processes null as if the argument was "null" char sequence.
538-
val toAppend = value ?: "null"
539-
checkBoundsIndexes(startIndex, endIndex, toAppend.length)
532+
@SinceKotlin("1.4")
533+
@WasExperimental(ExperimentalStdlibApi::class)
534+
fun appendRange(value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder {
535+
checkBoundsIndexes(startIndex, endIndex, value.length)
540536
val extraLength = endIndex - startIndex
541537
ensureExtraCapacity(extraLength)
542-
(toAppend as? String)?.let {
538+
(value as? String)?.let {
543539
_length += insertString(array, _length, it, startIndex, extraLength)
544540
return this
545541
}
546542
var index = startIndex
547543
while (index < endIndex)
548-
array[_length++] = toAppend[index++]
544+
array[_length++] = value[index++]
549545
return this
550546
}
551547

@@ -555,20 +551,17 @@ actual class StringBuilder private constructor (
555551
* The inserted characters go in the same order as in the [value] character sequence, starting at [index].
556552
*
557553
* @param index the position in this string builder to insert at.
558-
* @param value the character sequence from which a subsequence is inserted. If [value] is `null`,
559-
* then characters will be inserted as if [value] contained the four characters `"null"`.
554+
* @param value the character sequence from which a subsequence is inserted.
560555
* @param startIndex the beginning (inclusive) of the subsequence to insert.
561556
* @param endIndex the end (exclusive) of the subsequence to insert.
562557
*
563558
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] character sequence indices or when `startIndex > endIndex`.
564559
* @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
565560
*/
566-
@SinceKotlin("1.3")
567-
@ExperimentalStdlibApi
568-
fun insertRange(index: Int, value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder {
569-
// Kotlin/JVM processes null as if the argument was "null" char sequence.
570-
val toInsert = value ?: "null"
571-
checkBoundsIndexes(startIndex, endIndex, toInsert.length)
561+
@SinceKotlin("1.4")
562+
@WasExperimental(ExperimentalStdlibApi::class)
563+
fun insertRange(index: Int, value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder {
564+
checkBoundsIndexes(startIndex, endIndex, value.length)
572565
checkInsertIndex(index)
573566
val extraLength = endIndex - startIndex
574567
ensureExtraCapacity(extraLength)
@@ -577,7 +570,7 @@ actual class StringBuilder private constructor (
577570
var from = startIndex
578571
var to = index
579572
while (from < endIndex) {
580-
array[to++] = toInsert[from++]
573+
array[to++] = value[from++]
581574
}
582575

583576
_length += extraLength
@@ -597,8 +590,8 @@ actual class StringBuilder private constructor (
597590
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] array indices or when `startIndex > endIndex`.
598591
* @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
599592
*/
600-
@SinceKotlin("1.3")
601-
@ExperimentalStdlibApi
593+
@SinceKotlin("1.4")
594+
@WasExperimental(ExperimentalStdlibApi::class)
602595
fun insertRange(index: Int, value: CharArray, startIndex: Int, endIndex: Int): StringBuilder {
603596
checkInsertIndex(index)
604597
checkBoundsIndexes(startIndex, endIndex, value.size)
@@ -665,8 +658,8 @@ public actual inline operator fun StringBuilder.set(index: Int, value: Char): Un
665658
*
666659
* @throws IndexOutOfBoundsException or [IllegalArgumentException] if [startIndex] is less than zero, greater than the length of this string builder, or `startIndex > endIndex`.
667660
*/
668-
@SinceKotlin("1.3")
669-
@ExperimentalStdlibApi
661+
@SinceKotlin("1.4")
662+
@WasExperimental(ExperimentalStdlibApi::class)
670663
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
671664
@kotlin.internal.InlineOnly
672665
public actual inline fun StringBuilder.setRange(startIndex: Int, endIndex: Int, value: String): StringBuilder =
@@ -681,8 +674,8 @@ public actual inline fun StringBuilder.setRange(startIndex: Int, endIndex: Int,
681674
*
682675
* @throws IndexOutOfBoundsException if [index] is out of bounds of this string builder.
683676
*/
684-
@SinceKotlin("1.3")
685-
@ExperimentalStdlibApi
677+
@SinceKotlin("1.4")
678+
@WasExperimental(ExperimentalStdlibApi::class)
686679
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
687680
@kotlin.internal.InlineOnly
688681
public actual inline fun StringBuilder.deleteAt(index: Int): StringBuilder = this.deleteAt(index)
@@ -695,8 +688,8 @@ public actual inline fun StringBuilder.deleteAt(index: Int): StringBuilder = thi
695688
*
696689
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this string builder indices or when `startIndex > endIndex`.
697690
*/
698-
@SinceKotlin("1.3")
699-
@ExperimentalStdlibApi
691+
@SinceKotlin("1.4")
692+
@WasExperimental(ExperimentalStdlibApi::class)
700693
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
701694
@kotlin.internal.InlineOnly
702695
public actual inline fun StringBuilder.deleteRange(startIndex: Int, endIndex: Int): StringBuilder = this.deleteRange(startIndex, endIndex)
@@ -713,8 +706,8 @@ public actual inline fun StringBuilder.deleteRange(startIndex: Int, endIndex: In
713706
* @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationOffset],
714707
* or when that index is out of the [destination] array indices range.
715708
*/
716-
@SinceKotlin("1.3")
717-
@ExperimentalStdlibApi
709+
@SinceKotlin("1.4")
710+
@WasExperimental(ExperimentalStdlibApi::class)
718711
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
719712
@kotlin.internal.InlineOnly
720713
public actual inline fun StringBuilder.toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = this.length) =
@@ -731,8 +724,8 @@ public actual inline fun StringBuilder.toCharArray(destination: CharArray, desti
731724
*
732725
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] array indices or when `startIndex > endIndex`.
733726
*/
734-
@SinceKotlin("1.3")
735-
@ExperimentalStdlibApi
727+
@SinceKotlin("1.4")
728+
@WasExperimental(ExperimentalStdlibApi::class)
736729
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
737730
@kotlin.internal.InlineOnly
738731
public actual inline fun StringBuilder.appendRange(value: CharArray, startIndex: Int, endIndex: Int): StringBuilder =
@@ -741,18 +734,17 @@ public actual inline fun StringBuilder.appendRange(value: CharArray, startIndex:
741734
/**
742735
* Appends a subsequence of the specified character sequence [value] to this string builder and returns this instance.
743736
*
744-
* @param value the character sequence from which a subsequence is appended. If [value] is `null`,
745-
* then characters are appended as if [value] contained the four characters `"null"`.
737+
* @param value the character sequence from which a subsequence is appended.
746738
* @param startIndex the beginning (inclusive) of the subsequence to append.
747739
* @param endIndex the end (exclusive) of the subsequence to append.
748740
*
749741
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] character sequence indices or when `startIndex > endIndex`.
750742
*/
751-
@SinceKotlin("1.3")
752-
@ExperimentalStdlibApi
743+
@SinceKotlin("1.4")
744+
@WasExperimental(ExperimentalStdlibApi::class)
753745
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
754746
@kotlin.internal.InlineOnly
755-
public actual inline fun StringBuilder.appendRange(value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder =
747+
public actual inline fun StringBuilder.appendRange(value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder =
756748
this.appendRange(value, startIndex, endIndex)
757749

758750
/**
@@ -768,8 +760,8 @@ public actual inline fun StringBuilder.appendRange(value: CharSequence?, startIn
768760
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] array indices or when `startIndex > endIndex`.
769761
* @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
770762
*/
771-
@SinceKotlin("1.3")
772-
@ExperimentalStdlibApi
763+
@SinceKotlin("1.4")
764+
@WasExperimental(ExperimentalStdlibApi::class)
773765
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
774766
@kotlin.internal.InlineOnly
775767
public actual inline fun StringBuilder.insertRange(index: Int, value: CharArray, startIndex: Int, endIndex: Int): StringBuilder =
@@ -781,19 +773,18 @@ public actual inline fun StringBuilder.insertRange(index: Int, value: CharArray,
781773
* The inserted characters go in the same order as in the [value] character sequence, starting at [index].
782774
*
783775
* @param index the position in this string builder to insert at.
784-
* @param value the character sequence from which a subsequence is inserted. If [value] is `null`,
785-
* then characters will be inserted as if [value] contained the four characters `"null"`.
776+
* @param value the character sequence from which a subsequence is inserted.
786777
* @param startIndex the beginning (inclusive) of the subsequence to insert.
787778
* @param endIndex the end (exclusive) of the subsequence to insert.
788779
*
789780
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of the [value] character sequence indices or when `startIndex > endIndex`.
790781
* @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
791782
*/
792-
@SinceKotlin("1.3")
793-
@ExperimentalStdlibApi
783+
@SinceKotlin("1.4")
784+
@WasExperimental(ExperimentalStdlibApi::class)
794785
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
795786
@kotlin.internal.InlineOnly
796-
public actual inline fun StringBuilder.insertRange(index: Int, value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder =
787+
public actual inline fun StringBuilder.insertRange(index: Int, value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder =
797788
this.insertRange(index, value, startIndex, endIndex)
798789

799790
// Method parameters renamings
@@ -887,10 +878,14 @@ public inline fun StringBuilder.setLength(l: Int) = this.setLength(newLength = l
887878
* @throws IndexOutOfBoundsException or [IllegalArgumentException] when [start] or [end] is out of range of the [csq] character sequence indices or when `start > end`.
888879
* @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
889880
*/
890-
@OptIn(ExperimentalStdlibApi::class)
881+
@Deprecated(
882+
"Use insertRange(index: Int, csq: CharSequence, start: Int, end: Int) instead",
883+
ReplaceWith("insertRange(index, csq ?: "null", start, end)"),
884+
DeprecationLevel.WARNING
885+
)
891886
@kotlin.internal.InlineOnly
892887
public inline fun StringBuilder.insert(index: Int, csq: CharSequence?, start: Int, end: Int): StringBuilder =
893-
this.insertRange(index, csq, start, end)
888+
this.insertRange(index, csq ?: "null", start, end)
894889

895890
@Deprecated("Use set(index: Int, value: Char) instead", ReplaceWith("set(index, value)"), DeprecationLevel.WARNING)
896891
@kotlin.internal.InlineOnly
@@ -905,6 +900,6 @@ public inline fun StringBuilder.setCharAt(index: Int, value: Char) = this.set(in
905900
*
906901
* @throws IndexOutOfBoundsException if [index] is out of bounds of this string builder.
907902
*/
908-
@OptIn(ExperimentalStdlibApi::class)
903+
@Deprecated("Use deleteAt(index: Int) instead", ReplaceWith("deleteAt(index)"), DeprecationLevel.WARNING)
909904
@kotlin.internal.InlineOnly
910905
public inline fun StringBuilder.deleteCharAt(index: Int) = this.deleteAt(index)

0 commit comments

Comments
 (0)