From e94b247835da6d851b18d85ada1b9b54145f4a26 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Mon, 13 Nov 2023 23:06:57 +0200 Subject: [PATCH] Introduce Common StringBuilder.append/insert(Byte/Short) extensions #KT-63341 --- libraries/stdlib/api/js/kotlin.text.kt | 12 +++++ .../js/src/kotlin/text/StringBuilderJs.kt | 44 +++++++++++++++++ .../jvm/src/kotlin/text/StringBuilderJVM.kt | 48 ++++++++++++++++++ .../src/kotlin/text/StringBuilder.kt | 49 +++++++++++++++++++ .../stdlib/src/kotlin/text/StringBuilder.kt | 40 +++++++++++++++ 5 files changed, 193 insertions(+) diff --git a/libraries/stdlib/api/js/kotlin.text.kt b/libraries/stdlib/api/js/kotlin.text.kt index 3dd5f3a0c1f..e4b9813076a 100644 --- a/libraries/stdlib/api/js/kotlin.text.kt +++ b/libraries/stdlib/api/js/kotlin.text.kt @@ -46,10 +46,16 @@ public fun kotlin.text.StringBuilder.append(vararg value: kotlin.Any?): kotlin.t public fun kotlin.text.StringBuilder.append(vararg value: kotlin.String?): kotlin.text.StringBuilder +@kotlin.SinceKotlin(version = "1.9") +public inline fun kotlin.text.StringBuilder.append(value: kotlin.Byte): kotlin.text.StringBuilder + @kotlin.Deprecated(level = DeprecationLevel.ERROR, message = "Use appendRange instead.", replaceWith = kotlin.ReplaceWith(expression = "this.appendRange(str, offset, offset + len)", imports = {})) @kotlin.internal.InlineOnly public inline fun kotlin.text.StringBuilder.append(str: kotlin.CharArray, offset: kotlin.Int, len: kotlin.Int): kotlin.text.StringBuilder +@kotlin.SinceKotlin(version = "1.9") +public inline fun kotlin.text.StringBuilder.append(value: kotlin.Short): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.4") @kotlin.internal.InlineOnly public inline fun kotlin.text.Appendable.appendLine(): kotlin.text.Appendable @@ -439,6 +445,12 @@ public inline fun kotlin.CharSequence.indexOfFirst(predicate: (kotlin.Char) -> k public inline fun kotlin.CharSequence.indexOfLast(predicate: (kotlin.Char) -> kotlin.Boolean): kotlin.Int +@kotlin.SinceKotlin(version = "1.9") +public inline fun kotlin.text.StringBuilder.insert(index: kotlin.Int, value: kotlin.Byte): kotlin.text.StringBuilder + +@kotlin.SinceKotlin(version = "1.9") +public inline fun kotlin.text.StringBuilder.insert(index: kotlin.Int, value: kotlin.Short): kotlin.text.StringBuilder + @kotlin.SinceKotlin(version = "1.4") public inline fun kotlin.text.StringBuilder.insertRange(index: kotlin.Int, value: kotlin.CharArray, startIndex: kotlin.Int, endIndex: kotlin.Int): kotlin.text.StringBuilder diff --git a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt index 650dfc4a559..ce67ca6e855 100644 --- a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt +++ b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt @@ -651,6 +651,50 @@ public actual class StringBuilder public actual constructor(content: String) : A } +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE") +@SinceKotlin("1.9") +public actual inline fun StringBuilder.append(value: Byte): StringBuilder = this.append(value) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE") +@SinceKotlin("1.9") +public actual inline fun StringBuilder.append(value: Short): StringBuilder = this.append(value) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE") +@SinceKotlin("1.9") +public actual inline fun StringBuilder.insert(index: Int, value: Byte): StringBuilder = this.insert(index, value) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE") +@SinceKotlin("1.9") +public actual inline fun StringBuilder.insert(index: Int, value: Short): StringBuilder = this.insert(index, value) + /** * Clears the content of this string builder making it empty and returns this instance. * diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt index 50f7b909fb5..45e79e7c246 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt @@ -8,6 +8,54 @@ package kotlin.text +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.append(value: Byte): StringBuilder = this.append(value.toInt()) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.append(value: Short): StringBuilder = this.append(value.toInt()) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.insert(index: Int, value: Byte): StringBuilder = this.insert(index, value.toInt()) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.insert(index: Int, value: Short): StringBuilder = this.insert(index, value.toInt()) + /** * Clears the content of this string builder making it empty and returns this instance. * diff --git a/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt index 93b9965ce02..d2050dadf20 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt @@ -714,6 +714,55 @@ private constructor (private var array: CharArray) : CharSequence, Appendable { } } + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.append(value: Byte): StringBuilder = this.append(value) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.append(value: Short): StringBuilder = this.append(value) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.insert(index: Int, value: Byte): StringBuilder = this.insert(index, value) + +/** + * 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. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +@SinceKotlin("1.9") +@kotlin.internal.InlineOnly +public actual inline fun StringBuilder.insert(index: Int, value: Short): StringBuilder = this.insert(index, value) + /** * Clears the content of this string builder making it empty and returns this instance. * diff --git a/libraries/stdlib/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/src/kotlin/text/StringBuilder.kt index 5ecf8293454..3ba90e98487 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilder.kt @@ -324,6 +324,46 @@ public expect class StringBuilder : Appendable, CharSequence { } +/** + * 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") +public expect fun StringBuilder.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") +public expect fun StringBuilder.append(value: Short): 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") +public expect fun StringBuilder.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") +public expect fun StringBuilder.insert(index: Int, value: Short): StringBuilder + /** * Clears the content of this string builder making it empty and returns this instance. *