joinTo/joinToString: Do not call toString on elements that are already CharSequence or Char.
#KT-15557
This commit is contained in:
@@ -12158,10 +12158,7 @@ public fun <T, A : Appendable> Array<out T>.joinTo(buffer: A, separator: CharSeq
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
@@ -12345,7 +12342,7 @@ public fun <A : Appendable> CharArray.joinTo(buffer: A, separator: CharSequence
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(element.toString())
|
||||
buffer.append(element)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -2000,10 +2000,7 @@ public fun <T, A : Appendable> Iterable<T>.joinTo(buffer: A, separator: CharSequ
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -1271,10 +1271,7 @@ public fun <T, A : Appendable> Sequence<T>.joinTo(buffer: A, separator: CharSequ
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -6812,10 +6812,7 @@ public fun <T, A : Appendable> Array<out T>.joinTo(buffer: A, separator: CharSeq
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
@@ -6999,7 +6996,7 @@ public fun <A : Appendable> CharArray.joinTo(buffer: A, separator: CharSequence
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(element.toString())
|
||||
buffer.append(element)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -1013,10 +1013,7 @@ public fun <T, A : Appendable> Iterable<T>.joinTo(buffer: A, separator: CharSequ
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -705,10 +705,7 @@ public fun <T, A : Appendable> Sequence<T>.joinTo(buffer: A, separator: CharSequ
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -12241,10 +12241,7 @@ public fun <T, A : Appendable> Array<out T>.joinTo(buffer: A, separator: CharSeq
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
@@ -12428,7 +12425,7 @@ public fun <A : Appendable> CharArray.joinTo(buffer: A, separator: CharSequence
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(element.toString())
|
||||
buffer.append(element)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -2011,10 +2011,7 @@ public fun <T, A : Appendable> Iterable<T>.joinTo(buffer: A, separator: CharSequ
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -1290,10 +1290,7 @@ public fun <T, A : Appendable> Sequence<T>.joinTo(buffer: A, separator: CharSequ
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
@@ -53,3 +53,13 @@ public fun StringBuilder.append(vararg value: Any?): StringBuilder {
|
||||
@kotlin.jvm.JvmVersion
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun StringBuilder.set(index: Int, value: Char): Unit = this.setCharAt(index, value)
|
||||
|
||||
|
||||
internal fun <T> 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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,6 +259,22 @@ class ArraysTest {
|
||||
}
|
||||
|
||||
|
||||
@Test fun joinToString() {
|
||||
val text = arrayOf("foo", "bar").joinToString("-", "<", ">")
|
||||
assertEquals("<foo-bar>", text)
|
||||
|
||||
val text2 = arrayOf('a', "b", StringBuilder("c"), null, "d", 'e', 'f').joinToString(limit = 4, truncated = "*")
|
||||
assertEquals("a, b, c, null, *", text2)
|
||||
|
||||
val text3 = intArrayOf(1, 2, 5, 8).joinToString("+", "[", "]")
|
||||
assertEquals("[1+2+5+8]", text3)
|
||||
|
||||
val text4 = charArrayOf('f', 'o', 'o').joinToString()
|
||||
assertEquals("f, o, o", text4)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test fun min() {
|
||||
expect(null, { arrayOf<Int>().min() })
|
||||
expect(1, { arrayOf(1).min() })
|
||||
|
||||
@@ -42,9 +42,9 @@ class CollectionTest {
|
||||
val text = data.joinToString("-", "<", ">")
|
||||
assertEquals("<foo-bar>", text)
|
||||
|
||||
val big = listOf("a", "b", "c", "d", "e", "f")
|
||||
val text2 = big.joinToString(limit = 3, truncated = "*")
|
||||
assertEquals("a, b, c, *", text2)
|
||||
val mixed = listOf('a', "b", StringBuilder("c"), null, "d", 'e', 'f')
|
||||
val text2 = mixed.joinToString(limit = 4, truncated = "*")
|
||||
assertEquals("a, b, c, null, *", text2)
|
||||
}
|
||||
|
||||
@Test fun filterNotNull() {
|
||||
|
||||
@@ -23,10 +23,7 @@ fun strings(): List<GenericFunction> {
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(if (element == null) "null" else element.toString())
|
||||
buffer.appendElement(element, transform)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
@@ -35,7 +32,7 @@ fun strings(): List<GenericFunction> {
|
||||
"""
|
||||
}
|
||||
exclude(Strings)
|
||||
body(ArraysOfPrimitives) {
|
||||
bodyForTypes(ArraysOfPrimitives, *defaultPrimitives.toTypedArray()) { primitive ->
|
||||
"""
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
@@ -45,7 +42,7 @@ fun strings(): List<GenericFunction> {
|
||||
if (transform != null)
|
||||
buffer.append(transform(element))
|
||||
else
|
||||
buffer.append(element.toString())
|
||||
buffer.append(${if (primitive == PrimitiveType.Char) "element" else "element.toString()"})
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
|
||||
Reference in New Issue
Block a user