From 08ff52bee4ae811891c53a75c58390b8b46a698c Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 2 Apr 2020 12:54:50 +0300 Subject: [PATCH] Introduce StringBuilder.appendLine in stdlib-common #KT-37839 --- .../jvm/src/kotlin/text/StringBuilderJVM.kt | 147 ++++++++++++++++++ .../stdlib/src/kotlin/text/Appendable.kt | 15 ++ .../stdlib/src/kotlin/text/StringBuilder.kt | 36 +++++ .../stdlib/test/text/StringBuilderTest.kt | 32 ++++ 4 files changed, 230 insertions(+) diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt index e7f43e5cb59..bc6602f4d0f 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringBuilderJVM.kt @@ -158,6 +158,47 @@ public actual inline fun StringBuilder.insertRange(index: Int, value: CharSequen this.insert(index, value, startIndex, endIndex) +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendLine(value: StringBuffer?): 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: StringBuilder?): 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() + +/** 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() + +/** 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() + +/** 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() + +/** 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() + +/** 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() + + private object SystemProperties { /** Line separator for current system. */ @JvmField @@ -165,71 +206,177 @@ private object SystemProperties { } /** Appends a line separator to this Appendable. */ +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine()"), + level = DeprecationLevel.WARNING +) public fun Appendable.appendln(): Appendable = append(SystemProperties.LINE_SEPARATOR) /** Appends value to the given Appendable and line separator after it. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun Appendable.appendln(value: CharSequence?): Appendable = append(value).appendln() /** Appends value to the given Appendable and line separator after it. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun Appendable.appendln(value: Char): Appendable = append(value).appendln() /** Appends a line separator to this StringBuilder. */ +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine()"), + level = DeprecationLevel.WARNING +) public fun StringBuilder.appendln(): StringBuilder = append(SystemProperties.LINE_SEPARATOR) /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: StringBuffer?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: CharSequence?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: String?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Any?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: StringBuilder?): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: CharArray): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Char): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Boolean): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Int): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Short): StringBuilder = append(value.toInt()).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Byte): StringBuilder = append(value.toInt()).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Long): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Float): StringBuilder = append(value).appendln() /** Appends [value] to this [StringBuilder], followed by a line separator. */ +@Suppress("DEPRECATION") +@Deprecated( + "Use appendLine instead. Note that the new method always appends the line feed character '\\n' regardless of the system line separator.", + ReplaceWith("appendLine(value)"), + level = DeprecationLevel.WARNING +) @kotlin.internal.InlineOnly public inline fun StringBuilder.appendln(value: Double): StringBuilder = append(value).appendln() diff --git a/libraries/stdlib/src/kotlin/text/Appendable.kt b/libraries/stdlib/src/kotlin/text/Appendable.kt index 6d26fd12ced..cabb8699a78 100644 --- a/libraries/stdlib/src/kotlin/text/Appendable.kt +++ b/libraries/stdlib/src/kotlin/text/Appendable.kt @@ -65,6 +65,21 @@ public fun T.append(vararg value: CharSequence?): T { return this } +/** Appends a line feed character (`\n`) to this Appendable. */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun Appendable.appendLine(): Appendable = append('\n') + +/** Appends value to the given Appendable and a line feed character (`\n`) after it. */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun Appendable.appendLine(value: CharSequence?): Appendable = append(value).appendLine() + +/** Appends value to the given Appendable and a line feed character (`\n`) after it. */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun Appendable.appendLine(value: Char): Appendable = append(value).appendLine() + internal fun Appendable.appendElement(element: T, transform: ((T) -> CharSequence)?) { when { diff --git a/libraries/stdlib/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/src/kotlin/text/StringBuilder.kt index c91b16f3708..9c16f87f87a 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilder.kt @@ -428,3 +428,39 @@ public fun StringBuilder.append(vararg value: Any?): StringBuilder { append(item) return this } + +/** Appends a line feed character (`\n`) to this StringBuilder. */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendLine(): StringBuilder = append('\n') + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendLine(value: CharSequence?): 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: String?): 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: Any?): StringBuilder = append(value).appendLine() + +/** Appends [value] to this [StringBuilder], followed by a line feed character (`\n`). */ +@OptIn(ExperimentalStdlibApi::class) +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun StringBuilder.appendLine(value: CharArray): 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: Char): 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: Boolean): StringBuilder = append(value).appendLine() diff --git a/libraries/stdlib/test/text/StringBuilderTest.kt b/libraries/stdlib/test/text/StringBuilderTest.kt index b5ed72b20d8..0341c7ac791 100644 --- a/libraries/stdlib/test/text/StringBuilderTest.kt +++ b/libraries/stdlib/test/text/StringBuilderTest.kt @@ -484,4 +484,36 @@ class StringBuilderTest { assertFails { sb.toCharArray(chars, 0, 0, sb.length + 1) } } } + + @Test + fun appendLine() { + val stringBuilder = StringBuilder() + stringBuilder.appendLine('c') + stringBuilder.appendLine("string") + stringBuilder.appendLine(true) + stringBuilder.appendLine(charArrayOf('a', 'r', 'r', 'a', 'y')) + stringBuilder.appendLine() + stringBuilder.appendLine("char sequence" as CharSequence) + stringBuilder.appendLine(null as Any?) + stringBuilder.appendLine("nonnull" as Any?) + stringBuilder.appendLine(null as String?) + stringBuilder.appendLine(null as CharSequence?) + + val expected = + """ + c + string + true + array + + char sequence + null + nonnull + null + null + + """.trimIndent() + + assertEquals(expected, stringBuilder.toString()) + } }