From 0c033297a8de7e046c2b3081b443baa099835ce4 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 14 Nov 2019 07:09:42 +0300 Subject: [PATCH] Move Appendable to its own file --- libraries/stdlib/common/src/kotlin/TextH.kt | 6 -- .../stdlib/js/src/kotlin/text/AppendableJs.kt | 37 ++++++++++++ .../js/src/kotlin/text/StringBuilderJs.kt | 7 --- .../stdlib/src/kotlin/text/Appendable.kt | 59 +++++++++++++++++++ .../stdlib/src/kotlin/text/StringBuilder.kt | 19 ------ 5 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 libraries/stdlib/js/src/kotlin/text/AppendableJs.kt create mode 100644 libraries/stdlib/src/kotlin/text/Appendable.kt diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index cd5d183d00c..d5948fbebd3 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -5,12 +5,6 @@ package kotlin.text -expect interface Appendable { - fun append(c: Char): Appendable - fun append(csq: CharSequence?): Appendable - fun append(csq: CharSequence?, start: Int, end: Int): Appendable -} - expect class StringBuilder : Appendable, CharSequence { constructor() constructor(capacity: Int) diff --git a/libraries/stdlib/js/src/kotlin/text/AppendableJs.kt b/libraries/stdlib/js/src/kotlin/text/AppendableJs.kt new file mode 100644 index 00000000000..dee565e2e05 --- /dev/null +++ b/libraries/stdlib/js/src/kotlin/text/AppendableJs.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin.text + +/** + * An object to which char sequences and values can be appended. + */ +public actual interface Appendable { + /** + * Appends the specified character [c] to this Appendable and returns this instance. + * + * @param c the character to append. + */ + public actual fun append(c: Char): Appendable + + /** + * Appends the specified character sequence [csq] to this Appendable and returns this instance. + * + * @param csq the character sequence to append. If [csq] is `null`, then the four characters `"null"` are appended to this Appendable. + */ + public actual fun append(csq: CharSequence?): Appendable + + /** + * Appends a subsequence of the specified character sequence [csq] to this Appendable and returns this instance. + * + * @param csq the character sequence from which a subsequence is appended. If [csq] is `null`, + * then characters are appended as if [csq] contained the four characters `"null"`. + * @param start the beginning (inclusive) of the subsequence to append. + * @param end the end (exclusive) of the subsequence to append. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [start] or [end] is out of range of the [csq] character sequence indices or when `start > end`. + */ + public actual fun append(csq: CharSequence?, start: Int, end: Int): Appendable +} \ No newline at end of file diff --git a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt index b30eb9e52bc..fc12d7cabea 100644 --- a/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt +++ b/libraries/stdlib/js/src/kotlin/text/StringBuilderJs.kt @@ -5,13 +5,6 @@ package kotlin.text - -public actual interface Appendable { - public actual fun append(csq: CharSequence?): Appendable - public actual fun append(csq: CharSequence?, start: Int, end: Int): Appendable - public actual fun append(c: Char): Appendable -} - public actual class StringBuilder(content: String = "") : Appendable, CharSequence { actual constructor(capacity: Int) : this() {} diff --git a/libraries/stdlib/src/kotlin/text/Appendable.kt b/libraries/stdlib/src/kotlin/text/Appendable.kt new file mode 100644 index 00000000000..742cda8971f --- /dev/null +++ b/libraries/stdlib/src/kotlin/text/Appendable.kt @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("StringsKt") + +package kotlin.text + +/** + * An object to which char sequences and values can be appended. + */ +expect interface Appendable { + /** + * Appends the specified character [c] to this Appendable and returns this instance. + * + * @param c the character to append. + */ + fun append(c: Char): Appendable + + /** + * Appends the specified character sequence [csq] to this Appendable and returns this instance. + * + * @param csq the character sequence to append. If [csq] is `null`, then the four characters `"null"` are appended to this Appendable. + */ + fun append(csq: CharSequence?): Appendable + + /** + * Appends a subsequence of the specified character sequence [csq] to this Appendable and returns this instance. + * + * @param csq the character sequence from which a subsequence is appended. If [csq] is `null`, + * then characters are appended as if [csq] contained the four characters `"null"`. + * @param start the beginning (inclusive) of the subsequence to append. + * @param end the end (exclusive) of the subsequence to append. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [start] or [end] is out of range of the [csq] character sequence indices or when `start > end`. + */ + fun append(csq: CharSequence?, start: Int, end: Int): Appendable +} + +/** + * Appends all arguments to the given [Appendable]. + */ +public fun T.append(vararg value: CharSequence?): T { + for (item in value) + append(item) + return this +} + + +internal fun Appendable.appendElement(element: T, transform: ((T) -> CharSequence)?) { + when { + transform != null -> append(transform(element)) + element is CharSequence? -> append(element) + element is Char -> append(element) + else -> append(element.toString()) + } +} diff --git a/libraries/stdlib/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/src/kotlin/text/StringBuilder.kt index 9f06ff4c924..c35e5bf63b4 100644 --- a/libraries/stdlib/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/src/kotlin/text/StringBuilder.kt @@ -25,15 +25,6 @@ public inline fun buildString(builderAction: StringBuilder.() -> Unit): String = public inline fun buildString(capacity: Int, builderAction: StringBuilder.() -> Unit): String = StringBuilder(capacity).apply(builderAction).toString() -/** - * Appends all arguments to the given [Appendable]. - */ -public fun T.append(vararg value: CharSequence?): T { - for (item in value) - append(item) - return this -} - /** * Appends all arguments to the given StringBuilder. */ @@ -51,13 +42,3 @@ public fun StringBuilder.append(vararg value: Any?): StringBuilder { append(item) return this } - - -internal fun Appendable.appendElement(element: T, transform: ((T) -> CharSequence)?) { - when { - transform != null -> append(transform(element)) - element is CharSequence? -> append(element) - element is Char -> append(element) - else -> append(element.toString()) - } -}