From fed453fdea05909f588e56ddad9016bb84c1c3de Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 13 Jul 2023 11:52:14 +0000 Subject: [PATCH] Provide Common StringBuilder.append/insert with primitive type arguments #KT-57359 Merge-request: KT-MR-10892 Merged-by: Abduqodiri Qurbonzoda --- .../kotlin/kotlin/text/StringBuilderNative.kt | 12 +- libraries/stdlib/api/js/kotlin.text.kt | 60 +++++++ .../js/src/kotlin/text/StringBuilderJs.kt | 150 ++++++++++++++++++ .../jvm/src/kotlin/text/StringBuilderJVM.kt | 12 +- .../src/kotlin/text/StringBuilder.kt | 126 +++++++++++++-- .../stdlib/src/kotlin/text/StringBuilder.kt | 144 +++++++++++++++++ .../wasm/src/kotlin/text/StringBuilderWasm.kt | 30 ++++ 7 files changed, 508 insertions(+), 26 deletions(-) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/text/StringBuilderNative.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/text/StringBuilderNative.kt index 9ce74a59001..95a6c9586e9 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/text/StringBuilderNative.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/text/StringBuilderNative.kt @@ -10,32 +10,32 @@ import kotlin.native.internal.GCUnsafeCall /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Byte): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Byte): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Short): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Short): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Int): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Int): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Long): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Long): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Float): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Float): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Double): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Double): StringBuilder = append(value).appendLine() @DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.6") diff --git a/libraries/stdlib/api/js/kotlin.text.kt b/libraries/stdlib/api/js/kotlin.text.kt index e98ea30aa5c..99df0794897 100644 --- a/libraries/stdlib/api/js/kotlin.text.kt +++ b/libraries/stdlib/api/js/kotlin.text.kt @@ -74,6 +74,10 @@ public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Any?): kotl @kotlin.internal.InlineOnly public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Boolean): kotlin.text.StringBuilder +@kotlin.SinceKotlin(version = "1.9") +@kotlin.internal.InlineOnly +public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Byte): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.4") @kotlin.internal.InlineOnly public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Char): kotlin.text.StringBuilder @@ -86,6 +90,26 @@ public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.CharArray): @kotlin.internal.InlineOnly public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.CharSequence?): kotlin.text.StringBuilder +@kotlin.SinceKotlin(version = "1.9") +@kotlin.internal.InlineOnly +public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Double): kotlin.text.StringBuilder + +@kotlin.SinceKotlin(version = "1.9") +@kotlin.internal.InlineOnly +public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Float): kotlin.text.StringBuilder + +@kotlin.SinceKotlin(version = "1.9") +@kotlin.internal.InlineOnly +public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Int): kotlin.text.StringBuilder + +@kotlin.SinceKotlin(version = "1.9") +@kotlin.internal.InlineOnly +public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Long): kotlin.text.StringBuilder + +@kotlin.SinceKotlin(version = "1.9") +@kotlin.internal.InlineOnly +public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.Short): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.4") @kotlin.internal.InlineOnly public inline fun kotlin.text.StringBuilder.appendLine(value: kotlin.String?): kotlin.text.StringBuilder @@ -1665,6 +1689,9 @@ public final class StringBuilder : kotlin.text.Appendable, kotlin.CharSequence { @kotlin.SinceKotlin(version = "1.3") public final fun append(value: kotlin.Boolean): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.9") + public final fun append(value: kotlin.Byte): kotlin.text.StringBuilder + public open override fun append(value: kotlin.Char): kotlin.text.StringBuilder @kotlin.SinceKotlin(version = "1.4") @@ -1675,6 +1702,21 @@ public final class StringBuilder : kotlin.text.Appendable, kotlin.CharSequence { public open override fun append(value: kotlin.CharSequence?, startIndex: kotlin.Int, endIndex: kotlin.Int): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.9") + public final fun append(value: kotlin.Double): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun append(value: kotlin.Float): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun append(value: kotlin.Int): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun append(value: kotlin.Long): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun append(value: kotlin.Short): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.3") public final fun append(value: kotlin.String?): kotlin.text.StringBuilder @@ -1723,6 +1765,9 @@ public final class StringBuilder : kotlin.text.Appendable, kotlin.CharSequence { @kotlin.WasExperimental(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final fun insert(index: kotlin.Int, value: kotlin.Boolean): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.9") + public final fun insert(index: kotlin.Int, value: kotlin.Byte): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.4") @kotlin.WasExperimental(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final fun insert(index: kotlin.Int, value: kotlin.Char): kotlin.text.StringBuilder @@ -1735,6 +1780,21 @@ public final class StringBuilder : kotlin.text.Appendable, kotlin.CharSequence { @kotlin.WasExperimental(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final fun insert(index: kotlin.Int, value: kotlin.CharSequence?): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.9") + public final fun insert(index: kotlin.Int, value: kotlin.Double): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun insert(index: kotlin.Int, value: kotlin.Float): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun insert(index: kotlin.Int, value: kotlin.Int): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun insert(index: kotlin.Int, value: kotlin.Long): kotlin.text.StringBuilder + + @kotlin.SinceKotlin(version = "1.9") + public final fun insert(index: kotlin.Int, value: kotlin.Short): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.4") @kotlin.WasExperimental(markerClass = {kotlin.ExperimentalStdlibApi::class}) public final fun insert(index: kotlin.Int, value: kotlin.String?): kotlin.text.StringBuilder diff --git a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt index f680fc6220a..4b553b79cfb 100644 --- a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt +++ b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt @@ -100,6 +100,60 @@ public actual class StringBuilder actual constructor(content: String) : Appendab return this } + /** + * Appends the string representation of the specified byte [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + actual fun append(value: Byte): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified short [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + actual fun append(value: Short): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified int [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + actual fun append(value: Int): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified long [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + actual fun append(value: Long): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified float [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + actual fun append(value: Float): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified double [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + actual fun append(value: Double): StringBuilder = append(value.toString()) + /** * Appends characters in the specified character array [value] to this string builder and returns this instance. * @@ -208,6 +262,72 @@ public actual class StringBuilder actual constructor(content: String) : Appendab return this } + /** + * Inserts the string representation of the specified byte [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + actual fun insert(index: Int, value: Byte): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified short [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + actual fun insert(index: Int, value: Short): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified int [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + actual fun insert(index: Int, value: Int): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified long [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + actual fun insert(index: Int, value: Long): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified float [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + actual fun insert(index: Int, value: Float): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified double [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + actual fun insert(index: Int, value: Double): StringBuilder = insert(index, value.toString()) + /** * Inserts the specified character [value] into this string builder at the specified [index] and returns this instance. * @@ -704,3 +824,33 @@ public actual inline fun StringBuilder.insertRange(index: Int, value: CharArray, @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE") public actual inline fun StringBuilder.insertRange(index: Int, value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder = this.insertRange(index, value, startIndex, endIndex) + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Int): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Short): StringBuilder = append(value.toInt()).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Byte): StringBuilder = append(value.toInt()).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Long): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Float): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Double): StringBuilder = append(value).appendLine() \ No newline at end of file diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt index 25668bc7be6..fd6f6716b5d 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt @@ -169,32 +169,32 @@ public inline fun StringBuilder.appendLine(value: StringBuilder?): StringBuilder /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Int): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Int): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Short): StringBuilder = append(value.toInt()).appendLine() +public actual inline fun StringBuilder.appendLine(value: Short): StringBuilder = append(value.toInt()).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Byte): StringBuilder = append(value.toInt()).appendLine() +public actual inline fun StringBuilder.appendLine(value: Byte): StringBuilder = append(value.toInt()).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Long): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Long): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Float): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Float): StringBuilder = append(value).appendLine() /** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ @SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun StringBuilder.appendLine(value: Double): StringBuilder = append(value).appendLine() +public actual inline fun StringBuilder.appendLine(value: Double): StringBuilder = append(value).appendLine() private object SystemProperties { diff --git a/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt index 4d9e22710de..8f31f97b712 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt @@ -142,24 +142,67 @@ actual class StringBuilder private constructor ( */ actual fun append(value: Any?): StringBuilder = append(value.toString()) + // TODO: optimize the append overloads with primitive value! + /** * Appends the string representation of the specified boolean [value] to this string builder and returns this instance. * * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, * and then that string was appended to this string builder. */ - // TODO: optimize those! actual fun append(value: Boolean): StringBuilder = append(value.toString()) - fun append(value: Byte): StringBuilder = append(value.toString()) - fun append(value: Short): StringBuilder = append(value.toString()) - fun append(value: Int): StringBuilder { + + /** + * Appends the string representation of the specified byte [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + actual fun append(value: Byte): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified short [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + actual fun append(value: Short): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified int [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + actual fun append(value: Int): StringBuilder { ensureExtraCapacity(11) _length += insertInt(array, _length, value) return this } - fun append(value: Long): StringBuilder = append(value.toString()) - fun append(value: Float): StringBuilder = append(value.toString()) - fun append(value: Double): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified long [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + actual fun append(value: Long): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified float [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + actual fun append(value: Float): StringBuilder = append(value.toString()) + + /** + * Appends the string representation of the specified double [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + actual fun append(value: Double): StringBuilder = append(value.toString()) /** * Appends characters in the specified character array [value] to this string builder and returns this instance. @@ -253,6 +296,8 @@ actual class StringBuilder private constructor ( return (this as CharSequence).lastIndexOf(string, startIndex, ignoreCase = false) } + // TODO: optimize the insert overloads with primitive value! + /** * Inserts the string representation of the specified boolean [value] into this string builder at the specified [index] and returns this instance. * @@ -261,14 +306,67 @@ actual class StringBuilder private constructor ( * * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. */ - // TODO: optimize those! actual fun insert(index: Int, value: Boolean): StringBuilder = insert(index, value.toString()) - fun insert(index: Int, value: Byte) = insert(index, value.toString()) - fun insert(index: Int, value: Short) = insert(index, value.toString()) - fun insert(index: Int, value: Int) = insert(index, value.toString()) - fun insert(index: Int, value: Long) = insert(index, value.toString()) - fun insert(index: Int, value: Float) = insert(index, value.toString()) - fun insert(index: Int, value: Double) = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified byte [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + actual fun insert(index: Int, value: Byte): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified short [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + actual fun insert(index: Int, value: Short): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified int [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + actual fun insert(index: Int, value: Int): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified long [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + actual fun insert(index: Int, value: Long): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified float [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + actual fun insert(index: Int, value: Float): StringBuilder = insert(index, value.toString()) + + /** + * Inserts the string representation of the specified double [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + actual fun insert(index: Int, value: Double): StringBuilder = insert(index, value.toString()) /** * Inserts the specified character [value] into this string builder at the specified [index] and returns this instance. diff --git a/libraries/stdlib/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/src/kotlin/text/StringBuilder.kt index 1484036c39f..be3c6735c79 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilder.kt @@ -69,6 +69,60 @@ expect class StringBuilder : Appendable, CharSequence { @SinceKotlin("1.3") fun append(value: Boolean): StringBuilder + /** + * Appends the string representation of the specified byte [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + fun append(value: Byte): StringBuilder + + /** + * Appends the string representation of the specified short [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + fun append(value: Short): StringBuilder + + /** + * Appends the string representation of the specified int [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + fun append(value: Int): StringBuilder + + /** + * Appends the string representation of the specified long [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + fun append(value: Long): StringBuilder + + /** + * Appends the string representation of the specified float [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + fun append(value: Float): StringBuilder + + /** + * Appends the string representation of the specified double [value] to this string builder and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was appended to this string builder. + */ + @SinceKotlin("1.9") + fun append(value: Double): StringBuilder + /** * Appends characters in the specified character array [value] to this string builder and returns this instance. * @@ -157,6 +211,72 @@ expect class StringBuilder : Appendable, CharSequence { @WasExperimental(ExperimentalStdlibApi::class) fun insert(index: Int, value: Boolean): StringBuilder + /** + * Inserts the string representation of the specified byte [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + fun insert(index: Int, value: Byte): StringBuilder + + /** + * Inserts the string representation of the specified short [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + fun insert(index: Int, value: Short): StringBuilder + + /** + * Inserts the string representation of the specified int [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + fun insert(index: Int, value: Int): StringBuilder + + /** + * Inserts the string representation of the specified long [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + fun insert(index: Int, value: Long): StringBuilder + + /** + * Inserts the string representation of the specified float [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + fun insert(index: Int, value: Float): StringBuilder + + /** + * Inserts the string representation of the specified double [value] into this string builder at the specified [index] and returns this instance. + * + * The overall effect is exactly as if the [value] were converted to a string by the `value.toString()` method, + * and then that string was inserted into this string builder at the specified [index]. + * + * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. + */ + @SinceKotlin("1.9") + fun insert(index: Int, value: Double): StringBuilder + /** * Inserts the specified character [value] into this string builder at the specified [index] and returns this instance. * @@ -477,3 +597,27 @@ public inline fun StringBuilder.appendLine(value: Char): StringBuilder = append( @SinceKotlin("1.4") @kotlin.internal.InlineOnly public inline fun StringBuilder.appendLine(value: Boolean): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +public expect fun StringBuilder.appendLine(value: Byte): StringBuilder + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +public expect fun StringBuilder.appendLine(value: Short): StringBuilder + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +public expect fun StringBuilder.appendLine(value: Int): StringBuilder + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +public expect fun StringBuilder.appendLine(value: Long): StringBuilder + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +public expect fun StringBuilder.appendLine(value: Float): StringBuilder + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +public expect fun StringBuilder.appendLine(value: Double): StringBuilder \ No newline at end of file diff --git a/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt b/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt index a958c16a7ac..59a61428d66 100644 --- a/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt +++ b/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt @@ -7,6 +7,36 @@ package kotlin.text import kotlin.wasm.internal.* +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Byte): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Short): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Int): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Long): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Float): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.appendLine(value: Double): StringBuilder = append(value).appendLine() + internal fun insertString(array: CharArray, destinationIndex: Int, value: String, sourceIndex: Int, count: Int): Int { copyWasmArray(value.chars, array.storage, sourceIndex, destinationIndex, count) return count