Deprecate StringBuilder { } function, and introduce buildString { } instead.

#KT-7295 Fixed
This commit is contained in:
Ilya Gorbunov
2015-10-08 21:25:44 +03:00
parent 6d7cc0671e
commit af0a59dd02
3 changed files with 14 additions and 12 deletions
+2 -2
View File
@@ -170,13 +170,13 @@ public fun Element.addClass(vararg cssClasses: String): Boolean {
val missingClasses = cssClasses.filterNot { hasClass(it) } val missingClasses = cssClasses.filterNot { hasClass(it) }
if (missingClasses.isNotEmpty()) { if (missingClasses.isNotEmpty()) {
val presentClasses = classes.trim() val presentClasses = classes.trim()
classes = StringBuilder { classes = buildString {
append(presentClasses) append(presentClasses)
if (!presentClasses.isEmpty()) { if (!presentClasses.isEmpty()) {
append(" ") append(" ")
} }
missingClasses.joinTo(this, " ") missingClasses.joinTo(this, " ")
}.toString() }
return true return true
} }
@@ -6,14 +6,16 @@ package kotlin
/** /**
* Builds newly created StringBuilder using provided body. * Builds newly created StringBuilder using provided body.
*/ */
public inline fun StringBuilder(body: StringBuilder.() -> Unit): StringBuilder { @Deprecated("Use StringBuilder().apply { body } or use String.build { body } if you need String as a result.", ReplaceWith("StringBuilder().apply(body)"))
val sb = StringBuilder() public inline fun StringBuilder(body: StringBuilder.() -> Unit): StringBuilder = StringBuilder().apply(body)
sb.body()
return sb
}
/** /**
* Appends all arguments to the given Appendable. * Builds new string by populating newly created [StringBuilder] using provided [builderAction] and then converting it to [String].
*/
public inline fun buildString(builderAction: StringBuilder.() -> Unit): String = StringBuilder().apply(builderAction).toString()
/**
* Appends all arguments to the given [Appendable].
*/ */
public fun <T : Appendable> T.append(vararg value: CharSequence?): T { public fun <T : Appendable> T.append(vararg value: CharSequence?): T {
for (item in value) for (item in value)
@@ -6,10 +6,10 @@ import org.junit.Test as test
class StringBuilderTest { class StringBuilderTest {
@test fun stringBuild() { @test fun stringBuild() {
val s = StringBuilder { val s = buildString {
append("a") append("a")
append(true) append(true)
}.toString() }
assertEquals("atrue", s) assertEquals("atrue", s)
} }
@@ -21,8 +21,8 @@ class StringBuilderTest {
@test fun append() { @test fun append() {
// this test is needed for JS implementation // this test is needed for JS implementation
assertEquals("em", StringBuilder { assertEquals("em", buildString {
append("element", 2, 4) append("element", 2, 4)
}.toString()) })
} }
} }