Make consistent parameter nullability with appendLine

This commit is contained in:
Abduqodiri Qurbonzoda
2020-05-29 01:37:33 +03:00
parent e05eeea6cd
commit 1bd63bb07f
3 changed files with 20 additions and 6 deletions
@@ -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. * 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") @SinceKotlin("1.3")
actual fun append(value: String): StringBuilder { actual fun append(value: String?): StringBuilder {
this.string += value this.string += value ?: "null"
return this 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. * 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. * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@WasExperimental(ExperimentalStdlibApi::class) @WasExperimental(ExperimentalStdlibApi::class)
actual fun insert(index: Int, value: String): StringBuilder { actual fun insert(index: Int, value: String?): StringBuilder {
AbstractList.checkPositionIndex(index, length) 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 return this
} }
@@ -77,9 +77,11 @@ expect class StringBuilder : Appendable, CharSequence {
/** /**
* Appends the specified string [value] to this string builder and returns this instance. * 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") @SinceKotlin("1.3")
fun append(value: String): StringBuilder fun append(value: String?): StringBuilder
/** /**
* Returns the current capacity of this string builder. * 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. * 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. * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@WasExperimental(ExperimentalStdlibApi::class) @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]. * Sets the length of this string builder to the specified [newLength].
@@ -153,6 +153,9 @@ class StringBuilderTest {
StringBuilder().let { sb -> StringBuilder().let { sb ->
repeat(times) { sb.append("foo") } repeat(times) { sb.append("foo") }
assertEquals(expected, sb.toString()) 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()) assertEquals("_my insertTtT string test", sb.toString())
sb.insert(25, "_!_") sb.insert(25, "_!_")
assertEquals("_my insertTtT string test_!_", sb.toString()) assertEquals("_my insertTtT string test_!_", sb.toString())
sb.insert(13, null as String?)
assertEquals("_my insertTtTnull string test_!_", sb.toString())
} }
} }