diff --git a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt index 88d74f2f956..8141e6e13fe 100644 --- a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt +++ b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt @@ -116,10 +116,12 @@ public actual class StringBuilder actual constructor(content: String) : Appendab /** * Appends the specified string [value] to this string builder and returns this instance. + * + * If [value] is `null`, then the four characters `"null"` are appended. */ @SinceKotlin("1.3") - actual fun append(value: String): StringBuilder { - this.string += value + actual fun append(value: String?): StringBuilder { + this.string += value ?: "null" return this } @@ -280,14 +282,17 @@ public actual class StringBuilder actual constructor(content: String) : Appendab /** * Inserts the string [value] into this string builder at the specified [index] and returns this instance. * + * If [value] is `null`, then the four characters `"null"` are inserted. + * * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. */ @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) - actual fun insert(index: Int, value: String): StringBuilder { + actual fun insert(index: Int, value: String?): StringBuilder { AbstractList.checkPositionIndex(index, length) - this.string = this.string.substring(0, index) + value + this.string.substring(index) + val toInsert = value ?: "null" + this.string = this.string.substring(0, index) + toInsert + this.string.substring(index) return this } diff --git a/libraries/stdlib/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/src/kotlin/text/StringBuilder.kt index f9388f9dc16..faef1ff6aaa 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilder.kt @@ -77,9 +77,11 @@ expect class StringBuilder : Appendable, CharSequence { /** * Appends the specified string [value] to this string builder and returns this instance. + * + * If [value] is `null`, then the four characters `"null"` are appended. */ @SinceKotlin("1.3") - fun append(value: String): StringBuilder + fun append(value: String?): StringBuilder /** * Returns the current capacity of this string builder. @@ -200,11 +202,13 @@ expect class StringBuilder : Appendable, CharSequence { /** * Inserts the string [value] into this string builder at the specified [index] and returns this instance. * + * If [value] is `null`, then the four characters `"null"` are inserted. + * * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. */ @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) - fun insert(index: Int, value: String): StringBuilder + fun insert(index: Int, value: String?): StringBuilder /** * Sets the length of this string builder to the specified [newLength]. diff --git a/libraries/stdlib/test/text/StringBuilderTest.kt b/libraries/stdlib/test/text/StringBuilderTest.kt index f237f61b1bd..25b4b4186f4 100644 --- a/libraries/stdlib/test/text/StringBuilderTest.kt +++ b/libraries/stdlib/test/text/StringBuilderTest.kt @@ -153,6 +153,9 @@ class StringBuilderTest { StringBuilder().let { sb -> repeat(times) { sb.append("foo") } assertEquals(expected, sb.toString()) + + sb.append(null as String?) + assertEquals(expected + "null", sb.toString()) } } @@ -384,6 +387,8 @@ class StringBuilderTest { assertEquals("_my insertTtT string test", sb.toString()) sb.insert(25, "_!_") assertEquals("_my insertTtT string test_!_", sb.toString()) + sb.insert(13, null as String?) + assertEquals("_my insertTtTnull string test_!_", sb.toString()) } }