StringBuilder builder and appendln
This commit is contained in:
committed by
Andrey Breslav
parent
0717511abe
commit
516bae17d7
@@ -0,0 +1,37 @@
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* Builds newly created StringBuilder using provided body.
|
||||
*/
|
||||
public inline fun StringBuilder(body: StringBuilder.() -> Unit): StringBuilder {
|
||||
val sb = StringBuilder()
|
||||
sb.body()
|
||||
return sb
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all arguments to the given Appendable
|
||||
*/
|
||||
public fun <T : Appendable> T.append(vararg value: CharSequence?): T {
|
||||
for (item in value)
|
||||
append(item)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all arguments to the given StringBuilder
|
||||
*/
|
||||
public fun StringBuilder.append(vararg value: String?): StringBuilder {
|
||||
for (item in value)
|
||||
append(item)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all arguments to the given StringBuilder
|
||||
*/
|
||||
public fun StringBuilder.append(vararg value: Any?): StringBuilder {
|
||||
for (item in value)
|
||||
append(item)
|
||||
return this
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package kotlin
|
||||
|
||||
/** Line separator for current system. */
|
||||
val LINE_SEPARATOR: String = System.getProperty("line.separator")!!
|
||||
|
||||
/** Appends line separator to Appendable. */
|
||||
public fun Appendable.appendln(): Appendable = append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given Appendable and line separator after it. */
|
||||
public fun Appendable.appendln(value: CharSequence?): Appendable = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given Appendable and line separator after it. */
|
||||
public fun Appendable.appendln(value: Char): Appendable = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends line separator to StringBuilder. */
|
||||
|
||||
public fun StringBuilder.appendln(): StringBuilder = append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: StringBuffer?): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: CharSequence?): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: String?): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Any?): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: StringBuilder?): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: CharArray): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Char): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Boolean): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Int): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Short): StringBuilder = append(value.toInt()).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Byte): StringBuilder = append(value.toInt()).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Long): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Float): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
|
||||
/** Appends value to the given StringBuilder and line separator after it. */
|
||||
public fun StringBuilder.appendln(value: Double): StringBuilder = append(value).append(LINE_SEPARATOR)
|
||||
@@ -57,11 +57,11 @@ public val String.indices: IntRange
|
||||
* Returns a subsequence specified by given set of indices.
|
||||
*/
|
||||
public fun CharSequence.slice(indices: Iterable<Int>): CharSequence {
|
||||
val result = StringBuilder()
|
||||
val sb = StringBuilder()
|
||||
for (i in indices) {
|
||||
result.append(get(i))
|
||||
sb.append(get(i))
|
||||
}
|
||||
return result.toString()
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package test.text
|
||||
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
class StringBuilderTest {
|
||||
|
||||
test fun stringBuild() {
|
||||
val s = StringBuilder {
|
||||
append("a")
|
||||
append(true)
|
||||
}.toString()
|
||||
assertEquals("atrue", s)
|
||||
}
|
||||
|
||||
test fun appendMany() {
|
||||
assertEquals("a1", StringBuilder().append("a", "1").toString())
|
||||
assertEquals("a1", StringBuilder().append("a", 1).toString())
|
||||
assertEquals("a1", StringBuilder().append("a", StringBuilder().append("1")).toString())
|
||||
}
|
||||
|
||||
test fun append() {
|
||||
// this test is needed for JS implementation
|
||||
assertEquals("em", StringBuilder {
|
||||
append("element", 2, 4)
|
||||
}.toString())
|
||||
}
|
||||
}
|
||||
@@ -123,5 +123,4 @@ class StringTest {
|
||||
assertEquals("new name", s.replaceBefore("=", "new name"))
|
||||
assertEquals("/new/path", s.replaceBeforeLast("=", "/new/path"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user